1. Du Command Basics
du
The Disk Usage command is a utility in Linux/Unix systems for estimating file and directory disk usage. It recursively traverses the directory structure, computes the disk space occupied by each file and subdirectories, and displays the results in blocks (by default).
Basic syntax
du [Options] [File or directory]
If no file or directory is specified,du
The disk usage of the current directory and all subdirectories will be counted by default.
Why do I need the -h option?
Originaldu
The command output is in units of disk blocks (usually 1 block = 512 bytes or 1KB), which is not human reading-friendly. For example:
du data
Possible output:
123456 data/subdir1 789012 data/subdir2 912468 data
Such numbers are difficult to understand intuitively.-h
The emergence of the (human-readable) option solves this problem, which automatically selects the most appropriate unit (KB, MB, GB, or TB) to display the size:
du -h data
The output becomes:
4.0K data/subdir1 8.2M data/subdir2 1.2G data
2. Detailed explanation of du -h command
Output interpretation
du -h
The typical output contains multiple lines of information:
- Subdirectory line: Shows the size and path of each subdirectory
- Total rows(Last line): Shows the total size of the specified directory
For example:
4.0K data/subdir1 8.2M data/subdir2 1.2G data
here:
-
data/subdir1
Occupy 4.0KB -
data/subdir2
Occupy 8.2MB - The whole
data
Directory occupies 1.2GB
Common options combinations
-s
(Summary): Only the total size is displayed, not the subdirectory details
du -sh data
Output:
1.2G data
-c
(total): Add a total line at the end
du -hc data
Output:
4.0K data/subdir1 8.2M data/subdir2 1.2G data 1.2G Total dosage
--max-depth=N
: Control the display directory depth
du -h --max-depth=1 data
Only display the size of the first-level subdirectories under data
3. Comparison between du and other commands
du vs df
-
du
(Disk Usage): Calculate the space occupied by files and directories from the file system perspective -
df
(Disk Free): Displays the overall disk usage of the file system
du
More suitable for finding the space occupied by specific directories or files, anddf
It is more suitable for viewing the usage of the entire disk or partition.
du vs ls -l
-
ls -l
What is displayed is the actual size of the file (logical size) -
du
It shows the disk space occupied by the file (may be larger than the actual size due to block allocation)
For example, a 1-byte file:
-
ls -l
Show 1 byte -
du
May show 4KB (depending on the block size of the file system)
4. Practical application scenarios
1. Find the large directory
du -h / | sort -rh | head -n 20
This command combination:
- Calculate all directory sizes starting from the root directory
- Output in human-readable format
- Sort in reverse order by size
- Show the top 20 largest directories
2. Monitor user disk usage
du -sh /home/*
Quickly view disk usage for all user home directories.
3. Exclude specific directories
du -h --exclude='*.log' /var
Statistics/var
Directory size, but exclude all.log
document.
4. Compare directory changes
du -sh data # After doing some operationsdu -sh data
Compare the changes in directory size by performing twice.
5. Advanced skills and precautions
1. Processing symbolic links
By default,du
It will count the file size pointed to by the symbolic link. use-L
Options can follow symbolic links:
du -Lh /path
2. Display the modification time
Combined--time
Options can display the last modification time:
du -h --time data
3. Performance optimization
For large file systems,du
It may take a long time. Can:
- use
--apparent-size
Show apparent size rather than disk usage (faster but not accurate enough) - Limit directory depth
--max-depth
- Running during off-peak hours
4. Cross-file system statistics
defaultdu
It will count other file systems under the mount point. use-x
Can be restricted to the current file system:
du -xh /
6. FAQs
Q1: Whydu
anddf
Is the total space displayed inconsistent?
A1: Possible reasons include:
- Files that have been deleted but still have process open
- File system reserved space
- Different statistical methods (
df
Count the entire file system,du
Statistics specific documents)
Q2: How to count the number of files in a directory instead of size?
A2: Usefind
andwc
:
find data -type f | wc -l
Q3:du
Why is the displayed size larger than the actual file sum?
A3:du
The disk usage is counted, including:
- The actual content of the file
- File system metadata
- Additional space due to block allocation
This is the end of this article about the detailed explanation of common commands for viewing Linux directory size. For more related contents for viewing Linux directory size, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!