Script for removing old files

Associate
Joined
19 Jul 2006
Posts
1,847
Why does'nt this work?

Code:
#/bin/sh
BACKUP_DIR="/backups"
DAYS_TO_RETAIN=10
find $BACKUP_DIR -maxdepth 1 -type f -ctime +$DAYS_TO_RETAIN -delete

Im using openSuse.
I have 3 directiories that I want to empty so theres no files older than 10 days in there
/backups/moodlesoftwere/moodlesoftwere/2009/Feb
/backups/mysql_backups/moodle/2009/Feb
/backups/moodledata/moodledata/2009/Feb

TIA
 
#/bin/sh
should be #!/bin/sh

-maxdepth 1

limits search to a depth no greater than 1 from the current directory (1 will only search the current directory)

in your example this wont check any where further than: /backups/

dont think you need maxdepth for the example directories you specified

-type f

only files, so assume you want to keep the directory structure including old/empty directories.

-ctime +$DAYS_TO_RETAIN

are you sure you want to check ctime and not mtime?
ctime is the inode change time while mtime is the file modification time.

mtime is what you normally see when you do a directory listing.

i'd use something like:

Code:
find /backups/ -mtime +10 -exec rm {} \;
 
Thanks all.
That explanation is brilliant inph, as im still learning linux. I changed the -maxdepth to 5 but i may get rid of it.

the ctime worked but i take on board the mtime comment
 
Back
Top Bottom