Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Command for finding and deleting files more than a day old. 1

Status
Not open for further replies.

stla

IS-IT--Management
Mar 12, 2003
190
DE
I would like to write a small script that would look for backup and text files in a specific directory that are older than 1 day and the delete these files.

Can anyone help me with my script?

This is what I have put together:

find /path/to/directory -name *.bak *.txt -ctime +1 -exec rm {};

However there is something wrong with my script.

Any ideas?

Best regards
 
You need to "escape" the semicolon, or the shell will think it's a delimiter for a second command. You also need to quote anything with an asterisk. In other words, try this...
Code:
find /path/to/directory \( -name '*.bak' -o -name '*.txt' \) -ctime +1 -exec rm {} \;
Also, you might want to use mtime instead of ctime. That way if a file is being used and modified, it won't be deleted (till a day has passed).

 
Thanks very much!

Would I be correct in thinking that the -o flag means something like 'either or'?

Best regards
 
It is a logical "OR". So if the name matches '*.bak' OR '*.txt', it will then go on to test the ctime. If it is greater than one day, it will then do the "rm" to delete the file.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top