Friday, January 19, 2018

Useful du (Disk Usage) Commands to Find Disk Usage of Files and Directories

The Linux “du” (Disk Usage) is a standard Unix/Linux command, used to check the information of disk usage of files and directories on a machine.

The du command has many parameter options that can be used to get the results in many formats.

 The du command also displays the files and directory sizes in a recursively manner.

This blog explains 10 useful “du” commands with their examples, that might helps you to find out the sizes of files and directories in Linux. 

The information provided in this blog are taken from the man pages of du command.

1. To find out the disk usage summary of a /home/manish directory tree and each of its sub directories. Enter the command as:

[root@localhost]# du  /home/manish
40      /home/manish/downloads
4       /home/manish/.mozilla/plugins
4       /home/manish/.mozilla/extensions
12      /home/manish/.mozilla
12      /home/manish/.ssh
689112  /home/manish/Ubuntu-12.10
689360  /home/manish

The output of the above command displays the number of disk blocks in the /home/manish directory along with its sub-directories.

2. Using “-h” option with “du” command provides results in “Human Readable Format“. Means you can see sizes in BytesKilobytes, MegabytesGigabytes etc.

[root@localhost]# du -h /home/manish
40K     /home/manish/downloads
4.0K    /home/manish/.mozilla/plugins
4.0K    /home/manish/.mozilla/extensions
12K     /home/manish/.mozilla
12K     /home/manish/.ssh
673M    /home/manish/Ubuntu-12.10
674M    /home/manish

3. To get the summary of a grand total disk usage size of an directory use the option “-s” as follows.

[root@localhost]# du -sh /home/manish
674M    /home/manish

4. Using “-a” flag with “du” command displays the disk usage of all the files and directories.

[root@localhost]# du -a /home/manish
4       /home/manish/.bash_logout
12      /home/manish/downloads/uploadprogress-1.0.3.1.tgz
24      /home/manish/downloads/Phpfiles-org.tar.bz2
40      /home/manish/downloads
12      /home/manish/uploadprogress-1.0.3.1.tgz
4       /home/manish/.mozilla/plugins
4       /home/manish/.mozilla/extensions
12      /home/manish/.mozilla
4       /home/manish/.bashrc
689108  /home/manish/Ubuntu-12.10/ubuntu-12.10-server-i386.iso
689112  /home/manish/Ubuntu-12.10
689360  /home/manish

5. Using “-a” flag along with “-h” displays disk usage of all files and folders in human readeable format. The below output is more easy to understand as it shows the files in KilobytesMegabytes etc.

[root@localhost]# du -ah /home/manish
4.0K    /home/manish/.bash_logout
12K     /home/manish/downloads/uploadprogress-1.0.3.1.tgz
24K     /home/manish/downloads/Phpfiles-org.tar.bz2
40K     /home/manish/downloads
12K     /home/manish/uploadprogress-1.0.3.1.tgz
4.0K    /home/manish/.mozilla/plugins
4.0K    /home/manish/.mozilla/extensions
12K     /home/manish/.mozilla
4.0K    /home/manish/.bashrc
673M    /home/manish/Ubuntu-12.10/ubuntu-12.10-server-i386.iso
673M    /home/manish/Ubuntu-12.10
674M    /home/manish

6. Find out the disk usage of a directory tree with its subtress in Kilobyte blcoks. Use the “-k” (displays size in 1024 bytes units).

[root@localhost]# du -k /home/manish
40      /home/manish/downloads
4       /home/manish/.mozilla/plugins
4       /home/manish/.mozilla/extensions
12      /home/manish/.mozilla
12      /home/manish/.ssh
689112  /home/manish/Ubuntu-12.10
689360  /home/manish

7. To get the summary of disk usage of directory tree along with its subtrees in Megabytes (MB) only. Use the option “-mh” as follows. The “-m” flag counts the blocks in MB units and “-h” stands for human readable format.

[root@localhost]# du -mh /home/manish
40K     /home/manish/downloads
4.0K    /home/manish/.mozilla/plugins
4.0K    /home/manish/.mozilla/extensions
12K     /home/manish/.mozilla
12K     /home/manish/.ssh
673M    /home/manish/Ubuntu-12.10
674M    /home/manish

