Open In App

du Command in LINUX

While working on LINUX, there might come a situation when you want to transfer a set of files or the entire directory. In such a case, you might wanna know the disk space consumed by that particular directory or set of files. As you are dealing with LINUX, there exists a command line utility for this also which is du command that estimates and displays the disk space used by files.

So, in simple words du command-line utility helps you to find out the disk usage of set of files or a directory.



Here’s the syntax of du command :

//syntax of du command

du [OPTION]... [FILE]...
       or
du [OPTION]... --files0-from=F

where OPTION refers to the options compatible with du command and FILE refers to the filename of which you wanna know the disk space occupied.



Using du command

Suppose there are two files say kt.txt and pt.txt and you want to know the disk usage of these files, then you can simply use du command by specifying the file names along with it as:

//using du command

$du kt.txt pt.txt
8       kt.txt
4       pt.txt

/* the first column 
displayed the file's
disk usage */

So, as shown above du displayed the disk space used by the corresponding files.

Now, the displayed values are actually in the units of the first available SIZE from – -block-size, and the DU_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables and if not in this format then units are default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).

Don’t get puzzled from the above paragraph. We can simply use -h option to force du to produce the output in the human readable format.

Options for du command

Examples of using du command

1. Using -h option : As mentioned above, -h option is used to produce the output in human readable format.

//using -h with du

$du -h kt.txt pt.txt
8.0K    kt.txt
4.0K    pt.txt

/*now the output
is in human readable
format i.e in
Kilobytes */

2. Using du to show disk usage of a directory : Now, if you will pass a directory name say kartik as an argument to du it will show the disk usage info of the input directory kartik and its sub-directories (if any).

/*using du to display disk usage 
of a directory and its
sub-directories */

$du kartik
4       kartik/thakral
24      kartik

Above the disk usage info of the directory kartik and its sub-directory thakral is displayed.

3. Using -a option : now, as seen above only the disk usage info of directory
kartik and its sub-directory thakral is displayed but what if you also want to know the disk usage info of all the files present under the directory kartik. For this, use -a option.

//using -a with du

$du -a kartik
8       kartik/kt.txt
4       kartik/pt.txt
4       kartik/pranjal.png
4       kartik/thakral.png
4       kartik/thakral
24      kartik

/*so with -a option used
all the files (under directory
kartik) disk usage info is
displayed along with the 
thakral sub-directory */

4. Using -c option : This option displays the grand total as shown.

//using -c with du

$du -c -h kt.txt pt.txt
8.0K    kt.txt
4.0K    pt.txt
12.0K   total

/* at the end
total is displayed 
for the disk usage */

5. Using – -time option : This option is used to display the last modification time in the output of du.

//using --time with du

$du --time kt.txt
4       2017-11-18 16:00       kt.txt

/*so the last
modification date and
time gets displayed
when --time 
option is used */

6. Using – -exclude=PATTERN option : In one of the example above, all the files disk usage related info was displayed of directory kartik. Now, suppose you want to know the info of .txt files only and not of .png files, in that case to exclude the .png pattern you can use this option.

//using --exclude=PATTERN with du

$du --exclude=*.png -a kartik
8       kartik/kt.txt
4       kartik/pt.txt
4       kartik/thakral
24      kartik

/*so, in this case
.png files info are
excluded from the output */

7. Using – -max-depth=N option : Now, this option allows you to limit the output of du to a particular depth of a directory.
Suppose you have a directory named FRIENDS under which you have sub-directories as FRIENDS/college and FRIENDS/school and also under sub-directory college you have another sub-directory as FRIENDS/college/farewell then you can use – -max-depth=N option in this case as:

//using --max-depth=N with du

$du --max-depth=0 FRIENDS
24       FRIENDS


/* in this case you 
restricted du output
only to top=level
directory */

Now, for sub-directories college and school you can use :

$du --max-depth=1 FRIENDS
16      FRIENDS/college
8       FRIENDS/school
24      FRIENDS

Now, for FRIENDS/college/farewell you can use –max-depth=2 as:

$du --max-depth=2 FRIENDS
4       FRIENDS/college/farewell
16      FRIENDS/college
8       FRIENDS/school
24      FRIENDS

/*so this is how N
in --max-depth=N 
is used for levels */

8. Using – -files0-from=F option : As mentioned above, this is used to summarize disk usage of the NUL-terminated file names specified in the file F and if the file F is “-” then read names from the standard input.
Let’s use this option for taking input from STDIN as:

//using --files0from=F with du

$pwd
/home/kartik

$ls
kt.txt pt.txt thakral

/*now use this option for 
taking input from
STDIN */

$du --files0-from=-
kt.txt8 kt.txt
pt.txt4 pt.txt

/* in this case after 
giving kt.txt as a input
from STDIN there is need to
press Ctrl+D twice then the
output is shown and same for
pt.txt or any other file name
given from STDIN */

Applications of du command

Example of using du with filters

Let’s take a simple example of using du with sort command so that the output produced by du will be sorted in the increasing order of size of files.


$du -a kartik
8       kartik/kt.txt
4       kartik/pt.txt
4       kartik/pranjal.png
4       kartik/thakral.png
4       kartik/thakral
24      kartik

/*now using du to produce
sorted output */

$du -a kartik | sort -n
4       kartik/pt.txt
4       kartik/pranjal.png
4       kartik/thakral.png
4       kartik/thakral
8       kartik/kt.txt
24      kartik

/* now the output displayed
is sorted according to the size */

The sort command along with -n option used causes to list the output in numeric order with the file with the smallest size appearing first.
In this way du can be used to arrange the output according to the size.

That’s all about du command.


Article Tags :