Linux Commands

How to use du command in Linux

The du command is used to manage the storage space in Linux, for example, to check how much space is used by the files and how much is free to store new files and to install new packages. With the help of the du command, we can find out the space occupied by the files and directories in Linux, and in this write-up, we will learn the use of the du command on Linux with the help of different examples.

How to use the du command in Linux

The du command is used to find out how much system memory is used by different directories or subdirectories. The general syntax of the du command in Linux is:

$ du [options] directory/subdirectory

There are different options that can be used with the du command for different purposes:

Options Purposes
h To display the units
a To display the disk space utilized by all the files available of the specified directory
s To display the total of each directory
c It combines all the sizes of the files and displays them separately in the section of the total
–apparent-size It displays the absolute size of the files in the directory
–max-depth It is used to specify the command to which folder go within the directory
–exclude It is used to exclude some specified files
–time It displays the time of the last modification made in the file

Before using these options, we will display the disk size used by the directories in the Home directory using the command:

$ du /home/hammad

In the output, the sizes are displayed but without the units, it is difficult for us to recognize whether these are in GBs, MBs, or in KBs, to display the sizes with units, we execute the above command using the “-h” option:

$ du -h /home/hammad

The sizes utilized by the files in the directory are understandable as the units are mentioned with them; to display all the files present in any directory or sub-directory, we can use the “-a” flag:

$ du -ha /home/hammad/Documents

In the above output, if we want to display the summary of the size of “new_directory” only we can replace the “-s” flag with the “-a” flag in the options:

$ du -hs /home/hammad/Documents/new_directory

To display the size of all files of the directory of Documents, and display their combined result under the title of “Total” we use the “-c” flag:

$ du -hc /home/hammad/Documents

If we need to display the apparent size of the files available in the directory, we will use the option of the “–apparent-size”:

$ du --apparent-size /home/hammad/Documents

Similarly, we can limit the terminal to go to the specified subfolders by using the option of “–max-depth”, for example, we specify “1” to go to the only first subfolders:

$ du --max-depth=1 /home/hammad

We can see from the output, the size has been calculated and displayed only for the first folder, now if we want to calculate and display up to the first two folders, run the command:

$ du --max-depth=2 /home/hammad

We can exclude files by using the option of “–exclude”. For example, we can exclude all the hidden files from the home directory, by using the “./.” for hidden files:

$ du -ha --exclude=./.*

The last option we are discussing in this write-up is the time option which is used to display the changes made in the last file:

$ du --time

Conclusion

The du command is very helpful in managing the disk space as it tells how much space has been occupied by the files and packages, so we can calculate how much space is left free in the disk for new files and packages. In this write-up, we have discussed the different ways of using the du command with its various options.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.