0% found this document useful (0 votes)
11 views12 pages

Du Command

The document explains the 'du' command in Linux, which estimates and displays the disk space used by files and directories. It details the command syntax, various options available (such as -h for human-readable format and -a for all files), and provides examples of how to use the command effectively. Additionally, it discusses applications of the 'du' command and how it can be combined with other commands like 'sort' for enhanced functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views12 pages

Du Command

The document explains the 'du' command in Linux, which estimates and displays the disk space used by files and directories. It details the command syntax, various options available (such as -h for human-readable format and -a for all files), and provides examples of how to use the command effectively. Additionally, it discusses applications of the 'du' command and how it can be combined with other commands like 'sort' for enhanced functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

7/24/2021 du Command in LINUX - GeeksforGeeks

Related Articles

du Command in LINUX
Last Updated : 15 May, 2019

While working on LINUX, there might come a situation when you want to transfer a set of

files or the entire director y. In such a case, you might wanna know the disk space

consumed by that par ticular director y or set of files. A s 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 director y.

Here’s the syntax of du command :

//syntax of du command

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

where OP TION 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 k t.txt and pt.txt and you want to know the disk usage of

these files, then you can simply use du command by specif ying the file names along with

it as:

We use cookies to ensure you have the best browsing experience on our website. By using our Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/du-command-linux/ 1/12
7/24/2021 du Command in LINUX - GeeksforGeeks

//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 BLOCK SIZE 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

-a, – -all option : This option produces counts as output for all files, not for just

directories.

– -apparent-size option : This prints the apparent sizes for the files and not the disk

usage which can be larger due to holes in files (sparse), internal fragmentation and

indirect blocks but in real the apparent size is smaller.

-c, – -total option : This displays a grand total.

-B, – -block-size=SIZE option : This option causes the size to scale by SIZE like -BM

prints the size in Megabytes.

-b, – -bytes option : This option is equivalent to – -apparent-size – -block-size=1.

-D, – -dereference-args option : This option is used to dereference only the symbolic

links listed on the command line.

-H option : This option is equivalent to the above -D option.

– -files0-from=F option : 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.

We use cookies to ensure you have the best browsing experience on our website. By using our
-h, – -human-readable option : This prints the sizes in human readable format i.e in
Got It !
site,rounding
you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
values and using abbreviations like 1 K and this is the most of ten used option

https://www.geeksforgeeks.org/du-command-linux/ 2/12
7/24/2021 du Command in LINUX - GeeksforGeeks

with du.

– -si option: This is much similar to the -h option but uses power of 1000 and not of

1024.

-k option : its equivalent to – -block-size=1K.

-l, – -count-links option : This count sizes many times if files are hard-linked.

-m option : This is equivalent to – – block-size=1M.

-L, – -dereference option : This option dereferences all symbolic links.

-P, – -no-dereference option : This option tells du not to follow any symbolic links

which is by default setting.

-0, –null option : This ends each output line with 0 byte rather than a newline.

-S, – -separate-dirs option : This causes the output not to include the size of

subdirectories.

-s, – -summarize option : This option will allow to display a total only for each

argument.

-x, – -one-file-system option : This will cause du to skip directories on different file

systems.

-X, – -exclude-from=FILE option : Exclude files that match any pattern given in FILE.

– -exclude=PAT TERN option : It will exclude files that match PAT TERN.

-d, – -max-depth=N option : Print the total for a director y (or file, with –all) only if it is

N or fewer levels below the command line argument; –max-depth=0 is the same as –

summarize.

– -time option : This will show the time of the last modification of any file in the

director y, or any of its subdirectories.

– -time=WORD option : This shows time as WORD instead of modification time :atime,

access, use, ctime or status.

– -time-style=ST YLE option : this shows time using ST YLE: full-iso, long-iso, iso, or

+FORMAT (FORM AT is interpreted like the format of date).

– -help option : This will display a help message and exit.

– -version option : This will display version info and exit.

Examples of using du command

1. Using -h option : A s mentioned above, -h option is used to produce the output in human

readable format.

//using -h with du
We use cookies to ensure you have the best browsing experience on our website. By using our Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/du-command-linux/ 3/12
7/24/2021 du Command in LINUX - GeeksforGeeks

$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 director y : Now, if you will pass a director y name say

kar tik as an argument to du it will show the disk usage info of the input director y kar tik

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

A bove the disk usage info of the director y kar tik and its sub-director y thakral is

displayed.

