【Linux】命令 - du

Posted by 西维蜀黍 on 2021-10-20, Last Modified on 2023-02-16

du Command

du command, short for disk usage, is used to estimate file space usage.

The du command can be used to track the files and directories which are consuming excessive amount of space on hard disk drive.

Parameters

-h - human readable format(K, M, G)

$ du -h /home/mandeep/test 

Output:
44K    /home/mandeep/test/data
2.0M    /home/mandeep/test/system design
24K    /home/mandeep/test/table/sample_table/tree
28K    /home/mandeep/test/table/sample_table
32K    /home/mandeep/test/table
98M    /home/mandeep/test

-a - printing all files in directories

$ tree
.
|-- 10g_file.img
`-- folder1
    `-- aa

1 directory, 2 files

$ du -h -a
4.0K	./folder1/aa
8.0K	./folder1
11G	./10g_file.img
11G	.

# otherwise display the folder's names with occupasion space
$ du -h
8.0K	./folder1
11G	.

-c - print the total size of the target path

$ du -hc
8.0K	./folder1
11G	.
11G	total

$ du -h
8.0K	./folder1
11G	.

-d <max lavel> - max level

$ du -h -d 0 .
11G	.
$ du -h -d 1 .
8.0K	./folder1
11G	.

--time - print the timestamp of last modified

$ du -h --time
8.0K	2021-10-20 00:16	./folder1
11G	2021-10-20 00:16	.

-s - summary

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.

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

Reference