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
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
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
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
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
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
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
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 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
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