The du (disk usage) command in Linux is used to estimate and display the disk space used by files and directories. It helps in identifying the space usage of directories and their contents, allowing administrators to manage storage more effectively.
Basic Disk Usage
Display the disk usage of the current directory and its subdirectories:
du
Summarize Disk Usage
Show the total disk usage of a directory without listing every subdirectory:
du -s /path/to/directory
Human-Readable Format
Display disk usage in a human-readable format (e.g., KB, MB, GB):
du -h /path/to/directory
Summarize in Human-Readable Format
Combine summarizing and human-readable formatting:
du -sh /path/to/directory
Display Disk Usage for All Files and Directories
Show disk usage for all files and directories in the current location:
du -ah /path/to/directory
Limit Depth of Directory Tree
Limit the depth of the directory tree to display (e.g., only 1 level deep):
du -h --max-depth=1 /path/to/directory
Exclude Certain Files or Directories
Exclude specific files or directories from the disk usage calculation:
du -h --exclude="*.log" /path/to/directory
Sort by Disk Usage
Sort the output by disk usage (largest to smallest):
du -h /path/to/directory | sort -hr
Display Disk Usage of Specific File Types
Calculate disk usage for files of a specific type (e.g., .jpg
files):
du -ch *.jpg
Display Disk Usage in Bytes
Show disk usage in bytes (useful for scripting or precise measurements):
du -b /path/to/directory
Display Disk Usage of Mounted Filesystems
Show disk usage for all mounted filesystems:
du -sh /* 2>/dev/null
Ignore Permission Denied Errors
Suppress “Permission denied” errors when running du
:
du -sh /path/to/directory 2>/dev/null
Compare Disk Usage Over Time
Save disk usage to a file and compare it later:
du -sh /path/to/directory > disk_usage.txt
Display Disk Usage of a Specific User
Calculate disk usage for files owned by a specific user:
du -sh --apparent-size /home/username
Display Inode Usage
Show inode usage instead of disk space (useful for filesystems with many small files):
du --inodes /path/to/directory
Combine with find
for Advanced Filtering
Use find
with du
to calculate disk usage for files modified in the last 7 days:
find /path/to/directory -type f -mtime -7 -exec du -ch {} + | grep total$
Display Disk Usage of Symbolic Links
Include symbolic links in the disk usage calculation:
du -Lh /path/to/directory
Display Disk Usage in Kilobytes
Show disk usage in kilobytes (KB):
du -k /path/to/directory
Display Disk Usage in Megabytes
Show disk usage in megabytes (MB):
du -m /path/to/directory
Display Disk Usage of Hidden Files
Include hidden files and directories in the calculation:
du -sh .[!.]* *
Notes:
- The
du
command can be resource-intensive, especially when used on large filesystems or directories with many subdirectories and files. Limiting the depth or excluding certain files can improve performance. - Use the
-s
flag to avoid displaying disk usage for individual files when you only want a summary of a directory.