Disc space issues and purging of files

Hi All,

I am looking forward to create a unix shell script to purge the files.

The requirement is:

1) Do df -k and check the current space occupied for the /a1 folder.
2) If the space consumed is greater than 90 %, delete all the DEF* files from a subfolder /a1/archive.

Example:

df -k results in:

/dev/lv02         4194304    412292   91%    94883    10% /a1

Since the space is consumed > 90 % (91% in this case), the following files needs to be deleted:

/a1/ARCHIVE/DEF111.txt
/a1/ARCHIVE/DEF112.txt
/a1/ARCHIVE/DEF113.txt
/a1/ARCHIVE/DEF114.txt
/a1/ARCHIVE/DEF115.txt
/a1/ARCHIVE/DEF116.txt

Thanks in advance!

Ok, can you show us what you have tried?

Since I have no prior unix experience, I have no script composed as of now.

You can try something like below:

 
used=$( df -k "/a1" | awk 'NR > 1 { sub("%",X,$4); print $4 } ' )       # Get used percentage for folder: a1
 
if [ $used -gt 90 ]                                                     # Check if it is greater than 90
then
        echo rm -f /a1/ARCHIVE/DEF*.txt                                 # If yes, remove all /a1/ARCHIVE/DEF*.txt files
fi
 

Note: Remove highlighted echo if o/p looks good.

Thanks Bipin!
I would test this script.