Posted by Steve on Fri 20 May 2005 at 07:26
If you don't wish to be suprised when you suddenly run out of disk space on your system(s) it's a good idea to regularly monitor how much free space you have, and which directories and files are taking up the most space.
The basic way to check on your current disk space is via the df command, where df stands for "disk free".
There are several ways you can have the output displayed, from the default:
skx@mystery:~$ df Filesystem 1K-blocks Used Available Use% Mounted on /dev/hda1 256445 125412 117351 52% / tmpfs 242112 4 242108 1% /dev/shm /dev/hda9 30526204 19487308 9488216 68% /home /dev/hda8 366015 8326 338161 3% /tmp /dev/hda5 4806048 990076 3571836 22% /usr /dev/hda6 2883672 610948 2126240 23% /var
To make this more readable you can add the '-h', or "--human-readable" option:
skx@mystery:~$ df -h Filesystem Size Used Avail Use% Mounted on /dev/hda1 251M 123M 115M 52% / tmpfs 237M 4.0K 237M 1% /dev/shm /dev/hda9 30G 19G 9.1G 68% /home /dev/hda8 358M 8.2M 331M 3% /tmp /dev/hda5 4.6G 967M 3.5G 22% /usr /dev/hda6 2.8G 597M 2.1G 23% /var
Both of these commands will list the same information for each mounted partition, or device.
An alternative to the standard df command is the di package. Once it's installed (via apt-get install di) you can invoke it without any arguments to see a basic overview:
root@sun:~# di Filesystem Mount Megs Used Avail %used fs Type /dev/sda1 / 8450.3 1381.8 6639.3 21% ext3 tmpfs /dev/shm 250.8 0.0 250.8 0% tmpfs /dev/sdb7 /home 34181.3 14406.5 18038.5 47% ext3
di is very customizable. For example you can make the display use Gb instead of Mb for its output:
root@sun:~# di -g Filesystem Mount Gigs Used Avail %used fs Type /dev/sda1 / 8.3 1.3 6.5 21% ext3 tmpfs /dev/shm 0.2 0.0 0.2 0% tmpfs /dev/sdb7 /home 33.4 14.1 17.6 47% ext3
You can also specify which columns should be specified (such as "-A" to show all available columns) with the use of a format string.
A format string basically specifies which fields you wish to see, with codes replaced by the output. To specify the format string you use "-f", then the list of columns you wish to include from the list of available ones.
The formats are described in the man page which you can read by running "man di" but as a quick example you can see the mounted filesystems and the percentage used with the following command:
root@sun:~# di -f m2 Mount %used / 17% /dev/shm 0% /home 44%
Once you've discovered how much space you have available the next most common thing you'll wish to do is see how much space a given directory or file is using.
The du, (or "disk usage"), command will show you how much space a file or directory is occupying:
root@sun:~# du --human-readable 12K ./.gnupg 4.0K ./tmp 4.0K ./public_html 8.0K ./.ssh 4.0K ./bin 32K ./.irssi 100K .
As you can see the "--human-readable", or '-h' flag works here as it did for the "df" command.
To find the biggest files or directories beneath the current one you can use the following command to produce a sorted list:
skx@mystery:~/Programs$ du -ah | sort -rn 940K ./buildd 936K ./buildd/wanna-build 900K ./GNUMP3d/old/website/screenshots 896K ./GNUMP3d/Webpages/gnump3d/screenshots 896K ./GNUMP3d/old/software/gnump3d/screenshots 824K ./GNUMP3d/Program/gnump3d/gnump3d-2.9.4.zip 820K ./Shellcode-Exploitation-Book 816K ./Shellcode-Exploitation-Book/Source_Files 780K ./HTMLPP/htmlpp-4.2a 704K ./altermime-0.3.4 .... ... .... ... etc
Add "| head" to only show the top few lines :
skx@mystery:~$ du -ah | sort -rn | head
The total size of directory can be found by using the either the '-s' or "--summarize" opton:
skx@mystery:~$ du --summarize --human-readable 4.8G .
Or for file or directory in the current one:
skx@mystery:~$ du --summarize --human-readable * 13M Archive 104K bin 667M debian 1.2M debian-administration 1.5G Images 470M Mail 55M Programs 69M public_html 1.2M ROMS 18M sarge 7.1M Text 252M tmp 381M Videos 301M Web
Thankfully you don't need to type out most of the options to df or du as bash's tab completion will fill them out for you easily.
If you install the logwatch package you'll receive a daily email of all the "interesting" messages from your system - as well as a summery of your available disk space. This can be useful if you have a lot of machines and don't wish to be suprised in the event of a full disk
There are also a large number of scripts available online which will send you an email when a partition is reaching a state of fullness - which you can schedule to run regularly via cron if you wish.