Home  >  Article  >  Operation and Maintenance  >  what is linux du

what is linux du

青灯夜游
青灯夜游Original
2022-04-18 19:18:207640browse

In Linux, the full name of du is "Disk Usage". It is a command to count the disk space occupied by a directory or file. The syntax is "du [option] [directory or file name]". The du command supports a variety of options: 1. "-h", which can display the size in easy-to-read units; 2. "-s", which can display the total size of the directory; 3. "-d", etc.

what is linux du

#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

linux du command

du is the abbreviation of Disk Usage, one of the most popular commands on Linux, du is the statistical directory or file Commands that occupy disk space

du The format of the command is as follows:

du [选项] [目录或文件名]

Commonly used options are as follows:

  • - a: Display the size of all files and folders in the directory

  • -h: Display the size in easy-to-read units such as Kb, Mb, Gb, etc.

  • --si: Similar to the -h option, but the calculation is based on 1000 instead of 1024

  • -s: Display the total size of the directory

  • -d: is the abbreviation of the --max-depth=N option, indicating which level of directories to go deep into. Directories exceeding the specified number of levels will be ignored

  • -c: In addition to displaying the directory size In addition, an extra line displays the total usage

  • --time: Displays the time of the most recently modified files in each directory

  • -t: Yes --threshold=Abbreviation for SIZE, filters out files and directories smaller than SIZE

  • --exclude=PATTERN: Filters out file names or directory names that match PATTERN

Usage example

Display all directories and file sizes

The following example shows The size of all directories and files under the directory, the default unit is Kb

[root@ecs-centos-7 tt]# du -a temp/
4       temp/suba.txt
4       temp/test/abc.txt
4       temp/test/ha/ha.txt
8       temp/test/ha
16      temp/test
4       temp/time.txt
28      temp/

Note: If the above example does not use the -a option, by default only the directory size will be displayed, not the file size. That is, executing du temp/ will only display the directory size. Please see the following example:

[root@ecs-centos-7 tt]# du temp
8       temp/test/ha
16      temp/test
28      temp

Displayed in an easy-to-read manner

Default display The size only has a single number, not even a unit, which makes people look a little confused at first glance. The -h option can be used to display the size in a human-readable way. This option should be the most commonly used

[root@ecs-centos-7 tt]# du -b temp/
4117    temp/test/ha
8218    temp/test
12326   temp/
[root@ecs-centos-7 tt]# du -h temp/
8.0K    temp/test/ha
16K     temp/test
28K     temp/
[root@ecs-centos-7 tt]# du --si temp/
8.2k    temp/test/ha
17k     temp/test
29k     temp/

In the above example, the default calculation base of the -h option is 1024, and the default calculation base of the --si option is 1000
So temp/test/haThe size of the directory calculated with the -h option is 8.0K, and the size calculated with the --si option is 8.2K

-h and the size unit of the --si option are automatically adjusted with the size of the directory and file

Total directory size

Sometimes we only need to know the total size of a directory and do not need to know the size of subdirectories and files in subdirectories. We can obtain the total directory size through the -s option

[root@ecs-centos-7 tt]# du -sh .
72K     .
[root@ecs-centos-7 tt]# du -sh temp/
28K     temp/

The above example obtains the total size of the current directory and the total size of the temp/ directory respectively

The total size of the directory can also be obtained through the -c option, but it displays the subdirectory size first, and the last line displays the total size. The last line of the example below total The 28K in front of the string indicates the total size of the temp/ directory

[root@ecs-centos-7 tt]# du -ch temp/
8.0K    temp/test/ha
16K     temp/test
28K     temp/
28K     total

Specify the directory depth

If there are many in one directory Subdirectory, if you only want to display the size of the directory with a specified number of levels, you can use the -d option to achieve the subdirectory structure of

temp/ as follows:

[root@ecs-centos-7 tt]# tree -d temp/
temp/
└── test
    └── ha

