PDA

View Full Version : Create a script to rmove oldest files


disco
07-14-2008, 03:08 PM
ok, I have never written a shell script before. What I need to do is remove the oldest files from a known directory when the hard disk is above a certain size, so that the hard disk never becomes full and the oldest data removed.

Any help would be grateful and even better if someone can code me the script :D

jggimi
07-14-2008, 04:04 PM
http://www.daemonforums.org/showthread.php?t=651

disco
07-14-2008, 05:59 PM
thanks for the link, but this seems to only remove files over n days. What I need to do is when the hard disk is greater the n percent full, remove the oldest files from the directory until the disk is lower than n percent full. Basically a FIFO.

ocicat
07-14-2008, 06:41 PM
thanks for the link, but this seems to only remove files over n days. What I need to do is when the hard disk is greater the n percent full, remove the oldest files from the directory until the disk is lower than n percent full.
Here are some hints you can use to help yourself:

The following tutorial is a good introduction to shell programming:

http://steve-parker.org/sh/sh.shtml
To determine whether a partition is more than some pre-determined percentage full, look the output of df & think about how to isolate a value in a particular column (HINT: with awk, this is easy...).
As for how to determine how old files are, look at the manpage for ls.

The link provided by jggimi will help you ferret out the details needed to set up cron which can automatically call your script on whatever periodic basis you want.

ai-danno
07-14-2008, 09:27 PM
Any help would be grateful and even better if someone can code me the script :D

Welcome to daemonforums! I see that this is your first posting here.

In the link provided by jggimi is a post that relates to finding files by age (by BSDFan666)... you may want to take another look at that.

TerryP
07-14-2008, 10:25 PM
because AWK is it's own language as well as a program (awk), I'll add to ocicats post by noting the easiest way to get a specific column out of 'df' output with awk.


df | awk '{ print $1 }'


which will print the first column to standard output, $2 the second column, $3 the third column and so on.


Accessing the column(s) a line at a time is also easy as pie but I'll let you figure that out along with more useful things ;-)