The Disk Usage (du) is one of the best command in Linux to check the directory size including its other contents as well as the size of individual files. This command is also very useful in checking the space hogs mean those files and directories that are consuming large amount of disk space on your hard disks or other storage mediums.
du command Syntax.
du [options] [file or directory name]
Here, the meaning of square bracket is they are the optional items. If you use the du command without any option or any argument then it will list names of files and directories and they space consumption for the given directory tree. For instance:
[root@LinuxServer Desktop]# du /etc
… [output truncated]
Here, the space occupied by any directory is the space of all the files and subdirectories of that directory. The last line of the output gives the total space occupied for that directory.
Different options that can be used with du command.
Same like other Linux or Unix commands, du also has different number of options and some of them are most commonly used. Even these options can also change little bit based on the operating system or its version.
[root@LinuxServer Desktop]# du -h /etc
… [output truncated]
-h is one of the good options used with du command. Here, -h means human readable. This means it makes our output easier to read. It will display the output in kilobyte (K), megabyte (M) and gigabyte (G). So, we can say above command is used to display the size of all directories and subdirectories and also total size of our current directory formatted with appropriate K, M or G.
[root@LinuxServer Desktop]# du -s /etc
This option summarizes only the total amount of total disk space occupied by that particular directory. This will also suppress the individual reports for all the files and subdirectories.
[root@LinuxServer Desktop]# du -a /etc | more
….. [output truncated]
The option –a is to report the total disk space usage for every directory at every level. Even this option is also useful to report, the consumption of space for each file in the directory tree. This means in the above example, it would list the names and size of each directory and files inside /etc directory.
[root@LinuxServer etc]# du -h s* | more
In this option, * means wildcard means it will try to match with any character. So, this options can be used to find the output of all those items whose names begin, ends or contain specific characters. In the above example, it will display the size of all the files and directories from the current directory whose names normally starts with S.
[root@LinuxServer etc]# du -hc s* | more
We can add –c option with our du command which generally provides a grand total of files and directories listed. In case of above example, it will list the grand total of all files listed in the current directory starting with the name s. Another example is as below where we are getting the size of each gif files available in current directory with its grand total.
du -hc *.gif
[root@LinuxServer etc]# du --max-depth=1
This option instructs du command to list the subdirectories and its size to our desired depth level. For example, the above example lists the directories to first tier only in the current direcotyr tree and their size. Even total consumption of space is also reported here. In case if we set the –max-depth= to zero, then du command will not list any subdirectories and it will only report the size of selected directory.
[root@LinuxServer Desktop]# du -h –exclude=’*.obj’
[root@LinuxServer Desktop]# du -h –exclude=’*.jpg’
Exclude files that match PATTERN. The above examples do not count *.obj or *.jpg files.
[root@LinuxServer Desktop]# du -sk .[A-z]* *| sort -n
Display everything sorted by size.
[root@LinuxServer Desktop]# du -sk * | sort -nr | head -3
To find top 3 directories by size.