Finding out disk space usage using Linux shell

January 28th, 2012

Linux command line program

Finding out your disk space usage using the shell or command line in Linux is easy once you know how. This article takes a look at the different commands that tell you your disk space usage at different levels of the file system i.e. from entire disk volumes/partitions all the way down to individual files and directories.

Partition or volume based disk usage

Finding out how much disk space is being used on the various partitions on your Linux powered computer is easily done with the df command:

abdussamad@homebase:~> df
Filesystem           1K-blocks      Used Available Use% Mounted on
rootfs                10317828   8244044   1549668  85% /
devtmpfs               1999012       184   1998828   1% /dev
tmpfs                  2005828      1924   2003904   1% /dev/shm
/dev/sda5             10317828   8244044   1549668  85% /
/dev/sda6            342672260 147194084 178071400  46% /home
/dev/sda1            317332444 188407808 128924636  60% /win

The output tells you the various mount points and the percentage space used but the units of measurement used are not very user friendly. To get more human readable output use the -h switch:

abdussamad@homebase:~> df -h
Filesystem            Size  Used Avail Use% Mounted on
rootfs                9.9G  7.9G  1.5G  85% /
devtmpfs              2.0G  184K  2.0G   1% /dev
tmpfs                 2.0G  1.9M  2.0G   1% /dev/shm
/dev/sda5             9.9G  7.9G  1.5G  85% /
/dev/sda6             327G  141G  170G  46% /home
/dev/sda1             303G  180G  123G  60% /win

Now I can see that I have 1.5GB available in the root partition. It’s a lot more user friendly.

Disk space use of directories or folders

The disk space used by folders can be found using the du command. Just running du will cause it to traverse the entire directory tree from the current point onwards. So its better to restrict output using the –max-depth switch:

 
abdussamad@homebase:~/public_html/myweb2/wp2> du --max-depth=1 
6748    ./wp-includes
12220   ./wp-content
4008    ./wp-admin
23180   .

To get more human friendly output you use the -h switch again:

abdussamad@homebase:~/public_html/myweb2/wp2> du --max-depth=1 -h
6.6M    ./wp-includes
12M     ./wp-content
4.0M    ./wp-admin
23M     .

If you just want the total space used by the current directory you do a -sh:

abdussamad@homebase:~/public_html/myweb2/wp2> du -sh .
23M     .

File level disk space usage

The disk space used by individual files can be found using the long format of the ls command:

abdussamad@homebase:~/public_html/myweb2/wp2> ls -l
total 204
-rw-r--r-- 1 abdussamad users   397 May 26  2008 index.php
-rw-r--r-- 1 abdussamad users 16899 Jun  8  2011 license.txt
-rw-r--r-- 1 abdussamad users  9200 Oct 11 04:50 readme.html
-rw-r--r-- 1 abdussamad users  4268 Oct 20 19:40 wp-activate.php
drwxr-xr-x 9 abdussamad users  4096 Dec 13 03:30 wp-admin
.
.
.

For file sizes in human friendly measurement units you use the -h switch as before:

abdussamad@homebase:~/public_html/myweb2/wp2> ls -lh
total 204K
-rw-r--r-- 1 abdussamad users  397 May 26  2008 index.php
-rw-r--r-- 1 abdussamad users  17K Jun  8  2011 license.txt
-rw-r--r-- 1 abdussamad users 9.0K Oct 11 04:50 readme.html
-rw-r--r-- 1 abdussamad users 4.2K Oct 20 19:40 wp-activate.php
drwxr-xr-x 9 abdussamad users 4.0K Dec 13 03:30 wp-admin

Conclusion

I hope I’ve covered all the disk space related command line utilities that come with a typical Linux distribution. If there is a program that I missed please let me know in the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *