9 basic 'du' command in linux with practical examples

du command in linux

In this article, I am going to share another popular command in Linux/Unix platform is named 'du' which stands for "Disk Usage". It is a standard command used to estimate the file space usage (means, in the terminal we can find the exact amount of disk space used by directories and files). We can use 'du' command with various options in Terminal to generate various types of outputs. This 'du' command is mostly used by all the System Admins, to find out all the unwanted files, the unused large size of files or archived files that can be deleted/clear to make sufficient free space to Servers.

This guide will help you how to use various options with du commands. All the below examples are tested on RHEL/CENTOS 7.6

The Global Syntax of the du command:

du [OPTION]... [FILE]...
du [OPTION]... --files0-from=F

1. How to check the disk usage summary of a directory?

# du linuxteck

 

Output:

984              linuxteck/.svn/pristine/16
2936            linuxteck/.svn/pristine/f3
32948          linuxteck/.svn/pristine/7b
5676            linuxteck/.svn/pristine/a7
26604          linuxteck/.svn/pristine/80
2460            linuxteck/.svn/pristine/ca
2964            linuxteck/.svn/pristine/09
1429536      linuxteck/.svn/pristine
1439820      linuxteck/.svn
2920440      linuxteck

Note: Using 'du' command without any option will list out all the files and folders of the given directory or the current working directory. Also, it will display the file sizes in blocks along with their paths and finally, the total file size in blocks will be printed at the bottom of the line. In the above example, you can see the block size of files with their paths. The main drawback of the above output is not a human-readable format.

2. How to check the disk usage in a human-readable format?

# du -h linuxteck

 

Output:

984K                  linuxteck/.svn/pristine/16
2.9M                  linuxteck/.svn/pristine/f3
33M                   linuxteck/.svn/pristine/7b
5.6M                  linuxteck/.svn/pristine/a7
26M                   linuxteck/.svn/pristine/80
2.5M                  linuxteck/.svn/pristine/ca
2.9M                  linuxteck/.svn/pristine/09
1.4G                   linuxteck/.svn/pristine
1.4G                   linuxteck/.svn
2.8G                   linuxteck

Note: Using 'du -h' option will list all the output in "Human Readable Format". This '-h' option will convert block size into a human-readable format such as Bytes, Kilobytes, Megabytes or Gigabytes. In the above example, you can see the output of all the file sizes is printed either in " K-Kilobytes, M-Megabytes or G-Gigabytes ". This format can be measured the size of any files or directories most easily.

3. How to check the total usage size of a particular directory?

# du -sh linuxteck

 

Output:

2.8G linuxteck

Note: Using 'du -sh' option will display the exact usage size of a directory. The '-s' flag will display the total of a directory with block size but the combination of '-h' flag will convert the output into a human-readable format. You can see the above example of the total usage of "linuxteck" folder. In real time '-sh' combination is most widely used with du command.

4. How to list the disk usage of all files including directories?

# du -a linuxteck

 

Partical Output:

12                  linuxteck/.svn/pristine/09/09f1e40157be7ffd978ca54e7b0008f4fa295d4d.svn-base
4                     linuxteck/.svn/pristine/09/092262196b9dedac2bfe0ecfcef1351fdf502d06.svn-base
980                linuxteck/.svn/pristine/09/09545e437a50988578e1204eab3cdd6db16bfb4b.svn-base
20                  linuxteck/.svn/pristine/09/09e0967731aece14f71707f6af7dff96f84ad17a.svn-base
2964              linuxteck/.svn/pristine/09
1429536        linuxteck/.svn/pristine
10268            linuxteck/.svn/wc.db
1439820        linuxteck/.svn
4                     linuxteck/wp-register.php
2920440        linuxteck

Note: Using '-a' option will list and print the disk usage of every file including the directories, sub-directories. This command will help you to identify the largest files/folders from the given path also help you to delete/clear the unused or largest files to make sufficient free space to Servers. In the above example, you can see the difference compared to the previous examples, here, it listed every file including the directories. If you add, '-h' flag along with the above command like 'du -ah', then all the output comes in a human-readable format.

