Knowledge Base/Server Administration

How do I check disk usage?

posted this on May 24, 2012, 11:50 AM

So, you'd like to see your disk usage, huh? To do so, we'll need to use SSH. Need help connecting?

Want to see overall disk usage?

Certainly! Linux includes the "df" command to show usage. Let's use the following command:

df -h

The -h switch makes the output "human readable," so instead of the actual total number of bytes, you will see the output in megabytes or gigabytes. The output, depending on distro, will be something like:

Filesystem            Size  Used Avail Use% Mounted on
/dev/xvda1             20G  701M   19G   4% /
none                  367M  128K  367M   1% /dev
none                  372M     0  372M   0% /dev/shm
none                  372M   44K  372M   1% /var/run
none                  372M     0  372M   0% /var/lock
none                  372M     0  372M   0% /lib/init/rw

Th very first output there is /dev/xvda1. This is the overall usage of your server. The rest of them are the various directories required by the operating system.

Want to break down directories and their specific usage?

We'll want to use the "du" command to check usage in each of our directories located in /. So, use sudo or obtain root and visit the / directory (cd /). You can use the following command to see your disk usage:

du -h --max-depth=1

"du" is the main command to give a disk space break down. The "-h" switch makes the output human readable (described earlier). The "--max-depth=1" switch tells the command to just give the report for one level deep. The space for all folders below that level are still counted, but the sub folders themselves are not displayed.

When the command is run, you would see something displayed like the following:

3.8M ./bin
2.0K ./boot
30K ./dev
3.7M ./etc
9.0K ./home
1.0K ./initrd
16M ./lib
1.0K ./mnt
1.0K ./opt
39K ./package
0 ./proc
191M ./root
5.0M ./sbin
1.0K ./selinux
1.0K ./sys
1.0K ./tmp
1.0K ./tmp_logrotate
972M ./usr
255M ./var
1.5G . 

The last line will give you the total space used for all folders it counted. In this case, the total disk space usage on this server right now is 1.5GB. Of that amount, 972MB of space can be found in the /usr folder.

So lets say we think that amount is a little high and want to find out a little more detail about the usage inside that folder. So switch to the /usr folder.

cd /usr

Run the earlier command again to see the usage inside of that folder.

du -h --max-depth=1

You should then see the space inside of the /usr folder.

6.7M ./X11R6
98M ./bin
1.0K ./etc
1.0K ./games
19M ./include
3.0K ./java
37K ./kerberos
390M ./lib
39M ./libexec
26M ./local
21M ./sbin
375M ./share
14K ./src
972M .

The above commands can be used for finding the space usage of any folder and the files below it. Just switch to that directory using the "cd" command and use du to check.

It is very, very important to note that just because a directory is using a lot of space, that does NOT mean there is an issue or the directory and files inside it need to be removed. Removing files and folders without regard to what they may be doing is similar to just removing system files in Windows. It's a quick way to hose your server!

Questions? Ask us! We're always happy to help!

Topic is closed for comments