Web Hosting

Ten Simple Commands to Check Your Disk Space

Disk Usage

The ‘du’ command in Linux is a standard Unix/Linux command to check the capacity of disk used by files and directories on a machine. There are several parameter options in which the du command can be utilized to get the results in many formats. Additionally, the du command can be 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 –
[[email protected]]# 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

The output displays the number of disk blocks in the /home/bodhost directory as well as its sub-directories.

  1. If you want the results in “Human Readable Format”, use “-h” option with “du” command. With this command you will see the sizes in Bytes, Kilobytes, Megabytes , Gigabytes, Terabytes, etc.
 
[[email protected]]# 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
  1. For getting the summary of a grand total disk usage size of an directory, the option “-s” can be used as follows –
 
[[email protected]]# du -sh /home/bodhost

674M    /home/bodhost 
  1. You can use “-a” flag with “du” command for displaying the files and directories’ usage.
 
[[email protected]]# 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 “-a” flag with “-h”, you will get the output in a human readable format i.e. Kilobytes, Megabytes, etc.

 
[[email protected]]# 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 will be displayed in 1024 bytes units.

 
[[email protected]]# 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 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.

 
[[email protected]]# 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 “-c” flag. For example, your directory has required 674MB space then the last two lines of the output will be as below –

 
[[email protected]]# 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 
  1. 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 –
 
[[email protected]]# 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
  1. Use the flag “-time” for displaying the disk usage based on time modification as below –
 
[[email protected]]# 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