The du
command in Unix-like operating systems is used to estimate and report the disk space usage of files and directories. Unlike the df
command, which shows the disk usage of entire file systems, du
provides a more granular view, typically at the directory level, helping users identify which files and directories are consuming the most disk space. It’s an essential tool for managing disk space, especially on systems with large amounts of data or many files.
Here’s an in-depth guide to the du
command and its usage:
The basic syntax of the du
command is:
du [options] [directory or file]
- directory or file: The path of the directory or file you want to check. If no directory is provided,
du
will check the current directory.
-
Check disk usage of the current directory:
du
This command shows the disk usage of the current directory and all its subdirectories.
-
Check disk usage of a specific directory:
du /path/to/directory
This shows the disk usage for the specified directory and its subdirectories.
The du
command typically displays output in the following format:
4 /path/to/directory/file1
12 /path/to/directory
- Size (in blocks): The first column indicates the disk space used by the file or directory, in 1K-blocks by default.
- Path: The second column shows the path to the file or directory.
For example, the above output indicates that file1
uses 4 blocks, and the directory itself uses 12 blocks, including subdirectories.
The du
command includes several options to change its behavior or provide more specific information. Here are some of the most commonly used options:
-h
: Displays disk space in a human-readable format (KB, MB, GB, etc.).Example output:du -h
4.0K /path/to/file1 12K /path/to/directory
du [directory]
: Shows the disk usage of a specific directory and its subdirectories.du -h /home/user/Documents
-s
: Summarizes the total disk usage for the specified directory, without showing details for each subdirectory.Example output:du -sh /home/user
1.5G /home/user
--max-depth=[N]
: Limits the depth of subdirectories to display. For example,--max-depth=1
shows only the disk usage for the top-level directories.du -h --max-depth=1 /home/user
-a
: Includes all files in the output, not just directories.du -ah /home/user
-b
: Shows the disk usage in bytes.du -b /path/to/file_or_directory
-c
: Displays a grand total at the end of the output.Example output:du -ch /home/user
4.0K /home/user/file1 12K /home/user 12K total
-x
: Limits the display to the current file system. This excludes directories mounted on different file systems.du -h -x /home
-k
: Forces the display of disk space in kilobytes (default for many systems).du -k /path/to/directory
To display disk usage for the current directory in a human-readable format:
du -h
To check the disk usage of a directory (e.g., /var/log
):
du -sh /var/log
To display the disk usage of all files and subdirectories in a specific directory:
du -ah /home/user
To get a summary of disk usage for the entire system (or a specific directory):
du -sh /
To view the disk usage of the top-level directories in the /home/user
directory:
du -h --max-depth=1 /home/user
To show the disk usage of a directory in bytes:
du -b /path/to/directory
To display the disk usage and include a grand total:
du -ch /home/user
You can use du
with sorting (sort
) to identify the largest directories. For example:
du -ah /path/to/directory | sort -rh | head -n 10
This command lists the top 10 largest files and directories in the specified directory.
If you want to exclude certain files or directories from the du
output, you can use grep
or other shell utilities in combination with du
:
du -ah /home/user | grep -v '/tmp'
This excludes any results related to /tmp
.
You can combine du
with the watch
command to monitor disk usage over time:
watch du -h --max-depth=1 /home/user
This will update the disk usage statistics every 2 seconds.
Option | Description |
---|---|
-h |
Display disk usage in human-readable format (KB, MB, GB, etc.) |
-s |
Show the total disk usage of the specified directory |
--max-depth=[N] |
Limit the output to the top N levels of subdirectories |
-a |
Display disk usage for all files (not just directories) |
-b |
Display disk usage in bytes |
-c |
Display a total at the end of the output |
-k |
Display disk usage in kilobytes |
-x |
Exclude directories on different file systems from the output |
-i |
Show inode usage (for file systems with many small files) |
The du
command is a powerful and flexible tool for monitoring disk space usage on Unix-like systems. It allows users to track disk usage at a fine-grained level, helping system administrators and regular users identify large files or directories that may need to be cleaned up or managed. By using various options, du
can be customized to provide both detailed and summary views, making it an indispensable part of any system administrator's toolkit.
The du
(disk usage) command in Unix-like operating systems (Linux, macOS, etc.) is used to estimate and report the disk space used by files and directories. It provides detailed information about the disk usage of files and directories, which is essential for managing storage and ensuring that systems do not run out of space.
The basic syntax of the du
command is:
du [options] [file or directory]
- file or directory: This is the file or directory you want to check the disk usage for. If no file or directory is specified,
du
will display the disk usage for the current directory.
-
Check disk usage for the current directory:
du
This will display the disk usage of the current directory and its subdirectories.
-
Check disk usage for a specific directory:
du /path/to/directory
This will display the disk usage of the specified directory and its subdirectories.
The default output of du
is a list of directories and their respective disk usage in kilobytes (KB).
Column | Description |
---|---|
Size | The disk space used by the file or directory (in kilobytes by default). |
Directory | The directory or file for which the disk usage is reported. |
8 ./folder1
4 ./folder1/subfolder1
12 ./folder2
24 .
In this example:
- The total disk usage of
folder1
(including its subfoldersubfolder1
) is 8 KB. - The total disk usage of
folder2
is 12 KB. - The total disk usage of the current directory (represented by
.
) is 24 KB.
The du
command comes with several options to modify its behavior or provide more detailed information.
-h
: This option displays the disk usage in a human-readable format, using KB, MB, GB, etc.Example output:du -h
8.0K ./folder1 4.0K ./folder1/subfolder1 12K ./folder2 24K .
-s
: This option shows only the total disk usage of the specified directory (and not its subdirectories).Example output:du -sh /path/to/directory
24K /path/to/directory
-a
: This option shows the disk usage for all files and subdirectories in addition to directories.Example output:du -ah
4.0K ./file1 8.0K ./folder1 4.0K ./folder1/subfolder1 12K ./folder2 24K .
--max-depth=N
: This option limits the depth of the directory structure to display. For example,--max-depth=1
will show disk usage for the current directory and its immediate subdirectories, but not deeper levels.Example output:du -h --max-depth=1
8.0K ./folder1 12K ./folder2 24K .
--exclude=PATTERN
: This option excludes files or directories matching the given pattern.This will show disk usage for all files and directories, excluding those that end withdu -ah --exclude="*.log"
.log
.
-d N
: This option limits the depth of directory listing, similar to--max-depth=N
.du -hd 2
-
| sort -h
: To sort the output by disk usage in ascending or descending order, you can pipe the output ofdu
into thesort
command.du -ah | sort -h
-
-r
: Sort in reverse order (largest files/directories first).du -ah | sort -hr
-b
: This option shows disk usage in bytes (not kilobytes, which is the default).du -b
To check the disk usage for a directory (e.g., /home/user/documents
) in a human-readable format:
du -sh /home/user/documents
To show the disk usage of all files and directories within /home/user/documents
:
du -ah /home/user/documents
To view the disk usage for the current directory and its immediate subdirectories only:
du -h --max-depth=1
To list the largest files in the current directory and subdirectories, sorted by disk usage:
du -ah | sort -rh | head -n 10
This will display the top 10 largest files and directories in the current directory.
To exclude all .log
files from the disk usage calculation:
du -ah --exclude="*.log"
To show disk usage for a directory and its subdirectories up to 2 levels deep:
du -h --max-depth=2 /path/to/directory
-
Monitoring Disk Usage: Track the disk usage of specific directories or files to ensure you're not running out of space.
-
Disk Cleanup: Identify large files or directories that can be deleted or archived to free up disk space.
-
Backup Verification: Check how much space the backup files are consuming by running
du
on the backup directory. -
Optimizing Disk Space: Use
du
to identify large directories or files that could be optimized or compressed to save space.
| Option | Description |
| ------------------- | -------------------------------------------------------------------- | ------------------------------------------------------------------------ |
| -h
| Display sizes in human-readable format (KB, MB, GB, etc.). |
| -s
| Show only total disk usage for the specified directory. |
| -a
| Show disk usage for all files and directories, not just directories. |
| --max-depth=N
| Limit the depth of the directory structure displayed. |
| --exclude=PATTERN
| Exclude files or directories matching the pattern from the output. |
| -b
| Display disk usage in bytes instead of kilobytes. |
| -d N
| Limit the depth of directory listing. |
| | sort -h
| Sort the output by disk usage in ascending order. |
| | sort -hr
| Sort the output by disk usage in descending order (largest files first). |
The du
command is an invaluable tool for disk space management and analysis. It helps users and system administrators understand how disk space is being used across files and directories, making it easier to identify large files and directories that might be taking up unnecessary space. With its variety of options, du
provides a flexible and powerful method for managing disk space effectively on Unix-like systems.
The du
(Disk Usage) command in Unix-like operating systems (Linux, macOS, etc.) is used to estimate and report the disk space usage of files and directories. It is a helpful tool for system administrators and users who want to analyze and manage disk space, especially for directories with large amounts of data.
Here's an in-depth explanation of the du
command, its options, and examples of how to use it effectively:
The basic syntax of the du
command is:
du [options] [file/directory]
- file/directory: The file or directory you want to check for disk usage. If no file or directory is specified,
du
will report the disk usage of the current directory.
-
Check disk usage of the current directory:
du
This shows the disk usage of the current directory and its subdirectories in terms of disk blocks (usually 1K-blocks).
-
Check disk usage for a specific directory:
du /path/to/directory
This shows the disk usage for the specified directory.
By default, du
displays the disk usage for each directory and its subdirectories, in 1K-blocks. For example:
8 ./dir1
4 ./dir1/subdir1
4 ./dir1/subdir2
16 .
In this output:
8
represents the total disk usage ofdir1
.4
is the disk usage ofsubdir1
andsubdir2
.16
at the bottom represents the total disk usage of the current directory (.
), which includes the disk usage of all its subdirectories.
There are several useful options you can use with du
to modify its behavior. Here are some of the most common options:
-h
: Displays the disk usage in a human-readable format, using KB, MB, GB, etc., instead of the default number of 1K-blocks.Example output:du -h
8.0K ./dir1 4.0K ./dir1/subdir1 4.0K ./dir1/subdir2 16K .
-s
: Summarizes the total disk usage for the given directory or file, rather than showing the disk usage of all its subdirectories.Example output:du -sh /path/to/directory
16K /path/to/directory
-a
: Displays disk usage for all files and directories, rather than just directories.Example output:du -ah /path/to/directory
4.0K /path/to/directory/file1.txt 8.0K /path/to/directory/dir1 4.0K /path/to/directory/dir1/file2.txt
--max-depth=N
: Limits the display of disk usage to directories that are at mostN
levels deep.Example output:du -h --max-depth=1 /path/to/directory
Here,8.0K /path/to/directory/dir1 4.0K /path/to/directory/dir2 16K /path/to/directory
--max-depth=1
shows only the top-level directories and their sizes.
--exclude=PATTERN
: Excludes files or directories that match the given pattern.du -ah --exclude='*.txt' /path/to/directory
-i
: Reports the number of inodes (file system objects) used by each file or directory, rather than the disk space usage.Example output:du -i /path/to/directory
8 ./dir1 4 ./dir1/subdir1 4 ./dir1/subdir2 16 .
--max-depth=N
: Combined with sorting tools likesort
, you can usedu
to display the largest directories. This is helpful for finding large files and directories consuming disk space.du -h --max-depth=1 /path/to/directory | sort -rh
-k
: Forces the display of disk space in kilobytes (default behavior).-m
: Displays the disk usage in megabytes.-g
: Displays the disk usage in gigabytes.
du -h -m /path/to/directory
This will display the disk usage in megabytes.
Here are some practical examples of how you can use the du
command:
-
Check disk usage for the current directory and all its subdirectories:
du -ah
-
Check the total disk usage of a specific directory in human-readable format:
du -sh /var/log
-
List the disk usage of each subdirectory in the
/home
directory, but limit the depth to 2 levels:du -h --max-depth=2 /home
-
Find large files and directories within the
/var
directory:du -ah /var | sort -rh | head -n 10
-
Check disk usage of all files and directories in the
/etc
directory but exclude.bak
files:du -ah --exclude='*.bak' /etc
You can use du
in a script to track changes in disk usage over time. For example, you could use the following command to capture the disk usage and save it to a file:
du -sh /path/to/directory > disk_usage.txt
You can combine du
with find
to identify large files or directories that may be taking up significant disk space:
find /path/to/directory -type f -exec du -h {} + | sort -rh | head -n 10
This finds the 10 largest files within the directory and displays them.
To automate disk space checks, you can use du
within a cron job. For example, you can schedule a weekly report of the /home
directory disk usage:
crontab -e
Add the following line to check the disk usage of /home
and email it to yourself every Sunday at midnight:
0 0 * * SUN du -sh /home | mail -s "Disk Usage Report" [email protected]
Option | Description |
---|---|
-h |
Human-readable output (KB, MB, GB, etc.) |
-s |
Show total disk usage for the specified directory or file |
-a |
Show usage for all files, not just directories |
--max-depth=N |
Limit the depth of directory recursion to N levels |
-i |
Show inode usage rather than disk space |
--exclude=PATTERN |
Exclude files and directories matching the pattern |
-k |
Display usage in kilobytes (default) |
-m |
Display usage in megabytes |
-g |
Display usage in gigabytes |
The du
command is a powerful tool for checking disk usage in Linux and other Unix-like systems. It allows you to get detailed information about disk space usage at the file and directory levels. By using the various options, you can tailor its output to your specific needs—whether you're checking for large files, summarizing disk usage, or tracking usage over time. Regularly using du
can help prevent your system from running out of disk space and allow you to manage storage effectively.