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. Similarly, the 'df' command also used on day-today activities by all System Admin. To know more about 'df' commands click here
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
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
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
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
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
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
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
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
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
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.
I hope this article will help you to learn few options with 'du' commands. Drop me your feedback/comments.