In this article, we will discuss another popular command for the Linux/Unix platform. It is called 'du', which stands for "Disk Usage". It is a standard command used to estimate space usage (meaning, in the terminal we can find the exact size each directory and file takes up). There are multiple ways we can generate various types of output in Terminal using the 'du' command with various options. It is usually used by all System Administrators to find unwanted files, files with unused large sizes, or archive files that can be deleted/cleared in order to provide sufficient free space for servers.
In addition to the du command, System Administrators will also use the df command to monitor the space occupied in the servers and storage boxes, to learn more information about the 'df' command, click here
Users without permission cannot execute the 'du' command on a specific file or directory. In those cases, you will need to use the 'sudo' command. Setting up sudo users? Click here to find the steps.
This guide will help you on how to use various options with du commands. All the below examples were 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: The 'du' command without any options will list all the files and folders in the given directory or the current working directory. Additionally, it will also be displayed in blocks along their paths, and at the bottom of the page it will display the total file size in blocks. As you can see in the above example, the file paths are shown along with the block sizes of existing files. The above output is not in a human-readable format, which is its main disadvantage.
2. How to check 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 the 'du -h' option will list all the outputs 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 printed either in " K-Kilobytes, M-Megabytes or G-Gigabytes ". The size of any files or directories can be measured most easily in this format.
3. How to check the total usage size of a particular directory?
# du -sh linuxteck
Output:
2.8G linuxteck
Note: Using the 'du -sh' option will display the exact usage size of the 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 the '-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 the '-a' option you will list and print the disk usage of every file including the directories and sub-directories. This command will help you identify the largest files/folders from the given path and 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 to 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 the '-B' flag combined with 'K', '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 their 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 the '-exclude' option with the '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 the '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 appearing in the recently modified data list.
I hope this article will help you to learn a few options with 'du' commands. Drop me your feedback/comments.
A few du references have been collected from the site
Thank you!
2 replies on “9 basic 'du' command in linux with practical examples”
Thanks!
This help me a lot!
Thank you, the detail was very nice.