In linux, you can use du to view the disk usage of the file. you can view the du English description estimatefilespaceusage through mandu, or you can simply understand it as diskusage. The first letter of each word is du, this makes it easy to remember.
In linux, you can use du to view the disk usage of the file. you can use man du to view the description of du in English. estimate file space usage can also be simply understood as disk usage, the first letter of each word is du, which makes it easy to remember.
1. du command format
Du-hsaS file
The parameter meanings:
- -H: human readable. the capacity display is more friendly and will be automatically converted to G and M units.
- -S: summarize, used to list the total amount, not the sub-directory capacity
- -A: all indicates listing all files, not just directories.
- -S: separate dirs, which is used occasionally without counting the sub-directory capacity.
2. du Command Test
First create a test directory and use ls to view the files in the Test Directory:
$ ls -alh
Du Command Test Directory
Test is an empty directory with no files in it.
View the directory capacity usage
$ du -h4.0K ./test24K .
By default, du lists the directory and all subdirectories.
The dot (.) directory indicates the current directory, which occupies 24 K. how is this capacity calculated? Is the total capacity of files and sub-directories under the Directory, plus the space occupied by the Directory itself. The preceding example is calculated as follows:
. Bash_history +. bash_logout +. bashrc +. profile + test Directory + point (.) directory itself = 4 + 4 + 4 + 4 + 4 + 4 = 24 K
Why is the. bash_history file 4 K different from the capacity listed in ls above? This involves the knowledge of the file system. The blocksize here is 4 K. even if the file content is not 4 K, it will actually occupy a block, so it is 4 K. For more information, see blocksize of the file system.
Only view the total number of directories, not list subdirectories
$ du -hs24K .
This will only list the capacity of the specified directory.
Only view the total number of files under the Directory, excluding the sub-directory capacity
$ du -hS4.0K ./test20K .
The dot (.) directory displays 20 K, because the subdirectory (./test) is excluded)
Lists the capacity of all files, including files and subdirectories.
$ du -ah4.0K ./test4.0K ./.bash_history4.0K ./.bashrc4.0K ./.bash_logout4.0K ./.profile24K .
View the capacity of a single file
$ du -h .profile4.0K .profile
This is not commonly used because the ls command can also be used to view the file capacity usage.
Summary
In general, I usually use du-hs, which is very convenient to view the total directory capacity. For more information, see man du.