Linux commands: du and the options you should be using

abstract :
The 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.

-h , --human-readable

The -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, --summarize

The -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 -h output.

-a, --all

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/

–time

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

-c, --total

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

-X, --exclude=Pattern

The -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 man du.

总结: du是用来查看文件内存使用的命令, -s 表示汇总总共使用了多少内存,与 -c类似区别是-c多了一个total汇总选项,这一个命令其实更像一张空头支票,实用性不大

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值