In Linux, du stands for “disk usage” and it’s a command most often used to check the size (and other space/disk usage) of the files and directories.
The du command can be used on any Linux distro, including Ubuntu, Debian, Fedora, Linux Mint, and more. It can even be used on macOS.
Basic du command syntax
The syntax of the du command in Linux is:
du [OPTIONs] [FILEs]
You can run the du command just by entering “du” without any options or flags:
This will give you an output with disk usage information for the directories in the directory you’re currently in. Running the du command without flags or options will give you a huge output with not-so-readable stats.
Example output by running the du command without options or flags:
... hundreds of lines 14308 ./www/html/wordpress/wp-content 71960 ./www/html/wordpress 95768 ./www/html 95772 ./www 1574560 .
This is just a part of the output when running du in /var/. You’ll get hundreds of lines for each (sub)directory in /var/. The number in that output is (usually) kilobytes (KB).
The last line of the results from the du command is the total file size.
If you run the command without providing a directory, it will run in the directory you’re currently in (pwd). If you’d like to run the command in a different directory, you’ll need to specify it after the options.
You’ll get the results for the directories within /var/www/html, regardless of what directory you’re currently in.
Options you can use with the du command
Here are some of the useful options/flags you can use with the du command:
- -h, –human-readable – this option will show the file sizes in a “human readable” format, for example: 100K, 100MB, 100G. This is the most commonly used option for du. To display the results only in KBs, MBs or GBs, use -k, -m or -g
- –a, –all will display everything, including directories, and files.
- -t, –threshold=SIZE – excludes results smaller than the specified SIZE, or greater than -SIZE (if negative). Example: du –treshold=1G will only show files that are larger than 1GB.
- –-time – shows the last modified date/time of each file, next to its file size.
- -X, –exclude=PATTERN – this option will exclude files that match the given pattern. Example: du –exclude=”*.txt” will exclude all .txt files from the output.
- -s, –summarize to get the total size of a directory.
- –help – to get detailed help and information about the du command and its options
The most commonly used examples of the du command in Linux
Here are some useful examples of the du command that are commonly used in Linux:
Display the results in gigabytes (GB)
Use the -h option for this. Example:
20K ./wp-content/themes/twentytwentytwo/parts 6.7M ./wp-content/themes/twentytwentytwo 14M ./wp-content/themes 14M ./wp-content 71M .
Instead of just numbers, without specifying what they are (KB, MB, GB)
Display directories AND files in a readable format
For this, you need to use both the -h and -a options. It’s the most commonly used example for the du command:
This will also display the size of the files in a human-readable format (K, M, G)
Exclude results smaller than 100MB
To exclude all files and directories that are smaller than 100MB in size, use the –threshold option:
du -ha --threshold=100M
This will only show files and directories that are larger than 100MB.
Show the last modified date next to the file size
Use the –time option to get the modified date right next to the size in the results. Example:
du -ha --time
This will give you an output similar to:
14M 2022-11-15 21:03 ./wp-content 4.0K 2021-12-14 10:44 ./wp-config-sample.php 4.0K 2020-02-06 08:33 ./wp-blog-header.php 8.0K 2022-10-17 14:22 ./wp-trackback.php 4.0K 2022-09-19 11:59 ./wp-load.php 4.0K 2022-03-19 22:31 ./wp-links-opml.php 71M 2022-11-15 21:03 .
Exclude certain file types
A commonly used option for the du command is –exclude. This is most often used to exclude certain file types. For example, if you want to exclude all .php files from the results, you’ll need to use:
du -ha --exclude="*.php"
Only show the total size of the directory
If you want to check the size of a certain (single) directory, you need to use the -s (–summarize) option. By using the -s option, you’ll get the size info for the directory you’re running the command for. This is also the last/final line of the results when you run the du command without the -s flag.
For example, if you run:
du -hs /var/www/html/wordpress
Only the size of the wordpress directory.
Now that you know how to check the size/disk usage of directories and files in Linux, you can learn more about other commands.
Know of any other useful du command examples? Leave a comment below
I’m sure there are other useful command examples and different use cases for the du command. Leave a comment below if you have any suggestions.