3. Using -a option : now, as seen above only the disk usage info of director y

kar tik and its sub-director y thakral is displayed but what if you also want to know the

disk usage info of all the files present under the director y kar tik. For this, use -a option.

//using -a with du

$du -a kartik
8 kartik/kt.txt
4 kartik/pt.txt
4 kartik/pranjal.png
We use cookies to ensure you have the best browsing experience on our website. By using our Got It !
4 kartik/thakral.png
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/du-command-linux/ 4/12
7/24/2021 du Command in LINUX - GeeksforGeeks

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=PAT TERN option : In one of the example above, all the files disk

We use cookies to ensure you have the best browsing experience on our website. By using our Got It !
usage related info was displayed of director y kar tik. Now, suppose you want to know the
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/du-command-linux/ 5/12
7/24/2021 du Command in LINUX - GeeksforGeeks

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

par ticular depth of a director y.

Suppose you have a director y named FRIENDS under which you have sub-directories as

FRIENDS/college and FRIENDS/school and also under sub-director y college you have

another sub-director y 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
We use cookies to ensure you have the best browsing experience on our website. By using our Got It !
8 you acknowledge
site, FRIENDS/school
that you have read and understood our Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/du-command-linux/ 6/12
7/24/2021 du Command in LINUX - GeeksforGeeks

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 : A s 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


We use cookies to ensure you have the best browsing experience on our website. By using our Got It !
giving
site, kt.txtthat
you acknowledge asyouahave
input
read and understood our Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/du-command-linux/ 7/12
7/24/2021 du Command in LINUX - GeeksforGeeks

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

It can be used to find out the disk space occupied by a par ticular director y in case of

transferring files from one computer to another.

du command can be linked with pipes to filters.A filter is usually a specialized

program that transforms the data in a meaningful way.

There also exists some other ways like df command to find the disk usage but they all

lack du ability to show the disk usage of individual directories and files.

It can also be used to find out quickly the number of sub-directories present in a

director y.

Example of using du with filters

Let ’s take a simple example of using du with sor t command so that the output produced

by du will be sor ted 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
We use cookies to ensure you have the best browsing experience on our website. By using our Got It !
4 you acknowledge
site, kartik/thakral.png
that you have read and understood our Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/du-command-linux/ 8/12
7/24/2021 du Command in LINUX - GeeksforGeeks

4 kartik/thakral
8 kartik/kt.txt
24 kartik

/* now the output displayed


is sorted according to the size */

The sor t 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.

Like 0

Previous Next

ADVERTISEMENT BY ADRECOVER

We use cookies to ensure you have the best browsing experience on our website. By using our Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/du-command-linux/ 9/12
7/24/2021 du Command in LINUX - GeeksforGeeks

RECOMMENDED ARTICLES Page : 1 2 3

How to find time taken by a 'dd' command in Linux


01 05
command/program on Linux Shell? 16, Jun 17

12, Apr 16

'IPCS' command in Linux with


mindepth and maxdepth in Linux 06
examples
02
find() command for limiting search 21, Jun 17

to a specific directory.
28, May 17

select command in Linux with


07
Netstat command in Linux examples
03 30, May 17
09, Jan 19

Practical applications of 'ls' Sed Command in Linux/Unix with


04 08
command in Linux examples
04, Jun 17 21, Aug 17

Ar ticle Contributed By :

Kartik Thakral
@Kartik Thakral

Vote for difficulty

Easy Normal Medium Hard Expert

Article Tags : linux-command, Linux-file-commands, Linux-Unix


We use cookies to ensure you have the best browsing experience on our website. By using our Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/du-command-linux/ 10/12
7/24/2021 du Command in LINUX - GeeksforGeeks

Report Issue
Improve Article

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.

Load Comments

ADVERTISEMENT BY ADRECOVER

5th Floor, A-118,


Sector-136, Noida, Uttar Pradesh - 201305
[email protected]

Company Learn
About Us Algorithms
Careers Data Structures
Privacy Policy Languages
Contact Us CS Subjects
Copyright Policy Video Tutorials

Practice Contribute
Courses Write an Article
Company-wise Write Interview Experience
Topic-wise Internships
How
We use cookies to ensure youtohave
begin?
the best browsing experience on our website. By using ourVideos Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/du-command-linux/ 11/12
7/24/2021 du Command in LINUX - GeeksforGeeks

@geeksforgeeks , Some rights reserved

We use cookies to ensure you have the best browsing experience on our website. By using our Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/du-command-linux/ 12/12

You might also like