Ten Simple Commands to Check Your Disk Space

May 19, 2016 / General Discussion

The ‘du’ command in Linux is a standard Unix/Linux command to check the capacity of the disk used by files and directories on a machine. There are several parameter options in which the du command utilized to get the results in many formats. Additionally, the du command used in a recurrent manner.

Below are the ten useful ‘du’ commands with their examples. That will help you to calculate the disk space occupied by the files and directories in Linux.

  1. To calculate the disk space summary of a /home/bodhost directory tree along with its sub-directories, enter the below command –
    [root@bodhost]# du  /home/bodhost

    40     /home/bodhost/downloads
    4        /home/bodhost/.mozilla/plugins
    4        /home/bodhost/.mozilla/extensions
    12       /home/bodhost/.mozilla
    12       /home/bodhost/.ssh
    689112   /home/bodhost/Ubuntu-12.10
    689360   /home/bodhost
  2. The output displays the number of disk blocks in the /home/bodhost directory as well as its sub-directories.

    If you want the results in “Human Readable Format”, use the “-h” option with the “du” command. With this command, you will see the sizes in Bytes, Kilobytes, Megabytes, Gigabytes, Terabytes, etc.

    [root@bodhost]# du -h /home/bodhost

    40K        /home/bodhost/downloads
    4.0K       /home/bodhost/.mozilla/plugins
    4.0K       /home/bodhost/.mozilla/extensions
    12K        /home/bodhost/.mozilla
    12K        /home/bodhost/.ssh
    673M       /home/bodhost/Ubuntu-12.10
    674M       /home/bodhost
  3. For get the summary of a grand total disk usage size of a directory. The option “-s” can be used as follows –
    [root@bodhost]# du -sh /home/bodhost

    674M    /home/bodhost
  4. You can use “-a” flag with “du” command for displaying the files and directories’ usage.
    [root@bodhost]# du -a /home/bodhost

    4         /home/bodhost/.bash_logout
    12        /home/bodhost/downloads/uploadprogress-1.0.3.1.tgz
    24        /home/bodhost/downloads/Phpfiles-org.tar.bz2
    40        /home/bodhost/downloads
    12        /home/bodhost/uploadprogress-1.0.3.1.tgz
    4         /home/bodhost/.mozilla/plugins
    4         /home/bodhost/.mozilla/extensions
    12        /home/bodhost/.mozilla
    4         /home/bodhost/.bashrc
    689108    /home/bodhost/Ubuntu-12.10/ubuntu-12.10-server-i386.iso
    689112    /home/bodhost/Ubuntu-12.10
    689360    /home/bodhost
  5. If you use the “-a” flag with “-h”, you will get the output in a human-readable format i.e. Kilobytes, Megabytes, etc.
    [root@bodhost]# du -ah /home/bodhost
    
    4.0K      /home/bodhost/.bash_logout
    12K       /home/bodhost/downloads/uploadprogress-1.0.3.1.tgz
    24K       /home/bodhost/downloads/Phpfiles-org.tar.bz2
    40K       /home/bodhost/downloads
    12K       /home/bodhost/uploadprogress-1.0.3.1.tgz
    4.0K      /home/bodhost/.mozilla/plugins
    4.0K      /home/bodhost/.mozilla/extensions
    12K       /home/bodhost/.mozilla
    4.0K      /home/bodhost/.bashrc
    673M      /home/bodhost/Ubuntu-12.10/ubuntu-12.10-server-i386.iso
    673M      /home/bodhost/Ubuntu-12.10
    674M      /home/bodhost 
  6. To find the disk usage of a directory tree with its sub-trees in Kilobyte blocks, use the “-k”. Note: the size displayed in 1024 bytes units.
    [root@bodhost]# du -k /home/bodhost
    
    40          /home/bodhost/downloads
    4           /home/bodhost/.mozilla/plugins
    4           /home/bodhost/.mozilla/extensions
    12          /home/bodhost/.mozilla
    12          /home/bodhost/.ssh
    689112      /home/bodhost/Ubuntu-12.10
    689360      /home/bodhost 
  7. To get the information about disk usage of the directory tree and its sub-trees in Megabytes (MB). Only, use the “-mh” as follows. The “-m” flag calculates the blocks in MB units and “-h” stands for human-readable format.
    [root@bodhost]# du -mh /home/bodhost
    
    40K     /home/bodhost/downloads
    4.0K    /home/bodhost/.mozilla/plugins
    4.0K    /home/bodhost/.mozilla/extensions
    12K     /home/bodhost/.mozilla
    12K     /home/bodhost/.ssh
    673M    /home/bodhost/Ubuntu-12.10
    674M    /home/bodhost 
  8. If you need to calculate the grand total usage of disk space at the last line, use the “-c” flag. For example, if your directory has required 674MB space than the last two lines of the output will be as below –
    [root@bodhost]# du -ch /home/bodhost
    
    40K     /home/bodhost/downloads
    4.0K    /home/bodhost/.mozilla/plugins
    4.0K    /home/bodhost/.mozilla/extensions
    12K     /home/bodhost/.mozilla
    12K     /home/bodhost/.ssh
    673M    /home/bodhost/Ubuntu-12.10
    674M    /home/bodhost
    674M    total 
  9. If you want to exclude the “.txt” files while calculating the total directory size, use the flag “-exclude” and the output will be seen as below –
    [root@tecmint]# du -ah --exclude="*.txt" /home/tecmint
    
    4.0K    /home/bodhost/.bash_logout
    12K     /home/bodhost/downloads/uploadprogress-1.0.3.1.tgz
    24K     /home/bodhost/downloads/Phpfiles-org.tar.bz2
    40K     /home/bodhost/downloads
    12K     /home/bodhost/uploadprogress-1.0.3.1.tgz
    4.0K    /home/bodhost/.bash_history
    4.0K    /home/bodhost/.bash_profile
    4.0K    /home/bodhost/.mozilla/plugins
    4.0K    /home/bodhost/.mozilla/extensions
    12K     /home/bodhost/.mozilla
    4.0K    /home/bodhost/.bashrc
    24K     /home/bodhost/Phpfiles-org.tar.bz2
    4.0K    /home/bodhost/geoipupdate.sh
    4.0K    /home/bodhost/.zshrc
    120K    /home/bodhost/goaccess-0.4.2.tar.gz.1
    673M    /home/bodhost/Ubuntu-12.10/ubuntu-12.10-server-i386.iso
    673M    /home/bodhost/Ubuntu-12.10
    674M    /home/bodhost
  10. Use the flag “-time” for displaying the disk usage based on time modification as below –
    [root@tecmint]# du -ha --time /home/tecmint
    
    4.0K    2012-10-12 22:32        /home/bodhost/.bash_logout
    12K     2013-01-19 18:48        /home/bodhost/downloads/uploadprogress-1.0.3.1.tgz
    24K     2013-01-19 18:48        /home/bodhost/downloads/Phpfiles-org.tar.bz2
    40K     2013-01-19 18:48        /home/bodhost/downloads
    12K     2013-01-19 18:32        /home/bodhost/uploadprogress-1.0.3.1.tgz
    4.0K    2012-10-13 00:11        /home/bodhost/.bash_history
    4.0K    2012-10-12 22:32        /home/bodhost/.bash_profile
    0       2013-01-19 18:32        /home/bodhost/xyz.txt
    0       2013-01-19 18:32        /home/bodhost/abc.txt
    4.0K    2012-10-12 22:32        /home/bodhost/.mozilla/plugins
    4.0K    2012-10-12 22:32        /home/bodhost/.mozilla/extensions
    12K     2012-10-12 22:32        /home/bodhost/.mozilla
    4.0K    2012-10-12 22:32        /home/bodhost/.bashrc
    24K     2013-01-19 18:32        /home/bodhost/Phpfiles-org.tar.bz2
    4.0K    2013-01-19 18:32        /home/bodhost/geoipupdate.sh
    4.0K    2012-10-12 22:32        /home/bodhost/.zshrc
    120K    2013-01-19 18:32        /home/bodhost/goaccess-0.4.2.tar.gz.1
    673M    2013-01-19 18:51        /home/bodhost/Ubuntu-12.10/ubuntu-12.10-server-i386.iso
    673M    2013-01-19 18:51        /home/bodhost/Ubuntu-12.10
    674M    2013-01-19 18:52        /home/bodhost