Recovering lost disk space with DF and DU
So, you're running a Debian Linux server and all is going well. Suddenly, Bang! everything breaks, clients start calling asking about their email, your websites are down! Overnight your hard drive has filled up.
So how do to go about fixing this? (Note, though these instructions are tailored to Debian, they should work similarly for any flavour of Linux)
Well, the first thing to do is to find out if your hard drive is full. get a command line up and type:
You should see something like this:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hda1 255912 147070 95189 61% /
tmpfs 242272 0 242272 0% /dev/shm
/dev/hda9 69402812 901484 64975868 2% /home
/dev/hda8 369000 8495 340845 3% /tmp
/dev/hda5 4807056 561116 4001756 13% /usr
/dev/hda6 2885780 1432464 0 100% /var
tmpfs 10240 692 9548 7% /dev
Here we can see that the hda6 partition is full, in other words my /var directory. This is a good first step, but which files are using the space. Type:
du -sk * | sort -rg | less
You should see something like this:
713420 www
327916 log
180960 cache
142380 lib
776 backups
632 mail
576 spool
112 run
48 lost+found
12 named
8 tmp
8 lock
8 local
4 opt
So most of my drive space is being used by the www directory, in other words my websites. Now we need to find out specifically which files are taking up the space so type:
This will give you a list of all the files larger than 50 MB. You should get a list of large files in the /var directory. This is pretty helpful. Any large log files? these are safe to delete. Any big tar.gz files you're not using? Get rid of all the crap.
You can automate this deletion if you're feeling very brave. IMPORTANT: Only do this if you're absolutely sure what you're doing! It will delete any files over 100MB in size without warning! If you're certain you don't need them type:
IMPORTANT. Again, Check first that you dont need any of the big files! If you're in doubt do this manually.
Now do another:
and there, lots of free space for you to fill up all over again. One point, if you've deleted log files you may need to restart your services to recreate them, so for example if you're running apache2 and you've deleted apache logs you might need to do:
The same goes for postfix, etc.
As a final step you might want to set up a cron job to email your disk usage stats to you every day or so so type
09 00 * * * df | mail -s 'Server Disk Usage' This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
Last Updated ( Thursday, 16 August 2007 )















