check folder size in ubuntu

Find the size of a directory in Linux

To find out the size of a directory, we will use ‘du’ command. du stands for disk usage.

The typical syntax of du command is given below:

du [OPTION]... [FILE] [directory]...

du [OPTION]... --files0-from=F

Let us type the ‘du’ command in the Terminal and see what it displays.

$ du
148     ./.npm/_cacache/index-v5/e2
8       ./.npm/_cacache/index-v5/07/0f
8       ./.npm/_cacache/index-v5/07/dd
8       ./.npm/_cacache/index-v5/07/16
8       ./.npm/_cacache/index-v5/07/43
8       ./.npm/_cacache/index-v5/07/2b
12      ./.npm/_cacache/index-v5/07/4d
8       ./.npm/_cacache/index-v5/07/a6
8       ./.npm/_cacache/index-v5/07/e3
72      ./.npm/_cacache/index-v5/07
8       ./.npm/_cacache/index-v5/04/d4
8       ./.npm/_cacache/index-v5/04/11
8       ./.npm/_cacache/index-v5/04/ba
8       ./.npm/_cacache/index-v5/04/52
8       ./.npm/_cacache/index-v5/04/be
8       ./.npm/_cacache/index-v5/04/01
12      ./.npm/_cacache/index-v5/04/2a
8       ./.npm/_cacache/index-v5/04/68
8       ./.npm/_cacache/index-v5/04/6f

As you see above, du command displays the disk usage of the directories along with its sub-directories in the current directory.

To display a particular directory’s size, for example public_html, run:

