The du command comes from the term “disk usage.” As the name suggests, this command is used to estimate and display the disk space used by files and directories. The du command in Linux provides valuable information that helps identify space-consuming directories, manage disk space efficiently, and understand overall storage usage.
In this article, we will explore how the du command works and how to use its different options to analyze disk usage effectively.
Contents
Basic Uses of the du Command
The general syntax for this command is:
du [options] [file_or_directory]
Example
du file.txt
Output
4 file.txt
Example – Check the size of a directory
The following command shows the disk usage of a directory and all of its subdirectories.
du myfolder
Output
4 myfolder/file1
8 myfolder/file2
16 myfolder
You can also do this for multiple directories or files at once.
du dir1 dir2 dir3
A few important things to remember when using the du command:
If you run the du command without specifying any file or directory as an argument, it automatically works on the current directory. If you want to check the disk usage of another directory without navigating to it, you can simply provide the directory path as an argument to the command.
Flags
You can use multiple flags with the du command to customize and format the output. Some of the most common and useful flags used with the du command are listed below.
-h | ➟ | Sizes in human-readable format (KB, MB, GB) |
-s | ➟ | Don’t include the size of subdirectories. |
-a | ➟ | All files and directories, including hidden ones. |
--max-depth=N | ➟ | Limit subdirectory depth |
--exclude | ➟ | Excludes specific directories or files |
-x | ➟ | Stay on one filesystem |
Use of the du Command with Flags
The -h, -s, and -a flags are the most used flags with the du command.
Here are some basic examples of using the du command with different flags.
du # sizes of the directories and subdirectories (in KB)
du -h # Human-readable (KB, MB, GB)
du -a # Show file and dir sizes
du -s # Summary of the directory
du -sh # Human readable summary
du -ah # Human-readable + all files
Summarize and Total Size
Here are some example thst shows
du -sh # Summary of the directory
du -sh /path # Summary of a directory
du -sh * # Size of all items in current dir
du -sh * /path # Summary of all files/dirs
The -c flag adds the total disk usage at the end of the output. You can combine this flag with the above commands, as shown below.
du -ch # Total size + sub directory size
du -chs * # Total + all items in current dir
du -cah # All files + total + human-readable
The -x option tells the du command to stay on the same filesystem. It ignores mounted drives or partitions inside the directory tree.
du -shx / # Summary of root FS (exclude mounts)
Other useful uses
du -sh ~/.* # Check hidden dirs in home
du -sh /var/* # Check sizes of /var/ subdirectories
Shorting and Filtering
We can also filter the output using various parameters, such as how deep to explore subdirectories or the maximum file size to display.
du -h --max-depth=1 # Show 1 level deep
du -ah --threshold=1M # Only show files >1MB
We can also combine the du command with the sort command to organize and shorten the output, which makes it easier to identify directories that use the most disk space.
du -ah | sort -rh # Sort by size (largest first)
du -sh * | sort -h # Sorted by size (smallest first)
du -ah --max-depth=1 | sort -rh # short, 1 level deep
We can also combine it with the head command to display only the top few results, which helps quickly find the largest files or directories.
du -ah /path | sort -rh | head -20 # Top 20 largest files/dirs
Similarly, we can use it with grep to filter the output and show only lines that match a specific pattern or keyword.
du -ah / | grep "\.log$" # Find all log files with sizes
du -ah /var | grep -E "[0-9]G\b" # Find gigabyte-sized items
Exclude Files/Directories
We can also exclude specific directories or files from the du operation using the --exclude option.
du -h --exclude="*.log" # Exclude .log files
du -sh --exclude-dir=dir_to_ignore # Skip a directory
Advanced Options
We can also view advanced information about directories, such as modification time, apparent size, inode count, and other related details.
The --time option is used to display the modification timestamp of a file or directory along with its disk usage.
du -h --time
Normally, du shows the actual disk space used, which may be larger because of filesystem blocks.
If you want to see the logical size of files (apparent size), use the --apparent-size option.
du -h --apparent-size
In this case, the size displayed is the actual file size, not the space consumed by disk blocks.
You can also view the inode count using the --inodes option. An inode is a metadata record that stores information about a file, such as its permissions, owner, and location on the disk.
du -h --inodes