Results 1 to 6 of 6
hi all
I've got a shell script which suppose to delete files older then 7 days.
Unfortunatly it doesn't work.
here is the script:
#---------------------------------------
# Configration Variables
#
HOSTNAME=`hostname`
...
- 11-14-2007 #1Just Joined!
- Join Date
- Oct 2007
- Posts
- 16
Deleting files older then 7 days
hi all
I've got a shell script which suppose to delete files older then 7 days.
Unfortunatly it doesn't work.
here is the script:
#---------------------------------------
# Configration Variables
#
HOSTNAME=`hostname`
BACKUP_DIR=/mysql/backup
BACKUP_DAYS=7
find $BACKUP_DIR -name ${HOSTNAME}* -mtime +${BACKUP_DAYS} -print -exec rm -f {} \;
I run it as root.
Can anyone tell me why it doesn't work.
Thanks
Any help appreciated
- 11-15-2007 #2
Well, what exactly doesn't work about it?
If you run it by hand, does it work? Are you sure that there are files older than 7 days in the directory? Also, consider using "-delete" instead of the "-exec" you have.
One thing to consider would be surrounding the * in single quotes. The way you have it now, the pattern expansion is being performed by the shell. From the find man page:
Code:NON-BUGS $ find . -name *.c -print find: paths must precede expression Usage: find [-H] [-L] [-P] [path...] [expression] This happens because *.c has been expanded by the shell resulting in find actually receiving a command line like this: find . -name bigram.c code.c frcode.c locate.c -print That command is of course not going to work. Instead of doing things this way, you should enclose the pattern in quotes or escape the wild- card: $ find . -name \*.c -printDISTRO=Arch
Registered Linux User #388732
- 11-15-2007 #3Just Joined!
- Join Date
- Oct 2007
- Posts
- 15
- 11-15-2007 #4Just Joined!
- Join Date
- Oct 2007
- Posts
- 16
Sorry Guys
THE SCRIPT DOES WORK

I had files from nov 6th and on and thought the script would delete them on the 13th or 14th. since they are more than 7 days.
It did delete them this morning.
But the weird thing is it deleted nov 6th and nov 7th files.
I'm still new to linux and learning as I go along.
Valery and Cabhan thank you for your responses.
- 11-15-2007 #5Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
- 11-15-2007 #6Linux User
- Join Date
- Jun 2007
- Posts
- 318
You don't have to escape the {}. Your -name field would have to have the * escaped if you didn't enclose it with "s.
"${HOSTNAME}*"
or
${HOSTNAME}\*
Vic


Reply With Quote
