Saturday, 29 February 2020

du command in Linux with examples

du command, short for disk usage, is used to estimate file space usage.

du command, LPI Certifications, LPI Guides, LPI Learning, LPI Tutorials and Materials

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/xyz/test

Output:


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


Options :


du command, LPI Certifications, LPI Guides, LPI Learning, LPI Tutorials and Materials
-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 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/xyz/test

Output:

44K    /home/xyz/test/data
2.0M    /home/xyz/test/system design
24K    /home/xyz/test/table/sample_table/tree
28K    /home/xyz/test/table/sample_table
32K    /home/xyz/test/table
98M    /home/xyz/test

2. Use -a option for printing all files including directories.


du -a -h /home/xyz/test

Output:

This is partial output of above command.

4.0K    /home/xyz/test/blah1-new
4.0K    /home/xyz/test/fbtest.py
8.0K    /home/xyz/test/data/4.txt
4.0K    /home/xyz/test/data/7.txt
4.0K    /home/xyz/test/data/1.txt
4.0K    /home/xyz/test/data/3.txt
4.0K    /home/xyz/test/data/6.txt
4.0K    /home/xyz/test/data/2.txt
4.0K    /home/xyz/test/data/8.txt
8.0K    /home/xyz/test/data/5.txt
44K    /home/xyz/test/data
4.0K    /home/xyz/test/notifier.py

3. Use -c option to print total size


du -c -h /home/xyz/test

Output:

44K    /home/xyz/test/data
2.0M    /home/xyz/test/system design
24K    /home/xyz/test/table/sample_table/tree
28K    /home/xyz/test/table/sample_table
32K    /home/xyz/test/table
98M    /home/xyz/test
98M    total

4. To print sizes till particular level, use -d option with level no.


du -d 1 /home/xyz/test

Output:

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

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

du -d 2 /home/xyz/test

Output:

44    /home/xyz/test/data
2012    /home/xyz/test/system design
28    /home/xyz/test/table/sample_table
32    /home/xyz/test/table
100104    /home/xyz/test

5. Get summary of file system using -s option


du -s /home/xyz/test

Output:

100104    /home/xyz/test

6. Get the timestamp of last modified using --time option


du --time -h /home/xyz/test

Output:

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

Read More: LPI Certifications

Related Posts

0 comments:

Post a Comment