← 返回命令列表

Linux command

du 命令

文件

复制后可按需替换文件名、目录或参数。

常用示例

Show total size of a directory

du -sh [directory]

Show sizes in human-readable format

du -h [directory]

Show sizes of subdirectories

du -h --max-depth=1 [directory]

Show largest subdirectories first

du -h --max-depth=1 [directory] | sort -hr

Show size of all files and directories

du -ah [directory]

Stay on one filesystem

du -shx [directory]

说明

du (disk usage) estimates and reports file and directory space usage by recursively examining directory trees. Unlike df which shows filesystem-level free space, du focuses on individual files and directories, making it essential for identifying what's consuming disk space and where cleanup efforts should focus. The tool walks directory hierarchies and sums the disk space used by each file, reporting sizes at each directory level. By default, it shows sizes for all directories in the tree, but options like --max-depth allow limiting output to higher-level summaries. The -h flag converts raw block counts to human-readable formats (KB, MB, GB). du is commonly combined with sort to identify the largest space consumers. Patterns like "du -h | sort -hr | head" reveal the top disk space users, essential for troubleshooting full filesystems or planning cleanup operations. The command reports disk usage (actual blocks consumed) rather than apparent file size, which matters for sparse files and filesystems with compression. Hard links to the same inode are counted only once by default (use -l to count each link separately). The -x option prevents crossing filesystem boundaries, useful for analyzing specific filesystems without including mounted subdirectories.

参数

-h, --human-readable
Human-readable sizes (K, M, G)
-s, --summarize
Display total only
-c, --total
Produce grand total
-a, --all
Include files, not just directories
-d _N_, --max-depth=_N_
Maximum directory depth
-b, --bytes
Print sizes in bytes
-k, --kilobytes
Print sizes in kilobytes
-m, --megabytes
Print sizes in megabytes
-x, --one-file-system
Skip different filesystems
-l, --count-links
Count hard links multiple times
--apparent-size
Print apparent sizes rather than disk usage
--si
Like -h but use powers of 1000 (not 1024)
-L, --dereference
Follow symbolic links
--exclude=_pattern_
Exclude files matching pattern
--time
Show last modification time

FAQ

What is the du command used for?

du (disk usage) estimates and reports file and directory space usage by recursively examining directory trees. Unlike df which shows filesystem-level free space, du focuses on individual files and directories, making it essential for identifying what's consuming disk space and where cleanup efforts should focus. The tool walks directory hierarchies and sums the disk space used by each file, reporting sizes at each directory level. By default, it shows sizes for all directories in the tree, but options like --max-depth allow limiting output to higher-level summaries. The -h flag converts raw block counts to human-readable formats (KB, MB, GB). du is commonly combined with sort to identify the largest space consumers. Patterns like "du -h | sort -hr | head" reveal the top disk space users, essential for troubleshooting full filesystems or planning cleanup operations. The command reports disk usage (actual blocks consumed) rather than apparent file size, which matters for sparse files and filesystems with compression. Hard links to the same inode are counted only once by default (use -l to count each link separately). The -x option prevents crossing filesystem boundaries, useful for analyzing specific filesystems without including mounted subdirectories.

How do I run a basic du example?

Run `du -sh [directory]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -h, --human-readable do in du?

Human-readable sizes (K, M, G)