5. How to print the grand total for a directory?

# du -ch linuxteck

 

Output:

3.1M                  linuxteck/.svn/pristine/c6
2.2M                  linuxteck/.svn/pristine/3c
2.0M                  linuxteck/.svn/pristine/77
3.2M                  linuxteck/.svn/pristine/81
2.5M                  linuxteck/.svn/pristine/f6
1.4G                   linuxteck/.svn/pristine
4.0K                   linuxteck/.svn/tmp
1.4G                   linuxteck/.svn
132K                  linuxteck/Search/srdb-tests
392K                  linuxteck/Search
2.8G                   linuxteck/
2.8G                   total

Note: Using the '-c' option will list a grand total usage disk space at the very bottom of the output. If you add, '-h' flag along with the above command like 'du -ch', then all the output comes in a human-readable format. In the above example, you can see the very last like "showing the grand total as (2.8G total) which is a new row added using with 'c' flag".

6. How to change the default block size output into Kilobytes, Megabytes or Gigabytes?

# du -BK linuxteck
# du -BM linuxteck
# du -BG linuxteck

 

Output:

 

Kilobytes:-

392K                 linuxteck/Search
2921440K        linuxteck/

 

Megabytes:-

1M                    linuxteck/Search
2853M             linuxteck/

 

Gigabytes:-

1G                     linuxteck/Search
3G                     linuxteck/

Note: Using '-B' flag combined with 'K' or 'M' or 'G' will get the total disk usage of files and directories into Kilobytes, Megabytes or Gigabytes. In the above example, you can see the difference of using '-BK, -BM and -BG'.

7. How to check the size of all the sub-directories in the current location?

# du -h --max-depth=1 linuxteck                     OR                                 # du -h -d1 linuxteck

 

Output:

19M                 linuxteck/components
20K                  linuxteck/bin
20K                  linuxteck/sendmail
17M                 linuxteck/libraries
3.5M                linuxteck/modules
385M               linuxteck/.svn
15M                 linuxteck/plugins
5.1M                linuxteck/PHPExcel
16M                 linuxteck/media
964M               linuxteck

Note: Use either one of the above commands to list the size of all the sub-directories from the current folder. Some distro's '--max-depth' may not support, those cases you can use '-d' flag to get the same result. From the above example, you can see it has listed only the sub-folders and size in a human-readable format.

8. How to exclude a particular type of file while calculating the disk size?

# du -h --exclude="*.php" linuxteck

 

Output:

88K              linuxteck/media/media/images/mime-icon-16
240K            linuxteck/media/media/images
332K            linuxteck/media/media/css
576K            linuxteck/media/media/js
1.2M            linuxteck/media/media
28K              linuxteck/media/contacts/images
32K              linuxteck/media/contacts
16M             linuxteck/media
912M           linuxteck

Note: Using '-exclude' option with 'du' command, we can remove some particular pattern (extensions like .php,.txt,.png, etc) while calculating the disk usage for all the files and directories. The above examples have removed all the ".php" files while calculating the total size of the directory named "linuxteck".

9. How to check the disk usage of the last modification time?

# du -ha --time log

 

Output:

208K              2017-12-01 19:33 log/anaconda/syslog
3.5M              2017-12-01 19:33 log/anaconda
104K              2019-07-31 09:46 log/tuned/tuned.log.2
104K              2019-09-12 10:21 log/tuned/tuned.log.1
12K                2019-09-16 10:09 log/tuned/tuned.log
224K              2019-09-16 10:09 log/tuned
52K                2019-09-13 09:34 log/dmesg.old
4.0K               2019-09-16 10:16 log/linuxteck
54M               2019-09-16 10:16 log

Note: Using the'-time' option with 'du' command, it will list the last modified files and directories date and time. In the above example, I have created a directory named "linuxteck" under the log folder. In the output, you can see the "linuxteck" folder appeared in the recently modified data list.

Thank you for taking the time to read! I hope this article will help you to learn few options with 'du' commands. Drop me your feedback/comments.

Thank you!

If you like the article ? Please share it and help us grow 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *