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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find files to match a certain criteria the delete them. 5

Status
Not open for further replies.

stla

IS-IT--Management
Mar 12, 2003
190
DE
SGI IRIX

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
 
The problem is the -name statement - in short it cant take two patterns.

Try
Code:
find /path/to/dir ( -name "*.bak" -o -name "*.txt" ) -chtime +1 -exec rm {} \;
BTW it's more efficient to use
Code:
find /path/to/dir ( -name "*.bak" -o -name "*.txt" ) -chtime +1 | xargs rm
as the find opens an instance of rm for every file whereas xargs does them all at once.

On the internet no one knows you're a dog

Columb Healy
 
You have to quote the parens from the shell:
Code:
find /path/to/directory -ctime +1 -type f \( -name '*.bak' -o -name '*.txt' \) | xargs rm -f

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I'd recommend using -mtime (last modified) rather than -ctime (last metadata change/inode update).

Nice one stefan, when did that feature appear? It's not in GNU find 4.1.20[/b] anyway...

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top