du command in Linux with examples

On any operating system, the files on your hard disk take up a certain amount of space. In Linux specifically, you can view how much space that these files take up in the command line terminal by using the du command. The du command (the name is shortened from “disk usage”), as the name implies, will simply display, in its output, the amount of disk space being used by a specified file or directory.

For example, if you’re wondering why it seems like there are files that are consuming more disk space than they should be, you can use the du command to ascertain which files are the cause of it. In this tutorial, we will discuss the typical applications of the du command and some of its options to help you best utilize the space in your Linux system’s hard disk.

In this tutorial you will learn:

  • How to use the du command on Linux
du command in Linux with examples
du command in Linux with examples
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software du
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Frequently Used Options



du command in Linux Basic Examples

  1. We can use the du command for both files and directories. But for directories specifically, running the du command will simply display the disk usage of every file and subdirectory inside. If you run the du command by itself, without any additional options or a specified file, it will display the disk usage of the current working directory.
    $ du /etc
    8	/etc/cracklib
    12	/etc/vim
    12	/etc/cron.monthly
    [...]
    total: 11312
    

    Sometimes, though, you might have to view the disk usage of a file or directory that you do not have permissions for. If you try to run the du command on these files or directories, the output will display an error that reads “du: cannot read directory”. You can prevent this by evoking the sudo command.

  2. We can also use the du command to view the disk usage of multiple files or directories at the same time, instead of just one.
    $ du /etc /tmp
    
    Using the du command to display the disk usage of multiple directories
    Using the du command to display the disk usage of multiple directories

    In the screenshot above, you can see that we used the sudo command so that the du command has permission to read certain files and directories in the /tmp directory.

  3. In the examples above, we showed how the du command displays the disk usage of every file or subdirectory in a given directory. But we can go one step further from that with the -a (shortened from “all”) command option and display the disk usage of every file within the entire specified file path instead of only the tail end directory.
    $ du -a /home/user
    

    View the disk usage of every file within a file path
    View the disk usage of every file within a file path
  4. We can utilize the -h option to display the disk usage of a file or directory in a human readable format.
    $ du -h /var
    

    Using the du command with the human readable -h option to display disk usage of a file in a human readable format
    Using the human readable -h option to display disk usage of a file in a human readable format


NOTE
You can always use the man command to read more about the touch command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.

Advanced Usage

The du command is pretty simple, but as you’ve observed throughout the examples section of this article, it comes packed with a lot of options. Many of these options fly under the radar, and even some seasoned system administrators may not know them. However, they can definitely come in handy in various situations. In this section of the tutorial, we’ll show you a few of the lesser known options of the du command that we think are useful.

du command in Linux Advanced Examples

  1. In the examples above, we’ve covered how, by default, the du command displays the disk usage of a directory and every subdirectory within it. If there is a situation in which you need to view the disk space of one directory exclusively, we can use the -s command line option. This option will completely omit, from its output, any subdirectories inside the directory you specify in the command line.
    $ du -s Downloads
    

    Using the du command with the -s option to omit disk usage info from subdirectories
    Using the du command with the -s option to omit disk usage info from subdirectories
  2. We’ve already shown how the du command can be used to display the disk usage of multiple files or directories at the same time. But it can become quite tedious or challenging to summarize the disk usage when there are many files and subdirectories with additional files inside. In this situation, we could simply use the -c option. The -c option combines the size of multiple directories and calculates the total, so you don’t have to do any of the heavy lifting.
    $ du -c /etc /tmp
    
    Using the -c option to combine the size of multiple directories
    Using the -c option to combine the size of multiple directories

    As you can see in the screenshot above, while the total disk space usage of the specified directories is displayed, the individual amounts are also shown.



  3. We can also get the du command to display the absolute amount of data stored within a file or directory, as by default, the du command displays the amount of data that is actually using the space in a hard disk. We can achieve this by using --apparent-size.
    $ du --apparent-size /home/user
    12	/home/linuxconfig/.config/evolution/sources
    16	/home/linuxconfig/.config/evolution
    152	/home/linuxconfig/.config
    [...]
    total: 45955
    
  4. You can also specify the depth that the du command will go within a directory with the --max-depth option. For example, we would use the syntax below to display the disk usage of one subdirectory within the Downloads directory, while omitting its subdirectories.
    $ du --max-depth=2 Downloads
    

    Using the --max-depth command line option to display the disk usage of a directory at a specified depth
    Using the –max-depth command line option to display the disk usage of a directory at a specified depth
  5. We can also use the du command with the --exclude option to exclude specified files and directories from its output. We can do this by passing the pattern of the file and directory names as an argument for the du command. For example, we can exclude all hidden files in our home directory passing the ./. name pattern to the du command, as all hidden file names start with a period.
    $ du -ha --exclude=./.*
    

    Using the --exclude option to exclude all hidden files in our home directory
    Using the –exclude option to exclude all hidden files in our home directory
  6. We can also specify multiple exclusion patterns instead of just one like we did in the previous example. This can be achieved by repeating the --exclude option in the same syntax on the command line.
    $ du -ha --exclude=./.* --exclude=./P*
    
    Using the --exclude command line option to exclude multiple patterns
    Using the –exclude command line option to exclude multiple patterns

    As you can see in the screenshot above, we excluded both hidden files and directories and the files and directories whose names start with a P.



  7. An alternative way of excluding files and directories with the du command is by using the -x or --exclude-from command line options. This option differs from the one in the previous example in that it takes the name of a text file as an argument, and runs the du command on the patterns written inside. So, for example, let’s create a text file called exclusions.txt and add in our patterns. We’ll then pass it to the du command.
    $ du -ha --exclude-from=exclusions.txt
    

    It might be helpful to know that the size of the text file is included in the output when using the --exclude-from command line option.

Closing Thoughts

In this tutorial, we learned all about the du command on Linux which is essential to master for users and administrators who like to monitor disk usage from files and directories on Linux frequently. For further reading, check out the df command, which complements du very well.