2 directories

Specify the directory depth

[root@ecs-centos-7 tt]# du -d 0 temp/
28      temp/
[root@ecs-centos-7 tt]# du -d 1 temp/
16      temp/test
28      temp/
[root@ecs-centos-7 tt]# du --max-depth=2 temp/
8       temp/test/ha
16      temp/test
28      temp/

du -d 0 temp/: Displays the 0th level directory, which is the total size of the current directory. This is equivalent to the -s option

du -d 1 temp/: Display the total size of the first-level directory, that is, the temp/test directory

du --max-depth=2 temp/: Display the second-level directory, that is, temp/ The total size of the test/ha directory

Display the latest modification time

[root@ecs-centos-7 tt]# du --time temp
8       2020-07-21 20:11        temp/test/ha
16      2020-07-21 20:11        temp/test
28      2020-07-21 20:13        temp

The above example displays the latest modification time of each directory, and the time granularity is only accurate to the minute

If you want to display a finer granularity, you can use the --time-syle=STYLE option to specify the output format of the time, where STYLE represents the formatted output string of the date, and date The format of the formatted output of the command is the same

Example 1: Display the number of seconds in UTC time (the number of seconds from January 1, 1970 to the present)

[root@ecs-centos-7 tt]# du --time --time-style="+%s" temp/      
8       1595333498      temp/test/ha
16      1595333514      temp/test
28      1595333582      temp/

Example 2: Display the complete Year, month, day, hour, minute and second

[root@ecs-centos-7 tt]# du --time --time-style="+%F %T" temp/  
8       2020-07-21 20:11:38     temp/test/ha
16      2020-07-21 20:11:54     temp/test
28      2020-07-21 20:13:02     temp/

Filter by size

From the displayed results, filter out the directories and files of the specified size

[root@ecs-centos-7 tt]# du -b temp/
4117    temp/test/ha
8218    temp/test
12326   temp/
[root@ecs-centos-7 tt]# du -b -t 4118 temp/
8218    temp/test
12326   temp/

above In the example, directories smaller than 4118 bytes are filtered out

Filter by directory name or file name

If there are too many subdirectories in one directory, we can filter based on the subdirectory name Or the file name matches the specified pattern string, thereby filtering out the matching directories and files

[root@ecs-centos-7 tt]# du -a temp
4       temp/suba.txt
4       temp/test/abc.txt
4       temp/test/ha/ha.txt
8       temp/test/ha
16      temp/test
4       temp/time.txt
28      temp
[root@ecs-centos-7 tt]# du -a --exclude=*a* temp/
4       temp/test
4       temp/time.txt
12      temp/

In the above example, the filtered pattern string is: *a*

It means to filter out directories or files whose directory names or file names contain the characters a. In the example, the directory or file names in the first four lines all contain the characters a, so they are all filtered. Lost

What are the files that fill up the disk

The problem that developers often encounter is that the disk is full. At this time, we can use du and sort in combination. Find the "culprit"

  • 当前目录下文件从大到小排序
[root@ecs-centos-7 tt]# du -sh temp/* | sort -hr
10M     temp/clpay.tar
16K     temp/test
4.0K    temp/time.txt
4.0K    temp/lnsuba
  • 当前目录以及子目录从大到小排序
[root@ecs-centos-7 tt]# du -ah temp/* | sort -hr
10M     temp/clpay.tar
16K     temp/test
8.0K    temp/test/ha
4.0K    temp/time.txt
4.0K    temp/test/ha/ha.txt
4.0K    temp/test/abc.txt
4.0K    temp/lnsuba
  • 磁盘占用最大的三个目录以及子目录
[root@ecs-centos-7 tt]# du -ah temp/* | sort -hr | head -n 3
10M     temp/clpay.tar
16K     temp/test
8.0K    temp/test/ha

相关推荐:《Linux视频教程

The above is the detailed content of what is linux du. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn