Linux Tutorial
Linux File/Directory Management
Linux Packaging And Compression
Vim Text Editor
Linux Text Processing
Linux Software Installation
Linux User/User Group Management
Linux Permission Management
Linux Filesystem Management
Linux Advanced Filesystem Management
Linux System Management
Linux Backup and Recovery
Linux System Service Management
Linux System Log Management
Linux Boot Management
LAMP/LNMP Environment
SELinux Management
The du
(short for "disk usage") command is a Linux utility that estimates the space used by files and directories. It is useful for tracking down large files or directories that are consuming excessive disk space. Here's a basic tutorial on how to use the du
command:
Display the disk usage of a directory:
To display the disk usage of a specific directory, run the du
command followed by the directory path:
du /path/to/directory
By default, the output will show the space used by each subdirectory in kilobytes.
Display the disk usage of multiple directories:
To display the disk usage for multiple directories, list the directory paths after the du
command:
du /path/to/dir1 /path/to/dir2
Display the disk usage in human-readable format:
Use the -h
option to display the output in a more human-readable format, with sizes expressed in bytes (B), kilobytes (K), megabytes (M), gigabytes (G), or terabytes (T):
du -h /path/to/directory
Display the total disk usage of a directory:
To display only the total disk usage for a directory, use the -s
(summarize) option:
du -sh /path/to/directory
Display disk usage for specific file types:
You can use the --include
option followed by a file extension or pattern to display disk usage for specific file types:
du -ah --include='*.txt' /path/to/directory
Exclude specific directories or file types:
Use the --exclude
option followed by a directory or file pattern to exclude certain items from the disk usage output:
du -h --exclude='*.log' /path/to/directory
Find directories larger than a specific size:
You can use the find
command in combination with the du
command to find directories larger than a specific size. For example, to find directories larger than 100MB:
find /path/to/directory -type d -exec du -sh {} \; | awk '$1 ~ /[0-9]*M/ && $1 > 100'
These are some basic examples of using the du
command. By understanding how to estimate disk usage, display results in a human-readable format, and filter the output based on specific criteria, you can effectively manage disk space on your Linux system.
How to use du command in Linux:
The du
command in Linux is used to estimate file and directory space usage. A basic usage example is:
du /path/to/directory
This command displays the disk space used by the specified directory.
Measuring disk space usage with du: To measure disk space usage:
du /path/to/directory
This command provides the total disk space used by the specified directory.
Displaying human-readable disk space in du: To display disk space in human-readable format (e.g., KB, MB, GB):
du -h /path/to/directory
The -h
option makes the output more readable.
Recursive disk space counting with du: To count disk space recursively for subdirectories:
du -h --max-depth=1 /path/to/directory
The --max-depth
option limits the depth of recursion.
Filtering specific directories or files in du: To filter specific directories or files:
du -h --exclude=/path/to/exclude /path/to/directory
Use the --exclude
option to exclude specific paths.
Sorting du output by size in Linux:
To sort du
output by size:
du -h --max-depth=1 /path/to/directory | sort -h
The sort -h
option sorts the output in a human-readable way.
Excluding certain directories from du analysis:
To exclude certain directories from du
analysis:
du -h --exclude=/path/to/exclude /path/to/directory
Use the --exclude
option to exclude specific paths.
Checking disk space usage for multiple directories with du: To check disk space usage for multiple directories:
du -h /path/to/dir1 /path/to/dir2
This command provides disk space usage for each specified directory.
Using du for disk space analysis and cleanup in Linux: For disk space analysis and cleanup:
du -h --max-depth=1 /path/to/directory | sort -h
Identify large directories and files and consider cleaning or moving them to free up space.