• 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/
    
    • 1
    • 2
    • 3
    • 4

    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/
    
    • 1
    • 2
    • 3
    • 4

    -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/
    
    • 1
    • 2

    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/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    –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/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5

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

    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汇总选项,这一个命令其实更像一张空头支票,实用性不大

  • 相关阅读:
    【iOS】导航栏的显隐与self.view.height
    库存管理方法有哪些?
    DataOps不是工具,而是帮助企业实现数据价值的最佳实践
    koa-apis
    电脑没有声音了怎么恢复?恢复声音的6个方法【图解】
    低代码常见场景【上】|如何解决业务问题
    第二章 数据结构(二)
    通过uc.js实现pc firefox页面向下滚动自动隐藏导航栏,复刻firefox移动版的丝滑体验
    Quartus 使用 tcl 文件快速配置管脚
    基于SSM实现的社区论坛系统(附PPT、设计文档)
  • 原文地址:https://blog.csdn.net/lyzchengxuyuan/article/details/127843390