Cheatsheet & Examples: du

Display disk usage of the current directory
Example Usage:du
What it does:
Shows the disk usage of the current directory, including all subdirectories and files, in blocks of 512 bytes.
Command-line Arguments Explained:
du
: The basic command to estimate file space usage. Without options, it recursively lists the size of directories and files.
Display a summary of disk usage for the current directory
Example Usage:du -s
What it does:
Provides a single summary line showing the total disk usage of the current directory, excluding its subdirectories.
Command-line Arguments Explained:
-s
: Summarize the total disk usage of the specified directory (or current directory if none is given).
Display human-readable disk usage
Example Usage:du -h
What it does:
Shows disk usage in a human-readable format (e.g., 4.0K, 8.0M instead of raw block counts).
Command-line Arguments Explained:
-h
: Use suffixes like K, M, G to represent sizes in kilobytes, megabytes, and gigabytes.
Display human-readable summary of disk usage for the current directory
Example Usage:du -sh
What it does:
Combines -s
(summary) and -h
(human-readable) to show the total size of the current directory in an easy-to-read format.
Command-line Arguments Explained:
-s
: Summarize the total size of the specified directory.-h
: Display sizes in a human-readable format (e.g., 4.0K, 2.0G).
Display disk usage for all files and subdirectories
Example Usage:du -a
What it does:
Lists the disk usage of every file and directory in the specified path, including nested files.
Command-line Arguments Explained:
-a
: Display the size of all files, not just directories.
Display disk usage for a specific directory
Example Usage:du /path/to/directory
What it does:
Shows the disk usage of the specified directory, including its subdirectories and files.
Command-line Arguments Explained:
/path/to/directory
: The directory or file to analyze. Replace with the actual path.
Display disk usage with a specified level of detail (depth)
Example Usage:du -d 2
What it does:
Limits the depth of the directory traversal to 2 levels, showing the size of the current directory and its immediate subdirectories.
Command-line Arguments Explained:
-d
: Set the maximum depth of directory traversal (e.g.,-d 2
for two levels).
Display disk usage in kilobytes
Example Usage:du -k
What it does:
Shows disk usage in kilobytes instead of the default 512-byte blocks.
Command-line Arguments Explained:
-k
: Display sizes in kilobytes (1024 bytes). Note: This option is deprecated in some moderndu
implementations (e.g., GNUdu
).
Display disk usage in megabytes
Example Usage:du -m
What it does:
Shows disk usage in megabytes (1024 kilobytes) instead of the default 512-byte blocks.
Command-line Arguments Explained:
-m
: Display sizes in megabytes. Deprecated in moderndu
versions.
Show disk usage and sort by size
Example Usage:du -sh * | sort -n
What it does:
Displays the sizes of all files and directories in the current directory, sorted numerically by size.
Command-line Arguments Explained:
-s
: Summarize the total size of each item.-h
: Display sizes in a human-readable format.sort -n
: Sorts the output numerically (smallest to largest).
Display apparent size instead of disk usage
Example Usage:du --apparent-size
What it does:
Shows the apparent size of files (e.g., the size of the file as it appears on disk, not the actual disk space used, which may be larger for sparse files or files with hard links).
Command-line Arguments Explained:
--apparent-size
: Display the apparent size of files rather than the actual disk space.
Exclude specific files or directories from the output
Example Usage:du --exclude='*.tmp'
What it does:
Ignores files or directories matching the specified pattern (e.g., .tmp
files) when calculating disk usage.
Command-line Arguments Explained:
--exclude='pattern'
: Exclude entries matching the given pattern from the summary. Replacepattern
with your desired filter.
Combine disk usage with a total summary
Example Usage:du -sc
What it does:
Shows a summary for each directory and adds a total at the end.
Command-line Arguments Explained:
-s
: Display a summary for the directory.-c
: Add a total summary line at the end of the output.
Show disk usage for a specific subdirectory
Example Usage:du -d 1 /path/to/directory
What it does:
Limits the output to the specified directory and its immediate subdirectories (depth of 1).
Command-line Arguments Explained:
-d
: Set the maximum depth of the directory traversal./path/to/directory
: The directory to analyze.
Display disk usage in bytes
Example Usage:du -b
What it does:
Shows disk usage in bytes instead of the default 512-byte blocks.
Command-line Arguments Explained:
-b
: Display the size in bytes.
Show disk usage of the current directory and its immediate subdirectories
Example Usage:du -s *
What it does:
Provides a summary of each file and subdirectory in the current directory, excluding nested subdirectories.
Command-line Arguments Explained:
-s
: Summarize each item in the list.*
: List all files and directories in the current directory.
Display disk usage with custom time format
Example Usage:du --time-style=+%Y-%m-%d
What it does:
Adds modification time to the output using the specified format.
Command-line Arguments Explained:
--time-style=format
: Define the format of modification time output (e.g.,+%Y-%m-%d
for YYYY-MM-DD).
Display disk usage of a specific file
Example Usage:du filename.txt
What it does:
Shows the disk usage of the specified file.
Command-line Arguments Explained:
filename.txt
: The file to analyze.
Display disk usage in a specific filesystem
Example Usage:du -x /path/to/directory
What it does:
Limits the output to the specified filesystem, avoiding cross-filesystem analysis.
Command-line Arguments Explained:
-x
: Skip directories on different filesystems.
Check disk usage for all files in the current directory
Example Usage:du -a .
What it does:
Lists the size of every individual file in the current directory (including hidden files).
Command-line Arguments Explained:
-a
: Display the size of all files, not just directories..
: Refers to the current directory.
Display disk usage with a custom unit
Example Usage:du -k /path/to/dir
What it does:
Shows disk usage in kilobytes, overriding the default block size.
Command-line Arguments Explained:
-k
: Display sizes in kilobytes (1024 bytes). Note: This option is deprecated in somedu
versions.
Display disk usage of a directory and its subdirectories, excluding certain files
Example Usage:du -s /path/to/dir --exclude='logs/*'
What it does:
Calculates the disk usage of a directory while excluding subdirectories or files that match the specified pattern (e.g., logs/
).
Command-line Arguments Explained:
-s
: Summarize the total size of the directory.--exclude=pattern
: Exclude files or directories matching the pattern.
Show disk usage in a tree-like format
Example Usage:du -h --tree
What it does:
Displays disk usage in a tree-like structure, making it easier to visualize directory hierarchies.
Command-line Arguments Explained:
-h
: Human-readable output.--tree
: Print disk usage in a tree format. Note: This option may not be available on all systems.
Display disk usage for multiple directories
Example Usage:du /dir1 /dir2
What it does:
Calculates disk usage for each specified directory and displays their sizes.
Command-line Arguments Explained:
/dir1 /dir2
: One or more directories to analyze. Replace with actual paths.
Subscribe to my newsletter
Read articles from Hong directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Hong
Hong
I am a developer from Malaysia. I work with PHP most of the time, recently I fell in love with Go. When I am not working, I will be ballroom dancing :-)