PrimitiveType

Checking the size of files and directories in Linux with du

The du (disk usage) command can be used to check the size of files and directories in Linux. The syntax for the command is:

du [-options] [file1 file2...]

To display the size of the file(s) in the most readable format for humans, use the "h" option:

$ du -h img1.jpg 24K img1.jpg

$ du -h img1.jpg img2.jpg 24K img1.jpg 24K img2.jpg

Sometimes it is useful to display a total of the file sizes, for which the "c" option can be used:

$ du -hc img1.jpg img2.jpg 24K img1.jpg 24K img2.jpg 48K total

The du command displays size information for each argument. If this includes directories, size information is also displayed for each subdirectory:

$ du -h dir1 28K dir1/subdir1 56K dir1

$ du -hc dir1 28K dir1/subdir1 56K dir1 56K total

If there are a lot of subdirectories, this command may display more information than you want. To have the command summarize the size of each directory (so that it doesn't list the subdirectories individually) use the "s" option:

$ du -hs dir1 56K dir1

$ du -hcs dir1 56K dir1 56K total

A simple way to display just the total without printing any of the arguments in the display is to pipe the output of du to grep and search for "total":

$ du -hc *.jpg | grep total 348K total

This isn't a fullproof method because some of your files may include the word "total" in their names, but at least you will always see the total as the last line of output regardless.