Linux commands: du and the options you should be using
du command is a standard Linux/Unix command that allows a user to gain disk usage information quickly. It is best applied to specific directories and allows many variations for customizing the output to meet your needs.
As with most commands, the user can take advantage of many options or flags. Also, like many Linux commands, most users only use the same two or three flags to meet their specific set of needs. The aim here is to introduce the basic flags that people use, but also to look at some that are less common in hopes of improving our use of
du. Let's first look at the standalone command, and then add in various options.
[tcarrigan@rhel article_submissions]$ du /home/tcarrigan/article_submissions/ 12 /home/tcarrigan/article_submissions/my_articles 36 /home/tcarrigan/article_submissions/community_content 48 /home/tcarrigan/article_submissions/
You can see that there are three lines of output given by the basic command. The values on the far left are the disk usage, followed by the specific directory responsible for that usage. The bottom row is a summary of the entire
/home/tcarrigan/article_submissions directory. There is no indication as to what unit of measure is being used with the standard command, making this output less than useful. Here is where the options become necessary.
[ Want to learn more about Linux? Free online course: Red Hat Enterprise Linux technical overview. ]
-h , --human-readable
-h flag prints size outputs, such as the ones above, in a human-readable format. This format provides a unit of measure (Bytes). If we now run the
du -h command on the same directory, we see that the 12, 36, and 48 values are in KB.
[tcarrigan@rhel article_submissions]$ du -h /home/tcarrigan/article_submissions/ 12K /home/tcarrigan/article_submissions/my_articles 36K /home/tcarrigan/article_submissions/community_content 48K /home/tcarrigan/article_submissions/
-s flag is added to the
-h flag on occasion. With their powers combined, they do not become an eco-friendly demi-god. Instead, they allow us to get a summary of the directory's usage in a human-readable format.
[tcarrigan@rhel article_submissions]$ du -sh /home/tcarrigan/article_submissions/ 48K /home/tcarrigan/article_submissions/
If that output seems familiar, its because its an exact copy of the last line of the
This helpful option does exactly what you would think. It lists the sizes of all files and directories in the given file path. The
-a option is often combined with the
-h flag for ease of use. Notice the individual file sizes are listed with the directories.
[tcarrigan@rhel article_submissions]$ du -ah /home/tcarrigan/article_submissions/ 8.0K /home/tcarrigan/article_submissions/my_articles/Creating_physical_volumes 4.0K /home/tcarrigan/article_submissions/my_articles/Creating_volume_groups 12K /home/tcarrigan/article_submissions/my_articles 4.0K /home/tcarrigan/article_submissions/community_content/article 4.0K /home/tcarrigan/article_submissions/community_content/article2 4.0K /home/tcarrigan/article_submissions/community_content/article3 4.0K /home/tcarrigan/article_submissions/community_content/article4 12K /home/tcarrigan/article_submissions/community_content/real_sysadmins 8.0K /home/tcarrigan/article_submissions/community_content/podman_pulling 36K /home/tcarrigan/article_submissions/community_content 48K /home/tcarrigan/article_submissions/
I especially love this flag. It shows the time of the last modification to any file in the directory or subdirectory that you run it against. This flag was incredibly useful to me as a storage admin. On more than one occasion, I would have a customer write files to a subdirectory on accident, and then we needed to find where the write took place. I could use this flag in conjunction with the
-ah flags to find the directory last modified.
[tcarrigan@rhel article_submissions]$ du -ah --time /home/tcarrigan/article_submissions/ 8.0K 2020-04-07 11:30 /home/tcarrigan/article_submissions/my_articles/Creating_physical_volumes 4.0K 2020-04-07 11:31 /home/tcarrigan/article_submissions/my_articles/Creating_volume_groups 12K 2020-04-07 11:31 /home/tcarrigan/article_submissions/my_articles 4.0K 2020-02-06 16:47 /home/tcarrigan/article_submissions/community_content/article 4.0K 2020-02-06 16:48 /home/tcarrigan/article_submissions/community_content/article2 4.0K 2020-02-06 16:48 /home/tcarrigan/article_submissions/community_content/article3 4.0K 2020-02-06 16:48 /home/tcarrigan/article_submissions/community_content/article4 12K 2020-04-07 11:32 /home/tcarrigan/article_submissions/community_content/real_sysadmins 8.0K 2020-04-07 11:32 /home/tcarrigan/article_submissions/community_content/podman_pulling 36K 2020-04-07 11:32 /home/tcarrigan/article_submissions/community_content 48K 2020-04-07 11:32 /home/tcarrigan/article_submissions/
Note: This does not sort by last modification so you still need to pay attention to the times. The last modification is not always at the top
This option is more of a dummy check than it is useful, however, some people really like having a total measurement output. The
-c flag adds a line to the bottom of the output that gives you a grand total of all of the disk usage for the file path given.
[tcarrigan@rhel article_submissions]$ du -ch /home/tcarrigan/article_submissions/ 12K /home/tcarrigan/article_submissions/my_articles 36K /home/tcarrigan/article_submissions/community_content 48K /home/tcarrigan/article_submissions/ 48K total
Notice the bottom line here. The same information is displayed that is shown in the other examples of
du but without the 'total' banner to remind you.
-X option is a nifty little trick you can do if you know that your environment has a large number of a certain type of file that you do not wish to calculate in your findings. In my experience, certain customers would have large amounts of metadata files with the same file extension and did not wish to include those in their findings. I cannot demonstrate this here on my virtual machine; however, here is the syntax and an example.
[tcarrigan@rhel]$ du -ah --exclude="*.dll" /home/tcarrigan/article_submissions
This command would list all files and directory usage info in a human-readable format while excluding any file with the extension .dll. This is a bit niche, however, it does have a place in the world.
Wrap up and man page
Hopefully, you now have a better understanding how useful the
du utility can be. It is easy to get into the routine of only ever running
du -h and forgetting about all of the other incredibly powerful flags you have at your disposal. There are many flags that I did not cover in this article, but you can find all the information on the manual page for this command. To access the manpage, simply run
[ Want to test your sysadmin skills? Take a skills assessment today. ]