$ du public_html/
ubuntu@www:~$ du public_html/
56      public_html/routes
132     public_html/vendor/nikic/php-parser/grammar
68      public_html/vendor/nikic/php-parser/doc/component
100     public_html/vendor/nikic/php-parser/doc
100     public_html/vendor/nikic/php-parser/test/code/prettyPrinter/stmt
84      public_html/vendor/nikic/php-parser/test/code/prettyPrinter/expr
212     public_html/vendor/nikic/php-parser/test/code/prettyPrinter
68      public_html/vendor/nikic/php-parser/test/code/parser/stmt/namespace
48      public_html/vendor/nikic/php-parser/test/code/parser/stmt/function
84      public_html/vendor/nikic/php-parser/test/code/parser/stmt/class
20      public_html/vendor/nikic/php-parser/test/code/parser/stmt/loop
32      public_html/vendor/nikic/php-parser/test/code/parser/stmt/generator
324     public_html/vendor/nikic/php-parser/test/code/parser/stmt
64      public_html/vendor/nikic/php-parser/test/code/parser/scalar
44      public_html/vendor/nikic/php-parser/test/code/parser/errorHandling
44      public_html/vendor/nikic/php-parser/test/code/parser/expr/uvs
52      public_html/vendor/nikic/php-parser/test/code/parser/expr/fetchAndCall
264     public_html/vendor/nikic/php-parser/test/code/parser/expr
732     public_html/vendor/nikic/php-parser/test/code/parser
84      public_html/vendor/nikic/php-parser/test/code/formatPreservation
1032    public_html/vendor/nikic/php-parser/test/code
16      public_html/vendor/nikic/php-parser/test/PhpParser/Lexer
24      public_html/vendor/nikic/php-parser/test/PhpParser/Node/Stmt
12      public_html/vendor/nikic/php-parser/test/PhpParser/Node/Scalar
52      public_html/vendor/nikic/php-parser/test/PhpParser/Node
12      public_html/vendor/nikic/php-parser/test/PhpParser/ErrorHandler
60      public_html/vendor/nikic/php-parser/test/PhpParser/Builder
8       public_html/vendor/nikic/php-parser/test/PhpParser/Internal
28      public_html/vendor/nikic/php-parser/test/PhpParser/NodeVisitor
16      public_html/vendor/nikic/php-parser/test/PhpParser/Parser
320     public_html/vendor/nikic/php-parser/test/PhpParser
1364    public_html/vendor/nikic/php-parser/test
12      public_html/vendor/nikic/php-parser/bin
16      public_html/vendor/nikic/php-parser/test_old
20      public_html/vendor/nikic/php-parser/lib/PhpParser/Lexer/TokenEmulator
36      public_html/vendor/nikic/php-parser/lib/PhpParser/Lexer
44      public_html/vendor/nikic/php-parser/lib/PhpParser/PrettyPrinter
12      public_html/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/TraitUseAdap                                                                                        tation
200     public_html/vendor/nikic/php-parser/lib/PhpParser/Node/Stmt
12      public_html/vendor/nikic/php-parser/lib/PhpParser/Node/Name
112     public_html/vendor/nikic/php-parser/lib/PhpParser/Node/Expr/BinaryOp
56      public_html/vendor/nikic/php-parser/lib/PhpParser/Node/Expr/AssignOp
32      public_html/vendor/nikic/php-parser/lib/PhpParser/Node/Expr/Cast
376     public_html/vendor/nikic/php-parser/lib/PhpParser/Node/Expr
36      public_html/vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/MagicConst
68      public_html/vendor/nikic/php-parser/lib/PhpParser/Node/Scalar
708     public_html/vendor/nikic/php-parser/lib/PhpParser/Node
8       public_html/vendor/nikic/php-parser/lib/PhpParser/Comment
12      public_html/vendor/nikic/php-parser/lib/PhpParser/ErrorHandler
60      public_html/vendor/nikic/php-parser/lib/PhpParser/Builder
28      public_html/vendor/nikic/php-parser/lib/PhpParser/Internal
28      public_html/vendor/nikic/php-parser/lib/PhpParser/NodeVisitor
324     public_html/vendor/nikic/php-parser/lib/PhpParser/Parser
1500    public_html/vendor/nikic/php-parser/lib/PhpParser
1504    public_html/vendor/nikic/php-parser/lib
3212    public_html/vendor/nikic/php-parser
3216    public_html/vendor/nikic
24      public_html/vendor/league/flysystem-aws-s3-v3/src
40      public_html/vendor/league/flysystem-aws-s3-v3
44      public_html/vendor/league/flysystem/src/Plugin
24      public_html/vendor/league/flysystem/src/Util
24      public_html/vendor/league/flysystem/src/Adapter/Polyfill
96      public_html/vendor/league/flysystem/src/Adapter
288     public_html/vendor/league/flysystem/src
312     public_html/vendor/league/flysystem
76      public_html/vendor/league/oauth2-server/src/Grant
12      public_html/vendor/league/oauth2-server/src/AuthorizationValidators
12      public_html/vendor/league/oauth2-server/src/RequestTypes
20      public_html/vendor/league/oauth2-server/src/Exception
32      public_html/vendor/league/oauth2-server/src/Entities/Traits
64      public_html/vendor/league/oauth2-server/src/Entities
12      public_html/vendor/league/oauth2-server/src/Middleware
32      public_html/vendor/league/oauth2-server/src/Repositories
20      public_html/vendor/league/oauth2-server/src/ResponseTypes
276     public_html/vendor/league/oauth2-server/src
300     public_html/vendor/league/oauth2-server
84      public_html/vendor/league/event/src
96      public_html/vendor/league/event
752     public_html/vendor/league
28      public_html/vendor/filp/whoops/src/Whoops/Util
40      public_html/vendor/filp/whoops/src/Whoops/Exception
60      public_html/vendor/filp/whoops/src/Whoops/Handler
16      public_html/vendor/filp/whoops/src/Whoops/Resources/css
60      public_html/vendor/filp/whoops/src/Whoops/Resources/views

We can also display the size in “human readable format” (i.e. auto-selecting the appropriate unit for each size), rather than the standard block size.

To do so, add -h tag with du command as shown below.

$ du -h app/
ubuntu@www:~/public_html$ du -h app
32K     app/Helpers
728K    app/Http/Controllers/Api/V1
732K    app/Http/Controllers/Api
24K     app/Http/Controllers/Auth/Old
48K     app/Http/Controllers/Auth
104K    app/Http/Controllers/Admin
964K    app/Http/Controllers
44K     app/Http/Middleware
1016K   app/Http
24K     app/Providers
8.0K    app/Console
8.0K    app/Exports
12K     app/Mail
8.0K    app/Exceptions
36K     app/Models/api
164K    app/Models
1.4M    app

Now you see the size of the directories in Kilobytes, Megabytes and Gigabytes, which is very clear and easy to understand.

We can also display the disk usage size only in KB, or MB, or GB.

To do so, use -k for kilobytes, -m for megabytes

$ du -k app/
$ du -m app/

To find out which sub-directories consume how much disk size, use this command:

$ du -h --max-depth=1 | sort -hr

