du Command in Linux | Explained

Linux operating system (OS) comes with many different files which vary in disk size. If these files are too many, the user can face the disk usage notification indicating that you are low on space. For checking the disk usage, we use the “du” command. This post will demonstrate the different usage of the du command in Linux. The content for the post is as follows:

What is du Command in Linux?

In Linux, the “du” command stands for disk usage, which checks the memory allocation of different files. The syntax for the “du” command is shown below:

Syntax:

$ du [-Options] [File]

The usage of the “du” command depends on the options that can be obtained using the command:

$ du --help

The above image shows all the options that can be used with the “du” command.

Let’s move and implement the du command in the Linux terminal.

How to Use du Command in Linux?

You can use the du command using the terminal that you can access by pressing “CTRL + ALT + T” from the keyboard. Some of the examples are implemented below with different options.

Example 1: Printing Sizes in Human-Readable Format

To convert the disk usage into human-readable format, use the “h” option with the “du” command. This will show the file usage size in kilobytes(kB), megabytes(MB), or gigabytes (GB):

$ du -h /home/itslinuxfoss/Henry

Disk usage has been displayed in a human-readable format.

Example 2: Printing Grand Total Size

To print the grand total size, use the “c” option with the “du” command. To do this, execute the following command:

$ du -c -h /home/itslinuxfoss/Henry

Total disk usage size of the directory is printed on the screen.

Note: the total size is 194MB because there are two more files present other than “dir 2” and “Dir 1” in the Henry directory, which can be seen in the below image:

$ ls

Example 3: Printing All Files Including Directories With Last Modification

To get all disk usage for all files as well as directories with the last modification timestamp is obtained as follows:

$ du -ah --time /home/itslinuxfoss/Henry

All the files including directories have been displayed with their last modification time.

Example 4: Printing Sizes Till Specific Level

The “du” command also has the feature of deep analysis for the directory. The “du” command has “d” options and levels 1 and 2. Level 1 analyzes disk usage deeply, which means it will only go to the 1 subfolder, while level 2 analyzes disk usage more deeply and will check up to the 2 subfolders. Let’s understand this concept with an example.

To obtain level 1 analysis for any directory is obtained as follows:

$ du -d 1 /home/itslinuxfoss/

The above command has analyzed disk usage up to level 1.

Let’s do a level 2 analysis for the same directory:

$ du -d 2 /home/itslinuxfoss/

The above command has analyzed disk usage up to level 2 (up to 2 subfolders).

Example 5: Retrieving the Summary of File System

To obtain the overall summary of any directory using the “s” option with the command:

$ du -s -h /home/itslinuxfoss/Henry

The above image represents the total disk usage size of 194M, consumed by “Henry” directory.

Bonus Tip: Alternative Command of du in Linux

Apart from the “du” command, there is an alternative command, “ncdu”, that can be used for scanning and analyzing the disk usage in Linux. ”ncdu” stands for Ncurses Disk Usage that gives the useful and convenient way to analyze disk usage. This utility is pre-defined but can be installed using the given commands:

For Ubuntu/Debian:

$ sudo apt install ncdu

For CentOS:

$ sudo yum install epel-release

For Fedora:

$ sudo dnf install ncdu

To use the “ncdu” command, simply type it in the terminal and hit the enter button:

$ ncdu

This will give you the detailed description of the disk usage depending on where are you located in your directory.

Note: you can also use the “df” command for checking the disk usage, which prints the available disk usage:

$ df -a /home/itslinuxfoss/Henry

    Conclusion

    In Linux, the du command is a utility for scanning and viewing the disk usage occupied by files and directories. This post has demonstrated some of the most common usages of the “du” with the help of examples by utilizing different flags as well. Apart from this, the usage of alternative command for du has also been illustrated in this write-up.