Linux 命令:du 以及您应该使用的选项
该du
命令是标准的 Linux/Unix 命令,允许用户快速获取磁盘使用情况信息。它最适合应用于特定目录,并允许多种变体自定义输出以满足您的需求。
与大多数命令一样,用户可以利用许多选项或标志。此外,与许多 Linux 命令一样,大多数用户仅使用相同的两个或三个标志来满足其特定需求。这里的目的是介绍人们使用的基本标志,同时也介绍一些不太常见的标志,以期改善我们的使用du
。让我们首先查看独立命令,然后添加各种选项。
[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/
您可以看到基本命令给出了三行输出。最左边的值是磁盘使用情况,后面是负责该使用情况的特定目录。最下面一行是整个/home/tcarrigan/article_submissions
目录的摘要。没有指示标准命令使用的度量单位,因此此输出不太有用。这就是选项变得必要的地方。
[ 想了解更多 Linux 知识?免费在线课程:Red Hat Enterprise Linux 技术概述。]
-h , --人类可读
该-h
标志以人性化格式打印大小输出(如上图所示)。此格式提供度量单位(字节)。如果我们现在du -h
在同一目录上运行该命令,我们会看到 12、36 和 48 的值以 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,--总结
偶尔会将标志-s
添加到-h
标志中。结合它们的力量,它们不会成为环保的半神。相反,它们允许我们以人类可读的格式获取目录使用情况的摘要。
[tcarrigan@rhel article_submissions]$ du -sh /home/tcarrigan/article_submissions/
48K /home/tcarrigan/article_submissions/
如果该输出看起来很熟悉,那是因为它是输出最后一行的精确副本-h
。
-a、--全部
这个有用的选项的作用正如您所想。它列出了给定文件路径中所有文件和目录的大小。该-a
选项通常与标志结合-h
使用以方便使用。请注意,单个文件的大小与目录一起列出。
[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
-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
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, --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
.
[ Want to test your sysadmin skills? Take a skills assessment today. ]