The du
(Disk Usage) command reports the estimated amount of disk space used by files and directories on a machine.
The du
command accepts many options, which allow you to customize the disk usage results output in a variety of formats to meet your needs.
In this article we’ll learn to use the du
command to get disk usage information on files and directories. We will also explore some of it’s more frequently used options to modify the command’s output.
Table of Contents
- Prerequisites
- How to Use the du Command
- Show Output in Human Readable Format (du -h)
- Show All Output in Preferred Block Size (--block-size, -k, -m)
- Summarize Total Size of Files or Directories (du -s)
- Show Counts for All Files or Directories (du -a)
- Show Last Modification Time (-t)
- du Sort by Size
- Show Grand Total at Bottom of Output (-c)
- Show Disk Usage of Files Whose Names Match Pattern
- Exclude Files Based on Pattern Matching (--exclude)
- Exclude Files Based on Given Size (-t)
- Conclusion
Prerequisites
- A Linux or Unix-like operating system
- Access to a command-line
- A user with read permissions on the directories or files whose usage you’re trying to view
How to Use the du Command
The basic syntax of the du
command is the following:
du [OPTIONS]... [FILES_OR_DIRECTORIES]...
If run without any arguments, the du
command will display directory sizes recursively, starting from the current working directory.
du
will only output directories, without files, by default. We can use the -a
option to display disk usage for all files and directories.du
12292 ./Music/Metal 22532 ./Music/Rock 34828 ./Music 4 ./Notes 250884 ./Videos 35844 ./Pictures 1944624 .
Let’s explain the output above.
Left column: The values in the left column are the disk usage for each directory and subdirectory, displayed in Kibibytes (units of 1024 bytes). A kibibyte is equal to 1024 bytes, while a kilobyte is 1000 bytes.
To get a better idea of how sizes are displayed by default in Linux:
1 kibibyte = 1.024 kilobyte 1 mebibyte = 1.04858 megabyte 1 gibibyte = 1.07374 gigabytes
These can be made into a more human readable format, which we’ll cover in the next section.
Right column: Directories and sudirectories.
Last line: The total disk usage of the given directory. In the above case, we checked the current working directory, which is displayed as .
.
When we run the du
command with a directory as an argument, it will display the disk usage of the given directory, as well as its subdirectories:
du Music
12292 Music/Metal 22532 Music/Rock 34828 Music/
If you run du
on a directory on which you don’t have read permissions, you will receive Permssion denied
error, and it will just display the disk usage for an empty folder, which is usually 4 (4 kibibytes, which is 4.096 kilobytes):
du 'Secret Files'
du: cannot read directory 'Secret Files/': Permission denied 4 Secret Files/
In this case you can use sudo
:
sudo du 'Secret Files'
sudo du 'Secret Files'/ 12292 Secret Files/
du
accepts many options. We’ll cover the most used ones and the ones we considered would be useful in most cases. To see a full list of options you can read the manual by running man du
in your command line, or read du invocation (GNU Coreutils 9.0), which is more verbose.
Show Output in Human Readable Format (du -h)
The default size outputs are in bytes, which are not user friendly, and are difficult to make out.
Using the-h
option we can print the size output in Human Readable Format. This will display sizes in Kilobytes (K), Megabytes (M), Gigabytes (G), etc.
If we run du -h
on the current working directory, like we did above, we’ll see sizes displayed in human readable format.
du -h
13M ./Music/Metal 23M ./Music/Rock 35M ./Music 4.0K ./Notes 246M ./Videos 36M ./Pictures 1.9G .
Show All Output in Preferred Block Size (--block-size, -k, -m)
You can force commands to display in other sizes to make them easier to handle, depending on what you’re doing.
You may prefer sizes to be displayed as Kilobytes (kB
)/Megabytes (MB
)/Gigabytes (GB
), rather than the default Kibibytes (K
)/Mebibytes (M
)/Gibibytes (G
). Or perhaps you’re writing a script and you need sizes displayed in a certain format.
To do this you can use the --block-size
option. It has a lot of sizes you can use, so we’ll cover just a few. If you’d like to see the full list accepted block sizes see it at [Block size (GNU Coreutils 9.0)](https://www.gnu.org/software/coreutils/manual/html_node/Block-size.html).
To force du
to display sizes in kilobytes use --block-size=kB
:
du --block-size=kB
12588kB ./Music/Metal 23073kB ./Music/Rock 35664kB ./Music 5kB ./Notes 256906kB ./Videos 36705kB ./Pictures 1991300kB .
To force du
to display sizes in megabytes use --block-size=MB
:
13MB ./Music/Metal 24MB ./Music/Rock 36MB ./Music 1MB ./Notes 257MB ./Videos 37MB ./Pictures 1992MB .
du
to use a certain block size, it will round non-integer quantities to the next higher unit. For example, if a file has a size less than 1 megabyte, such as 4 kilobytes, and we force it to display values in megabytes, it will show the minimum size in megabytes, which is 1 megabyte.Similarly, if a file has a size of 1 kilobyte and we force du
to display all sizes in gigabytes, then it will display the file as having 1 gigabyte.
du --block-size=GB
1GB ./Music/Metal 1GB ./Music/Rock 1GB ./Music 1GB ./Notes 1GB ./Videos 1GB ./Pictures 2GB .
An easier way of displaying results in kibibytes or mebibytes is by using the -k
(same as --block-size=k
) or -m
(same as --block-size=M
) options.
Summarize Total Size of Files or Directories (du -s)
The -s
option forces du
to just display just the total size of the given directory, without displaying each subdirectory. It’s basically the last line we see when running the du
command without this option.
We can also combine the -s
option with the -h
option, to make the output human readable.
du -sh Music
35M Music/
Show Counts for All Files or Directories (du -a)
A very useful option -a
, which will display disk usage for all files and directories in the given directory. We’ll combine it with the -h
option to make the output easier to read:
du -ah Music
12M documents/Music/Metal/song_3.mp4 13M documents/Music/Metal 13M documents/Music/Rock/song_1.mp3 9.0M documents/Music/Rock/song_2.mp3 23M documents/Music/Rock 35M documents/Music 4.0K documents/Notes 245M documents/Videos/drone_footage.mp4 246M documents/Videos 342M documents/backup_db.sql 1.3G documents/backup.zip 0 documents/test 11M documents/Pictures/desert.png 9.0M documents/Pictures/cityscape.png 15M documents/Pictures/wallpaper.png 36M documents/Pictures 20K documents/config.php 1.9G documents/
As you can see, we’re seeing both directory and file sizes.
Show Last Modification Time (-t)
The --time
option displays the last modification made to each directory or file in the output.
du -h --time documents/
13M 2021-12-04 18:15 documents/Music/Metal 23M 2021-12-04 18:14 documents/Music/Rock 35M 2021-12-04 18:15 documents/Music 4.0K 2021-12-04 19:05 documents/Notes 246M 2021-12-04 17:40 documents/Videos 36M 2021-12-04 17:40 documents/Pictures 1.9G 2021-12-04 19:57 documents/
du Sort by Size
du
doesn’t offer an option to sort the output by size, but we can work around this by passing the output to the sort
command. sort
is a utility that takes text lines and sorts them in a particular order.
Since du
outputs numbers at the beginning of each line, this means we can use sort
‘s -n
(--numeric-order
) option.
sort
also accepts a -h
option, so it can read that type of output.Sort Smallest to Largest Size
By default, sort
will sort in ascending order, meaning it will sort from smallest to largest size.
du documents/ | sort -n
4 documents/Notes 12292 documents/Music/Metal 22532 documents/Music/Rock 34828 documents/Music 35844 documents/Pictures 250884 documents/Videos 1944628 documents/
To sort smallest to largest in human readable format, we need to pass the -h
option to both du
and sort
:
du -h documents/ | sort -h
4.0K documents/Notes 13M documents/Music/Metal 23M documents/Music/Rock 35M documents/Music 36M documents/Pictures 246M documents/Videos 1.9G documents/
Sort Largest to Smallest Size
To sort in reverse order, we can use the -r
(reverse) option with the sort
command:
du documents/ | sort -nr
1944628 documents/ 250884 documents/Videos 35844 documents/Pictures 34828 documents/Music 22532 documents/Music/Rock 12292 documents/Music/Metal 4 documents/Notes
To sort largest to smallest in human readable format, we’ll pass the -h
option to both du
and sort
:
du -h documents/ | sort -hr
1.9G documents/ 246M documents/Videos 36M documents/Pictures 35M documents/Music 23M documents/Music/Rock 13M documents/Music/Metal 4.0K documents/Notes
Show Grand Total at Bottom of Output (-c)
The -c
option adds a line showing the grand total of the directories and/or files we checked.
At first glance this might not seem useful at all, since du
already shows a total at the bottom of the output.
However the -c
option is useful when checking disk usage for multiple given directories in a single command.
du -ch documents/{Music,Videos}
13M documents/Music/Metal 23M documents/Music/Rock 35M documents/Music 246M documents/Videos 280M total
If we run the du
command without the -c
option, on multiple directories we don’t see the total:
du -h documents/{Music,Videos}
13M documents/Music/Metal 23M documents/Music/Rock 35M documents/Music 246M documents/Videos
Show Disk Usage of Files Whose Names Match Pattern
You can use shell patterns for matching files and directories of a particular extension or that start/end with a particular string.
Filename matching is still useful when we want to use du
in the current working directory:
du -a documents/Mu*
12288 Music/Metal/song_3.mp4 12292 Music/Metal 13312 Music/Rock/song_1.mp3 9216 Music/Rock/song_2.mp3 22532 Music/Rock 34828 Music
As you can see, we used the -a
option, and du
is displaying all files and directories whose path begin with Mu
.
However we can’t ask it to find all mp3
files in the documents/
directory, and display the total size of mp3
files.
To work around this we can use the find
command with a few options that allows us to pass the output to du
, and du
will then use the output as given directories:
find documents -type f -name '*.mp3' -print0 | du -h -c --files0-from=-
13M documents/Music/Rock/song_1.mp3 9.0M documents/Music/Rock/song_2.mp3 22M total
Exclude Files Based on Pattern Matching (--exclude)
We can exclude files and directories based on filename matching patterns using the --exclude
option.
This can be very useful when you know you have very large files of certain types or whose names contain certain strings.
First we’ll check all files and directories, and exclude file types with the .mp3
extension:
du -ah --exclude='*.mp3' documents/Music
12M documents/Music/Metal/song_3.mp4 13M documents/Music/Metal 4.0K documents/Music/Rock 13M documents/Music
The output shows only directories and an mp4
file.
Filename pattern matching isn’t limited to extensions. We can exclude any directory that starts, ends, or contains some string.
For example we’ll exclude everything that contains the letter i
:
du -ah --exclude='*i*' documents
4.0K documents/Notes 342M documents/backup_db.sql 0 documents/test 343M documents
It excludes directories with names such as Music
or Videos
because they contain the letter i
.
Exclude Files Based on Given Size (-t)
With the -t
option we can set a threshold (or a limit) to the file sizes we want du
to include in the output.
If you give du
a positive size (like 200MB
), it will only output files or directories with a size greater or equal to the threshold.
We’ll combine it with the -a
option to have more files to work with, and -h
to make it human readable.
du -aht 200MB documents
245M documents/Videos/drone_footage.mp4 246M documents/Videos 342M documents/backup_db.sql 1.3G documents/backup.zip 1.9G documents
As you can see, we’re seeing only files and directories with sizes greater than 200MB.
If you give du
a negative size (like -200MB
), it will only output files or directories with a size smaller or equal to the threshold.
du -aht -200MB documents
Now we’re seeing files and directories with sizes smaller than 200MB.
12M documents/Music/Metal/song_3.mp4 13M documents/Music/Metal 13M documents/Music/Rock/song_1.mp3 9.0M documents/Music/Rock/song_2.mp3 23M documents/Music/Rock 35M documents/Music 4.0K documents/Notes 0 documents/test 11M documents/Pictures/desert.png 9.0M documents/Pictures/cityscape.png 15M documents/Pictures/wallpaper.png 36M documents/Pictures 20K documents/config.php
Conclusion
In this tutorial we learned how to use the du
command to report estimated disk usage by files and directories in Linux and Unix-like systems, along with some of its’ various options.
The du
command has many more options to help you report disk usage. To easily check all options you can check the manual by running man du
in your command-line, or by reading the du invocation (GNU Coreutils 9.0), which offers more explanations.
If you encountered any issues or have any questions then feel free to leave a comment and we’ll get back to you as soon as we can.
How can we include the date of the file?
Hello, I believe this should work.
Oh I totally missed that in here, sorry. Is there a way to have it report files and skip folders?
I’m not sure how to do it with the
du
command alone.Could you try something like this?