The largest sub-directories will be displayed on the top. You can increase the directory depth level by increasing the value of –max-depth parameter.

As you may noticed in the all above outputs, du command only displayed the disk usage of directories. But, what about the files? To display the disk usage of all items including files and directories, use -a flag.

$ du -ah app/
ubuntu@www:~/public_html$ du -ah app
4.0K    app/UserSpokenLanguage.php
4.0K    app/Contact.php
4.0K    app/Country.php
4.0K    app/UserDevice.php
4.0K    app/Sos.php
4.0K    app/UserImage.php
4.0K    app/UserScreenshot.php
8.0K    app/Helpers/Notifications.php
8.0K    app/Helpers/FileUpload.php
8.0K    app/Helpers/Common.php
4.0K    app/Helpers/Helpers.php
32K     app/Helpers
4.0K    app/UserMeetupMessage.php
4.0K    app/Event.php
4.0K    app/Http/Controllers/HomeControllerOld.php
364K    app/Http/Controllers/Api/V1/ApiController.php
360K    app/Http/Controllers/Api/V1/ApiController.php_24_12_2019
728K    app/Http/Controllers/Api/V1
732K    app/Http/Controllers/Api
12K     app/Http/Controllers/WebsiteController04nov.php
28K     app/Http/Controllers/CronjobController.php
4.0K    app/Http/Controllers/Auth/LoginController.php
4.0K    app/Http/Controllers/Auth/ForgotPasswordController.php
4.0K    app/Http/Controllers/Auth/ResetPasswordController.php
4.0K    app/Http/Controllers/Auth/RegisterController.php
4.0K    app/Http/Controllers/Auth/Old/LoginController.php
4.0K    app/Http/Controllers/Auth/Old/ForgotPasswordController.php
4.0K    app/Http/Controllers/Auth/Old/ResetPasswordController.php
4.0K    app/Http/Controllers/Auth/Old/RegisterController.php
4.0K    app/Http/Controllers/Auth/Old/VerificationController.php
24K     app/Http/Controllers/Auth/Old
4.0K    app/Http/Controllers/Auth/VerificationController.php
48K     app/Http/Controllers/Auth
12K     app/Http/Controllers/WebsiteController14nov.php
12K     app/Http/Controllers/WebsiteController.php
4.0K    app/Http/Controllers/Admin/CmsController.php
4.0K    app/Http/Controllers/Admin/ProfessionController.php
12K     app/Http/Controllers/Admin/AdminController.php
4.0K    app/Http/Controllers/Admin/FaqController.php

We can also display the size of multiple directories at once as shown below.

$ du -h directory1 directory2

If you want to check the total disk space used by a particular directory, use the -s flag.

$ du -sh app
ubuntu@www:~/public_html$ du -sh app
1.4M    app
ubuntu@www:~/public_html$

EXAMPLES :

$ du -csh app /home/sk/
2.8G app
279G /home/sk/
281G total

$ du -ch Downloads/ | grep total
12G total

$ du -ch --exclude='*.mp4' | grep total 
6.4G total
$ man du
DU(1)                                                                      User Commands                                                                      DU(1)

NAME
       du - estimate file space usage

SYNOPSIS
       du [OPTION]... [FILE]...
       du [OPTION]... --files0-from=F

DESCRIPTION
       Summarize disk usage of the set of FILEs, recursively for directories.

       Mandatory arguments to long options are mandatory for short options too.

       -0, --null
              end each output line with NUL, not newline

       -a, --all
              write counts for all files, not just directories

       --apparent-size
              print  apparent  sizes,  rather  than  disk  usage; although the apparent size is usually smaller, it may be larger due to holes in ('sparse') files,
              internal fragmentation, indirect blocks, and the like

       -B, --block-size=SIZE
              scale sizes by SIZE before printing them; e.g., '-BM' prints sizes in units of 1,048,576 bytes; see SIZE format below

       -b, --bytes
              equivalent to '--apparent-size --block-size=1'

       -c, --total
              produce a grand total

       -D, --dereference-args
              dereference only symlinks that are listed on the command line

       -d, --max-depth=N
              print the total for a directory (or file, with --all) only if it is N or fewer levels below the command line argument;  --max-depth=0 is the same  as
              --summarize

       --files0-from=F
              summarize disk usage of the NUL-terminated file names specified in file F; if F is -, then read names from standard input

Leave a Reply

Your email address will not be published. Required fields are marked *