The focus of this article will be on how you can check the disk usage of Linux systems from the command line. The command used to estimate disk usgae is the du command, short for disk usage. The du command is part of the GNU Core Utilities and available on all Linux and UNIX systems.
The du command can be used on directories or files. By default it will check and output disk usage for directories (not file size). It is important to know the difference in order to get the information you need quickly.
Table of Contents
Disk Usage vs File Size
The du command shows you disk usage, which is different than file size. It shows you multiples of the filesystem block size. A particular files may be smaller, if it does not use a complete block. The ls command on the other hand shows apparent size of a file. Although the du command has an option to show apparent size (more on that below), we recommend using the ls command for files and the du command for directories.
The apparent size of a file is how much data is contains. Block-oriented devices (hard drives, SSD, etc..) work by storing data in fixed-size blocks. As a file is written to a block device, there may be a block that is not completely filled. As a result, disk usage (as reported by du) is rounded up to the highest amount of blocks used. This is a simplification, but block devices and file systems are outside the scope of this tutorial.
Basic Usage of the du Command
Using the du command without any options will recursively check every directory and sub-directory in the current working directory. The output displays each directory disk usage and path, with a total disk usage at the end. In this example is shows the total followed by a dot (noting the current working directory) since we did not specify a directory.
[mcherisi@putor ~]$ du 5996 ./.cache/gnome-software/appstream 8 ./.cache/gnome-software/langpacks 4 ./.cache/gnome-software/fwupd/remotes.d/lvfs 8 ./.cache/gnome-software/fwupd/remotes.d 92 ./.cache/gnome-software/fwupd ... 4 ./Downloads 4 ./Pictures 72 ./.pki/nssdb 76 ./.pki 4 ./Desktop 714356 .
The du Command Options
The du command provides a lot of options for expanding it’s usefulness. Here we will outline some of the most popular options with examples.
Display Sizes in Human Readable Format
The default output units are not very friendly to humans. By using the -h (–human-readable) option we can display the sizes in a much more friendly format (e.g. 4K, 59M, 3G)
[mcherisi@putor ~]$ du -h . 5.9M ./.cache/gnome-software/appstream 8.0K ./.cache/gnome-software/langpacks 4.0K ./.cache/gnome-software/fwupd/remotes.d/lvfs 8.0K ./.cache/gnome-software/fwupd/remotes.d 92K ./.cache/gnome-software/fwupd ... 4.0K ./Downloads 4.0K ./Pictures 72K ./.pki/nssdb 76K ./.pki 4.0K ./Desktop 698M .
Get Summary of Directory Size Only
It is seldom useful to see a giant list of files. You can use the -s (–summarize) option to display ONLY the total size of the directory.
[mcherisi@putor ~]$ du -s 714356 .
Display the Size of Directory in KB or MB
You can also decide to display disk usage size in Kb or MB.
To accomplish this simply use –k for kilobytes, and -m for megabytes.
[mcherisi@putor ~]$ du -k Videos/ 861872 Videos/YouTube 17176 Videos/Intros 60620 Videos/Snippets 939672 Videos/
Example using -m for MB:
[mcherisi@putor ~]$ du -m Videos/ 842 Videos/YouTube 17 Videos/Intros 60 Videos/Snippets 918 Videos/
Show All Files and Directories
As you may have noticed, these commands only display the disk usage of the directories, not the files. To display the files as well, use the -a (–all_ flag as shown.
[mcherisi@putor ~]$ du -ah Videos/ 235M Videos/YouTube/bash-hash-table-explained.mp4 87M Videos/YouTube/timeout-command.mp4 496M Videos/YouTube/ClusterSSH-Youtube.mp4 26M Videos/YouTube/SteamLocomotive-sl.mp4 842M Videos/YouTube 9.1M Videos/Intros/Putorius-Intro-No-Tagline.mp4 7.7M Videos/Intros/Putorius-Intro-Short-w-Tagline-v2.mp4 17M Videos/Intros 13M Videos/Snippets/tags-file.mp4 9.1M Videos/Snippets/using-admin-console.mp4 7.1M Videos/Snippets/main-config.mp4 12M Videos/Snippets/clusters-file.mp4 4.6M Videos/Snippets/auto-build-config.mp4 16M Videos/Snippets/basic-usage-no-config.mp4 60M Videos/Snippets 918M Videos/
NOTE: Don’t forget to subscribe to our YouTube channel for video tutorials!
Find Total Directory Usage
To show a grand total of all the disk usage you can use the -c (–total) option.
[mcherisi@putor ~]$ du -c Videos/ 861872 Videos/YouTube 17176 Videos/Intros 60620 Videos/Snippets 939672 Videos/ 939672 total
This is very similar to the summarize (-s) option. The main difference being that summarize will show ONLY the total, and -c (–total) will still show the full output.
[mcherisi@putor ~]$ du -c Videos/ 861872 Videos/YouTube 17176 Videos/Intros 60620 Videos/Snippets 939672 Videos/ 939672 total [mcherisi@putor ~]$ du -s Videos/ 939672 Videos/
Show Apparent File Size with du Command
To show the apparent size (actual data size) of a file you can use the –apparent-size option.
[mcherisi@putor YouTube]$ du ClusterSSH-Youtube.mp4 507152 ClusterSSH-Youtube.mp4 [mcherisi@putor YouTube]$ du --apparent-size ClusterSSH-Youtube.mp4 507146 ClusterSSH-Youtube.mp4
In the above example, you can see the apparent file size is slightly smaller than the size reported by du. The –apparent-size output is more in line with what the ls command would report.
Limiting Directory Level or Depth
If you have a lot of directory depth (sub-directories inside sub-directories) you may want to limit the depth that du will go. Using the –max-depth option you can specify how far down the directory tree du will plunge.
For this example, let’s say we have the following directory tree.
[mcherisi@putor Documents]$ tree ~/Documents/ /home/mcherisi/Documents/ └── Recipes └── Italian └── Pizzaiola └── Steak
We can instruct the du utility to only go two levels deep like so:
[mcherisi@putor Documents]$ du --max-depth=2 12 ./Recipes/Italian 16 ./Recipes 20 .
Here is an example using max depth 1 through 4 to better illustrate how it works.
[mcherisi@putor Documents]$ du -d 1 16 ./Recipes 20 . [mcherisi@putor Documents]$ du -d 2 12 ./Recipes/Italian 16 ./Recipes 20 . [mcherisi@putor Documents]$ du -d 3 8 ./Recipes/Italian/Pizzaiola 12 ./Recipes/Italian 16 ./Recipes 20 . [mcherisi@putor Documents]$ du -d 4 4 ./Recipes/Italian/Pizzaiola/Steak 8 ./Recipes/Italian/Pizzaiola 12 ./Recipes/Italian 16 ./Recipes 20 .
Showing Hidden Files with the du Command
Unfortunately, du does not have an option to show hidden files. Here is a little work-around to show hidden files.
[mcherisi@putor ~]$ du -sm .[!.]* * 1 .bash_history 1 .bash_logout 1 .bash_profile 1 .bashrc 37 .cache ...OUTPUT TRUNCATED...
Combining Options and Piping Output
As with most Linux commands you can combine the options to further specify the desired information in the output. You can also pipe the output to other utilities to further refine the output. Here are some examples.
Get Summarized Grand Total in Human Readable Format
By combining these three options we can get very specific information.
[mcherisi@putor ~]$ du -csh ~/Videos/ 918M /home/mcherisi/Videos/ 918M total
Sort Largest Sub-Directories
We can pipe the output to sort so list the sub-directories that are using the most disk space in order from largest to smallest.
[mcherisi@putor Documents]$ du -h -d1 ~/Videos/ | sort -hr 918M /home/mcherisi/Videos/ 842M /home/mcherisi/Videos/YouTube 60M /home/mcherisi/Videos/Snippets 17M /home/mcherisi/Videos/Intros
The du command is a very mature tool and has a good deal of options to expand it’s usefulness. We covered most of the popular options here, but there are several more. Check the man page for all the options available. However, after reading this article, you should be comfortable with using the du command to find the disk usage of your system.
Resources and Links
This site uses Akismet to reduce spam. Learn how your comment data is processed.