du command is the standard Linux/Unix command which is used to estimate and monitor the disk usage of a particular file or directory. The results are reported in terms of number of data blocks occupied by each object. Generally, the usual block size used these days is 1024 bytes; one may override this if he wishes to. It is capable of reporting the sizes of only specific directories, entire directory tree or even each single file. There is no better option to determine which file is consuming how much space on a certain file system. du is usually run to have a high-level overview and after identifying the largest directories, using find command, largest files containing in them can be observed.
In this article, I will explain basic usage of du command which will help you monitoring and analyzing disk spaces consumed by files and directories in your Linux/Unix machine. You can refer manual page of du command any time as the information included in this article is based on the same.
Basic Usage: "du" Command
1. The simplest use of du command (or any command I should say) will be when it is run without any additional parameters. It will display the disk usage summary of your current working directory and its sub-directories. To see your current/present working directory, run the command pwd. You can also mention the path of the directory whose disk space usage is needed to be analyzed.
For example: du /home/mandar/demo
As mentioned in earlier part of the article, it will show the amount of disk blocks held by the target directory and its sub-directories.
2. Understanding the disk space usage in terms of disk blocks may not be comfortable for us. –h option comes to the rescue which provides human understandable version of the result i.e. disk usage in terms of Bytes, Kilo-bytes, Mega-bytes and so on.
Syntax: du –h <path_to_directory>
3. To observe the grand total of the disk space being utilized by the directory, use the du command with a –s option. It will directly print the net amount of disk space occupied and not the individual details for each sub-directory.
Syntax: du -s <path_to_directory>
4. Till now, we have seen use of du command for displaying disk space covered by each directory and sub-directory. In order to display disk space usage for each file and directory, use du command with –a option.
Syntax: du –a <path_to_directory>
5. Let us now combine two options in a du command. First is –h, using which we could get results printed in human readable format and the other option is –a, which could show details for all files and directories included. Enough said !
Syntax: du –ah <path_to_directory>
6. Repeating again, when du command is run with no option, it would display all the sizes in terms of blocks of data, which is usually 1024 bytes of size. Actually, you are observing all the sizes with a unit of Kilo-bytes, but to confirm that it is always the case, it is recommended that you run du command with –k option. It will always show all the results in terms of Kilo-bytes.
Syntax: du –k <path_to_directory>
7. Similarly, to get the sizes in the units of Bytes and Mega-bytes, run the du command with the options –b and –m respectively.
Syntax: du –b <path_to_directory> or du –m <path_to_directory>
8. Use of du command with –c option will produce two lines at the end of the results showing the grand total of disk space usage.
Syntax: du –c <path_to_directory>
9. Till now, we have observed du command providing disk space utilization for the specified directory. We can also get the results for the same directory excluding some type of files. --exclude option excludes the files that matches given pattern and displays the total size of the directory.
Syntax: du –ah --exclude=“<pattern>” <path_to_directory>
Example: du –ah --exclude =”*.pdf” /home/mandar/demo
10. du command when run with the option --time displays the disk utilization with the timing details of previous modification.
Syntax: du -ah --time <path_to_directory>
Thats it !