Posts Tagged ‘blank space’

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

Sunday, June 18th, 2006

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/sh
 
# Source of all evil
DIR=/ftp
# Age of file in days
AGE=10
# 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.
EXCLUDE="lost+found|incoming"
 
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
done
 
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).