How to find size of directory in linux / du command tutorial in linux

Determining the size of a directory or its sub-directories is often required for the purpose of monitoring such as a folder which is taking space above some limit.

du command in linux
Linux provides du(stands for disk usage) command to list down the size of a folder and its sub-folders and files contained in them. By using this command, you can

  • determine disk usage by folders
  • list folders by their size
  • retrieve the size of directory, subdirectories and even the files contained in them.

du command has many options to generate different types of output such as list down the size of folders in blocks, in terms of units(such as KB, MG or GB), total size of the folder, excluding some specific files while calculating the size etc.
This article will explain various options that are available with du command along with an example for each option.
Simple usage example of du
du command when executed with only the path of the directory as the parameter lists down the size in blocks of all the sub-folders along with their paths. Total size of the argument folder is displayed at last. It does not display the details of files. Example,

user@server$ du /home/user/codippa
8           /home/user/codippa/legal
12         /home/user/codippa/articles
8           /home/user/codippa/tutorials/angular
12         /home/user/codippa/tutorials
57124   /home/user/codippa/

If du is not followed by any argument, then it lists down the size of all files and sub-folders of the current directory and all paths are preceded with ./

user@server$ cd /home/user/codippa
user@server$ du
8         ./legal
12       ./articles
8         ./tutorials/angular
12       ./tutorials
57124 .

You can also provide only the name of the folder for which you want to calculate the size but you must be in its parent folder. Example,

user@server$ cd /home/user
user@server$ du codippa
8          codippa/legal
12        codippa/articles
8          codippa/tutorials/angular
12        codippa/tutorials
57124  codippa

du command options
Following are the important options that you can use with du command.
1. -h : h stands for Human Readable Format. This option provides the size in the units that can be easily understood such as 2K, 40M or 2.5G where K stands for Kilobyte, M stands for Megabyte and G stands for Gigabyte. Rest output remains the same. Example,

user@server$ du -h /home/user/codippa
8.0K   /home/user/codippa/legal
12K    /home/user/codippa/articles
8.0K   /home/user/codippa/tutorials/angular
12K    /home/user/codippa/tutorials
56M   /home/user/codippa/

-h flag can be used with other flags listed below to display the sizes in easily understandable units.
2. -s: s stands for summary and this option provides the total size of the directory which is provided as argument to du command. It does not print the size of sub-directories.

user@server$ du -s /home/user/codippa
57124 /home/user/codippa

You can also use --summarize instead of -s.

Above command when combined with -h option, produces following output

user@server$ du -sh /home/user/codippa
56M /home/user/codippa

3. -a: a stands for all. This option includes the size of files also besides the size of all folders. Size displayed will be in blocks. Example,

user@server$ du -a /home/user/codippa
4          /home/user/codippa/legal/policy.xml
8          /home/user/codippa/legal
4          /home/user/codippa/articles/python.txt
4          /home/user/codippa/articles/checking disk size.txt
12        /home/user/codippa/articles
4          /home/user/codippa/tutorials/angular/router.txt
8          /home/user/codippa/tutorials/angular
12        /home/user/codippa/tutorials
57124  /home/user/codippa/

This option when used with -h prints the size in units.
4. -c: This option displays the total size of directory at the last line of command output with the caption “total”. Example,

user@server$ du -ch /home/user/codippa
8.0K   /home/user/codippa/legal
12K    /home/user/codippa/articles
8.0K   /home/user/codippa/tutorials/angular
12K    /home/user/codippa/tutorials
56M   /home/user/codippa/
56M   total

5. – -time: With this option, the files and directories are listed in the order of their modification time. Example,

user@server$ du -h – -time /home/user/codippa
8.0K   2019-07-25 12:30    /home/user/codippa/legal
12K    2019-07-25 12:30    /home/user/codippa/articles
8.0K   2019-07-25 12:30    /home/user/codippa/tutorials/angular
12K    2019-07-25 12:30    /home/user/codippa/tutorials
56M   2019-07-25 12:30    /home/user/codippa/

6. – -exclude: This flag is used to exclude a particular kind of files from the list. Syntax of using this flag is --exclude=Pattern
Example, below command will exclude all files with .xml extension from the list.

user@server$ du -a – -exclude=”*.xml” /home/user/codippa
8          /home/user/codippa/legal
4          /home/user/codippa/articles/python.txt
4          /home/user/codippa/articles/checking disk size.txt
12        /home/user/codippa/articles
4          /home/user/codippa/tutorials/angular/router.txt
8          /home/user/codippa/tutorials/angular
12        /home/user/codippa/tutorials
57124  /home/user/codippa/

There are other options such as

  • -k  : Displays the size in Kilobytes
  • -m : Displays the size in Megabytes
  • – -block-size : Display the size of files and folders in terms of block size. Example, if - -block-size=1k will display the size in block of 1k and - -block-size=2k will divide the size by 2 and so on.

To check all options, type du - -help on linux terminal.
Sort du output by size of folders
It is also possible to sort the output of du in the order of the folder size. Supply the output of du to sort command and it will do the rest. Example,

user@server$ du -h /home/user/codippa | sort -h
8.0K /home/user/codippa/legal
8.0K /home/user/codippa/tutorials/angular
12K /home/user/codippa/articles
12K /home/user/codippa/tutorials
56M /home/user/codippa/

As clear from the output, the list of folders is in increasing order of their sizes. -h option of sort command is used for comparison of sizes in human readable format.
To sort the list in reverse order, that is, folder with greatest size at the top and lowest size at bottom, add -r flag to sort. Example,

user@server$ du -h /home/user/codippa | sort -hr
56M     /home/user/codippa/
12K     /home/user/codippa/tutorials
12K     /home/user/codippa/articles
8.0K    /home/user/codippa/tutorials/angular
8.0K    /home/user/codippa/legal

Displaying folders till particular depth
It might happen that the level of nesting in the source folder is very deep and you do not want to go to the lowest level. There is an option to restrict the output of du to a particular level of nesting also.
Use --max-depth option with a numeric value which is the number of levels to which you want the folders to be listed. Example,

user@server$ du – -max-depth=1 /home/user/codippa
8      /home/user/codippa/legal
12    /home/user/codippa/articles
12    /home/user/codippa/tutorials
36    /home/user/codippa/

As you can see only first level folders below the source folder are listed. Let’s check with --max-depth of 2.

user@server$ du – -max-depth=2 /home/user/codippa
8      /home/user/codippa/legal
12    /home/user/codippa/articles
8      /home/user/codippa/tutorials/angular
12    /home/user/codippa/tutorials
36    /home/user/codippa/

Output is self-explanatory.
That is all on the du command tutorial in linux to list the size of files and folders in linux. Hit the clap below if you liked it.


Leave a Reply

Mark Your Impression

Notify of
Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.