How to find Disk Usage using DU command in Linux?

Posted in

How to find Disk Usage using DU command in Linux?

Maaz Bin Asad
Last updated on June 8, 2022

    Managing disks and file systems in Linux has now been easier than ever before, thanks to the GUI-based file managers. Creating and manipulating file systems, finding the amount of free space available, and the disk usage, all using the command-line, can be quite intimidating for beginners. However, once you get a good grip on these commands, you will find it quite easier to do so. It benefits you immensely if you know and understands the basic commands in Linux. Many times, before installing another package or downloading a huge file to your disk, you might want to check the available disk space in a directory. In case there's not enough space free, you can always find the largest-sized useless directory that you can delete to make up space for your new files. The du ( disk usage ) command is the best possible solution to your problem of finding the directories' sizes. Please note that this is different from the df command, which helps us find the amount of free space in a disk or a directory. The du command in Linux helps us find the amount of space in terms of bytes that a directory takes up. It also gives us other useful information regarding the disk usage of folders or files in the machine. Moreover, this command has several options that can be used to get the output in the most readable, pretty, and desired format. In this thorough article, we will explain how to use the du command with tons of examples. Please note that you should have access to a Linux machine, a command shell, and sudo privileges before you try to execute these commands. Also, working with sudo privileges is always a risky task, and we highly recommend that you execute these commands very carefully so that you don't end up making a mess of your system. The syntax of the du (disk usage) command is:

    $ du [options] {path to the target directory}

    Directory size using du Command

    If we execute the below command, it will display the sizes of all the files, subdirectories, and the sum of the sizes of subdirectories or files to get the size of the directory one level higher than it.

    $ du ~/Documents

    $ du ~:Documents

    Human-Readable Format

    However, it’s not clear what 4 or 8 means, whether it’s in KB, MB, GB, or other units. We can use the -h option along with the du command to print the result in a pretty, organized, and human-readable format.

    $ du -h ~/Documents

     $ du -h ~/Documents Instead of the -h option, we can also use the -m option to print the sizes in megabytes or the -k option to print the sizes in kilobytes.

    Display the Sizes of Files along with Directories

    You might have noticed that our Documents directory also has several files along with directories and folders. However, it does not display information regarding the files by default. To do so, we can use the -a ( all ) option along with the du command.

    $ du -ah ~/Documents

     $ du -ah ~/Documents You can see that now it displays the sizes of the files as well. One thing to note that even if the folder is empty, the minimum size is 4K. Also, it seems like all the sizes are a multiple of 4. This is so because the size of a block in a file system is 4KB. So, even if the folders are empty, the minimum size should be 4KB, and since the space for folders and files are allocated as memory blocks, the sizes are a multiple of 4.

    Total Size of a Directory

    If you just want to display the total size of a particular directory and not the sizes of its subdirectories and files inside it, you can use the -s (sum) option along with the du command.

    $ du -sh ~/Documents

     $ du -sh ~/Documents

    DU for Multiple Directories

    You can also use the du command to check the size or disk usages of multiple directories using a single command. For this, you will have to provide the file or directory path of all of them separated by spaces.

    $ du -h ~/Documents ~/sample

     $ du -h ~/Documents ~/sample You can use the -c option to show the total sum of all the directories that you mention. Please note that the -s option displays the sum of the sizes of files and subdirectories inside a directory. Let’s use both these options together.

    $ du -sch ~/Documents ~/sample

     $ du -sch ~/Documents ~/sample

    Defining the maximum depth

    If there are too many nested subdirectories inside a directory and you don’t want the sizes of all of them, you can use the -d or --max-depth options to define the maximum depth you want to recursively travel inside the current directory. For example, if you want to display the sizes of all the folders inside the current folder only and not of those at a further depth, you can set the depth to 1.

    $ du -h --max-depth=1 ~/Documents

     $ du -h --max-depth=1 ~/Documents You can see that in the above example, the command only returned the size of the current directory that is the Documents directory, and the directories just inside it, which is the sample directory. The directories folder1 and inner-folder-1 are not displayed as their depths are greater than 1.

    Excluding Files

    In case you want to leave or exclude certain files or extensions, you can use the --exclude option to specify the file name or use regex to specify a set of files. For example, if we don’t want to calculate the size of text files, we can use the following command.

    $ du -ah --exclude="*.txt" ~/Documents

     $ du -ah --exclude="*.txt" ~/Documents

    Sorting the output

    If you want to find the size of the biggest subdirectory or you want the sizes in increasing or decreasing order, you can use the sort command piped with the du command.

    $ du -ah ~/Documents | sort -rh

     $ du -ah ~/Documents | sort -rh The -r option, along with the sort command, helps to sort the output in reverse order of the sizes.

    Wrapping Up!

    In this article, we skimmed through the need to find out disk usage and sizes of directories in Linux. We discussed the basics of the du (disk usage) command along with its syntax. We further discussed the -h option that can be used to print the output in a pretty human-readable format. We saw several other use-cases of the du command combined with options such as -s, -c, -d, --exclude, etc., to filter our output in the way we want. We hope that through this detailed guide, you will now be able to get hands-on with the disk usage command in Linux. People are also reading:

    Leave a Comment on this Post

    0 Comments