Roy Roy - 5 years ago 105
Bash Question

Newbie needs help with Bash Scripting : Shell script to delete files when disk is full

I am writing a small little script to clear space on my linux everyday via CRON if the cache directory grows too large.
Since I am really green at bash scripting, I will need a little bit of help from you linux gurus out there.

Here is basically the logic (pseudo-code)

if ( Drive Space Left < 5GB )
{
change directory to '/home/user/lotsa_cache_files/'

if ( current working directory = '/home/user/lotsa_cache_files/')
{
delete files in /home/user/lotsa_cache_files/
}
}


Getting drive space left



I plan to get the drive space left from the '/dev/sda5' command.
If returns the following value to me for your info :

Filesystem 1K-blocks Used Available Use% Mounted on<br>
/dev/sda5 225981844 202987200 11330252 95% /


So a little regex might be necessary to get the '11330252' out of the returned value

A little paranoia



The 'if ( current working directory = /home/user/lotsa_cache_files/)' part is just a defensive mechanism for the paranoia within me. I wanna make sure that I am indeed in '/home/user/lotsa_cache_files/' before I proceed with the delete command which is potentially destructive if the current working directory is not present for some reason.

Deleting files



The deletion of files will be done with the command below instead of the usual rm -f:

find . -name "*" -print | xargs rm


This is due to the inherent inability of linux systems to 'rm' a directory if it contains too many files, as I have learnt in the past.

Thanks in advance, guys!

Roy Teo

Answer Source

Just another proposal (comments within code):

FILESYSTEM=/dev/sda1 # or whatever filesystem to monitor
CAPACITY=95 # delete if FS is over 95% of usage 
CACHEDIR=/home/user/lotsa_cache_files/

# Proceed if filesystem capacity is over than the value of CAPACITY (using df POSIX syntax)
# using [ instead of [[ for better error handling.
if [ $(df -P $FILESYSTEM | awk '{ gsub("%",""); capacity = $5 }; END { print capacity }') -gt $CAPACITY ]
then
    # lets do some secure removal (if $CACHEDIR is empty or is not a directory find will exit
    # with error which is quite safe for missruns.):
    find "$CACHEDIR" --maxdepth 1 --type f -exec rm -f {} \;
    # remove "maxdepth and type" if you want to do a recursive removal of files and dirs
    find "$CACHEDIR" -exec rm -f {} \;
fi 

Call the script from crontab to do scheduled cleanings

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download