du command in Linux with examples

du command, short for disk usage, is used to estimate file space usage.
The du command can be used to track the files and directories which are consuming excessive amount of space on hard disk drive.

Syntax :

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

Examples :



du /home/mandeep/test

Output:

44    /home/mandeep/test/data
2012    /home/mandeep/test/system design
24    /home/mandeep/test/table/sample_table/tree
28    /home/mandeep/test/table/sample_table
32    /home/mandeep/test/table
100104    /home/mandeep/test

Options :

-0, –null : end each output line with NULL
-a, –all : write count of all files, not just directories
–apparent-size : print apparent sizes, rather than disk usage.
-B, –block-size=SIZE : scale sizes to SIZE before printing on console
-c, –total : produce grand total
-d, –max-depth=N : print total for directory only if it is N or fewer levels below command line argument
-h, –human-readable : print sizes in human readable format
-S, -separate-dirs : for directories, don’t include size of subdirectories
-s, –summarize : display only total for each directory
–time : show time of last modification of any file or directory.
–exclude=PATTERN : exclude files that match PATTERN

Command usage examples with options :

  1. If we want to print sizes in human readable format(K, M, G), use -h option
    du -h /home/mandeep/test 
    
    Output:
    
    44K    /home/mandeep/test/data
    2.0M    /home/mandeep/test/system design
    24K    /home/mandeep/test/table/sample_table/tree
    28K    /home/mandeep/test/table/sample_table
    32K    /home/mandeep/test/table
    98M    /home/mandeep/test
    
  2. Use -a option for printing all files including directories.
    du -a -h /home/mandeep/test
    

    Output:
    This is partial output of above command.

    4.0K    /home/mandeep/test/blah1-new
    4.0K    /home/mandeep/test/fbtest.py
    8.0K    /home/mandeep/test/data/4.txt
    4.0K    /home/mandeep/test/data/7.txt
    4.0K    /home/mandeep/test/data/1.txt
    4.0K    /home/mandeep/test/data/3.txt
    4.0K    /home/mandeep/test/data/6.txt
    4.0K    /home/mandeep/test/data/2.txt
    4.0K    /home/mandeep/test/data/8.txt
    8.0K    /home/mandeep/test/data/5.txt
    44K    /home/mandeep/test/data
    4.0K    /home/mandeep/test/notifier.py
    
  3. Use -c option to print total size
    du -c -h /home/mandeep/test
    

    Output:

    44K    /home/mandeep/test/data
    2.0M    /home/mandeep/test/system design
    24K    /home/mandeep/test/table/sample_table/tree
    28K    /home/mandeep/test/table/sample_table
    32K    /home/mandeep/test/table
    98M    /home/mandeep/test
    98M    total
    
  4. To print sizes till particular level, use -d option with level no.
    du -d 1 /home/mandeep/test
    

    Output:

    44    /home/mandeep/test/data
    2012    /home/mandeep/test/system design
    32    /home/mandeep/test/table
    100104    /home/mandeep/test
    

    Now try with level 2, you will get some extra directories

    du -d 2 /home/mandeep/test
    

    Output:

    44    /home/mandeep/test/data
    2012    /home/mandeep/test/system design
    28    /home/mandeep/test/table/sample_table
    32    /home/mandeep/test/table
    100104    /home/mandeep/test
    
  5. Get summary of file system using -s option
    du -s /home/mandeep/test
    

    Output:

    100104    /home/mandeep/test
    
  6. Get the timestamp of last modified using --time option
    du --time -h /home/mandeep/test
    

    Output:

    44K    2018-01-14 22:22    /home/mandeep/test/data
    2.0M    2017-12-24 23:06    /home/mandeep/test/system design
    24K    2017-12-30 10:20    /home/mandeep/test/table/sample_table/tree
    28K    2017-12-30 10:20    /home/mandeep/test/table/sample_table
    32K    2017-12-30 10:20    /home/mandeep/test/table
    98M    2018-02-02 17:32    /home/mandeep/test
    

- Mandeep Singh

References :
1) du wikipedia
2) du man entry



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai



Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.