8. The “-c” flag provides a grand total usage disk space at the last line. If your directory taken 674MB space, then the last last two line of the output would be.

[root@localhost]# du -ch /home/manish
40K     /home/manish/downloads
4.0K    /home/manish/.mozilla/plugins
4.0K    /home/manish/.mozilla/extensions
12K     /home/manish/.mozilla
12K     /home/manish/.ssh
673M    /home/manish/Ubuntu-12.10
674M    /home/manish
674M    total

9. The below command calculates and displays the disk usage of all files and directories, but excludes the files that matches given pattern. The below command excludes the “.txt” files while calculating the total size of diretory. So, this way you can exclude any file formats by using flag “-–exclude“. See the output there is no txtfiles entry.

[root@localhost]# du -ah --exclude="*.txt" /home/manish
4.0K    /home/manish/.bash_logout
12K     /home/manish/downloads/uploadprogress-1.0.3.1.tgz
24K     /home/manish/downloads/Phpfiles-org.tar.bz2
40K     /home/manish/downloads
12K     /home/manish/uploadprogress-1.0.3.1.tgz
4.0K    /home/manish/.bash_history
4.0K    /home/manish/.bash_profile
4.0K    /home/manish/.mozilla/plugins
4.0K    /home/manish/.mozilla/extensions
12K     /home/manish/.mozilla
4.0K    /home/manish/.bashrc
24K     /home/manish/Phpfiles-org.tar.bz2
4.0K    /home/manish/geoipupdate.sh
4.0K    /home/manish/.zshrc
120K    /home/manish/goaccess-0.4.2.tar.gz.1
673M    /home/manish/Ubuntu-12.10/ubuntu-12.10-server-i386.iso
673M    /home/manish/Ubuntu-12.10
674M    /home/manish

10. Display the disk usage based on modification of time, use the flag “–time” as shown below.


[root@localhost]# du -ha --time /home/manish
4.0K    2012-10-12 22:32        /home/manish/.bash_logout
12K     2013-01-19 18:48        /home/manish/downloads/uploadprogress-1.0.3.1.tgz
24K     2013-01-19 18:48        /home/manish/downloads/Phpfiles-org.tar.bz2
40K     2013-01-19 18:48        /home/manish/downloads
12K     2013-01-19 18:32        /home/manish/uploadprogress-1.0.3.1.tgz
4.0K    2012-10-13 00:11        /home/manish/.bash_history
4.0K    2012-10-12 22:32        /home/manish/.bash_profile
0       2013-01-19 18:32        /home/manish/xyz.txt
0       2013-01-19 18:32        /home/manish/abc.txt
4.0K    2012-10-12 22:32        /home/manish/.mozilla/plugins
4.0K    2012-10-12 22:32        /home/manish/.mozilla/extensions
12K     2012-10-12 22:32        /home/manish/.mozilla
4.0K    2012-10-12 22:32        /home/manish/.bashrc
24K     2013-01-19 18:32        /home/manish/Phpfiles-org.tar.bz2
4.0K    2013-01-19 18:32        /home/manish/geoipupdate.sh
4.0K    2012-10-12 22:32        /home/manish/.zshrc
120K    2013-01-19 18:32        /home/manish/goaccess-0.4.2.tar.gz.1
673M    2013-01-19 18:51        /home/manish/Ubuntu-12.10/ubuntu-12.10-server-i386.iso
673M    2013-01-19 18:51        /home/manish/Ubuntu-12.10
674M    2013-01-19 18:52        /home/manish

1 comment:

  1. I have similar post of du command which shows few of the new options that are missing in your articles . Have a look :



    Hello Adam,
    The theory part is well explained about ‘df and du’ command, whereas the practical examples are not much. I have a post where you can find real-time 9 practical examples. You can check the following link :

    https://www.linuxteck.com/basic-du-command-in-linux-with-examples/

    ReplyDelete

Configure NFS Client

This example is based on the environment below. +----------------------+           |           +----------------------+ | [...