回到手册索引

命令用途

du(disk usage)命令用于统计文件和目录所占用的磁盘空间,帮助用户快速了解文件系统的空间分布情况。

常用用法示例

  1. 显示当前目录各子目录的磁盘使用情况(人性化显示)

    1
    2
    3
    4
    du -h
    4.0K ./subdir1
    8.0K ./subdir2
    12K .

    以易读的格式(如K、M、G)显示当前目录下每个子目录或文件所占的空间。

  2. 统计指定目录的总磁盘使用量

    1
    2
    du -sh /home/user
    1.2G /home/user

    说明:使用 -s 选项汇总显示指定目录的总空间,并用 -h 选项以人性化格式展示。

  3. 显示指定目录及其所有子文件和子目录的详细空间占用

    1
    2
    3
    4
    du -ah /var/log
    4.0K /var/log/alternatives.log
    12K /var/log/syslog
    16K /var/log

    说明-a 选项使得命令不仅显示目录,还显示所有文件的磁盘占用情况。

  4. 显示当前目录下各子目录的空间占用(限制层级为1)

    1
    2
    3
    4
    du --max-depth=1
    8 ./folder1
    4 ./folder2
    12 .

    说明--max-depth=1 限制递归显示的目录层级,便于快速查看当前目录下各子目录的总占用。

  5. 显示当前目录及所有文件的空间占用

    1
    2
    3
    4
    5
    du -a
    4 ./file1.txt
    4 ./subdir/file2.log
    8 ./subdir
    12 .

    说明-a 选项递归列出所有文件和目录的磁盘使用情况,适合详细分析每个文件的占用。

  6. 跨文件系统时,限制只统计当前文件系统内的使用情况

    1
    2
    3
    4
    du -xh /
    4.0K /boot
    1.5G /home
    2.0G /

    说明-x 选项使 du 仅统计与给定起始目录在同一文件系统中的内容,避免跨挂载点统计。

  7. 结合 sort 命令,对磁盘使用量进行排序

    1
    2
    3
    4
    du -h | sort -h
    4.0K ./a
    8.0K ./b
    12K .

    说明:通过管道将 du 命令的输出传递给 sort 命令,按数值大小排序,方便查找大文件或大目录。

  8. 显示目录的磁盘使用情况并附带最后修改时间

    1
    2
    3
    4
    du --time -h /var
    4.0K 2025-04-10 12:34 /var/cache
    8.0K 2025-04-10 12:35 /var/log
    12K 2025-04-10 12:36 /var

    说明--time 选项在显示磁盘使用量的同时,附带显示每个文件或目录最后修改的时间信息。

常用参数选项

  • -a, –all
    列出所有文件和目录的磁盘使用情况,而不仅仅是目录。适合详细分析各个文件的占用情况。

  • -h, –human-readable
    以易读的格式显示磁盘使用量(例如K、M、G),方便快速理解数据大小。

  • -s, –summarize
    仅显示指定目录的总占用量,而不显示子目录的详细信息,适合快速查看整体空间使用情况。

  • –max-depth=N
    限制目录递归显示的深度,其中 N 为数字。例如 --max-depth=1 表示只显示当前目录下各子目录的总占用情况。

  • -c, –total
    除了显示各个目录的空间占用外,还在最后汇总显示一个总计数,方便查看整个统计的总量。

  • -x, –one-file-system
    在统计磁盘使用时,不跨越不同的文件系统,确保只统计起始目录所在的文件系统内容,防止误统计挂载点的空间。

  • -L, –dereference
    跟踪符号链接所指向的实际文件或目录,而不是将符号链接本身作为统计对象,适用于需要准确计算实际占用空间的情况。

  • –time
    显示每个文件或目录的最后修改时间,方便了解文件最近的更新时间与空间使用的关系。

原厂文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Name
du - estimate file space usage
Synopsis
du [OPTION]... [FILE]...
du [OPTION]... --files0-from=F
Description

Summarize disk usage of each FILE, recursively for directories.

Mandatory arguments to long options are mandatory for short options too.

-a, --all
write counts for all files, not just directories
--apparent-size
print apparent sizes, rather than disk usage; although the apparent size is usually smaller, it may be larger due to holes in ('sparse') files, internal fragmentation, indirect blocks, and the like
-B, --block-size=SIZE
use SIZE-byte blocks
-b, --bytes
equivalent to '--apparent-size --block-size=1'
-c, --total
produce a grand total
-D, --dereference-args
dereference only symlinks that are listed on the command line
--files0-from=F
summarize disk usage of the NUL-terminated file names specified in file F; If F is - then read names from standard input
-H
equivalent to --dereference-args (-D)
-h, --human-readable
print sizes in human readable format (e.g., 1K 234M 2G)
--si
like -h, but use powers of 1000 not 1024
-k
like --block-size=1K
-l, --count-links
count sizes many times if hard linked
-m
like --block-size=1M
-L, --dereference
dereference all symbolic links
-P, --no-dereference
don't follow any symbolic links (this is the default)
-0, --null
end each output line with 0 byte rather than newline
-S, --separate-dirs
do not include size of subdirectories
-s, --summarize
display only a total for each argument
-x, --one-file-system
skip directories on different file systems
-X, --exclude-from=FILE
exclude files that match any pattern in FILE
--exclude=PATTERN
exclude files that match PATTERN
--max-depth=N
print the total for a directory (or file, with --all) only if it is N or fewer levels below the command line argument; --max-depth=0 is the same as --summarize
--time
show time of the last modification of any file in the directory, or any of its subdirectories
--time=WORD
show time as WORD instead of modification time: atime, access, use, ctime or status
--time-style=STYLE
show times using style STYLE: full-iso, long-iso, iso, +FORMAT FORMAT is interpreted like 'date'
--help
display this help and exit
--version
output version information and exit

Display values are in units of the first available SIZE from --block-size, and the DU_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables. Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).

SIZE may be (or may be an integer optionally followed by) one of following: KB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.
Patterns
PATTERN is a shell pattern (not a regular expression). The pattern ? matches any one character, whereas * matches any string (composed of zero, one or multiple characters). For example, *.o will match any files whose names end in .o. Therefore, the command

du --exclude=aq*.oaq

will skip all files and subdirectories ending in .o (including the file .o itself).
Author
Written by Torbjorn Granlund, David MacKenzie, Paul Eggert, and Jim Meyering.
Reporting Bugs
Report du bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
Report du translation bugs to <http://translationproject.org/team/>
Copyright
Copyright © 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
See Also
The full documentation for du is maintained as a Texinfo manual. If the info and du programs are properly installed at your site, the command

info coreutils aqdu invocationaq

should give you access to the complete manual.