Quick and dirty delete old files, with exclude list and support for filenames with spaces

Here’s a little script I’ve written which deletes older than AGE days files, and has an exclude list, just in case. It’s meant to be run by cron on a daily basis:


# Source of all evil
# Age of file in days
# Exclude list - Use pipe (|) seperated values. Example:
# EXCLUDE="me|tal" for excluding both "me" and both "tal". Use the longest
# possible expression, for accurate match. For example:
# EXCLUDE="/ftp/me|/ftp/tal". Below is the default minimal exclude list.

echo -n " > /tmp/del-list.txt

find $DIR/*/* -mtime +$AGE -print | grep -vE "$EXCLUDE" | tr 'n' ""n"" >> /tmp/del-list.txt

for i in `cat /tmp/del-list.txt` ; do
echo $i >> /var/log/del-ftp.log

cat /tmp/del-list.txt | xargs rm -Rf

rm /tmp/del-list.txt

It seems to work. So far, I have delete 2nd level directories when old enough (10 days by default), and I can handle files with spaces in their names (scheduled delete of filenames with spaces – for the sake of those searching for a solution. At least, I’ve used this expression and didn’t find a solution online).

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.