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

Help with Find command 1

Status
Not open for further replies.

JAFrank

Technical User
Nov 16, 2002
84
0
0
US
Hello, I am trying to use a cron entry to clean old files out of an archive directory. I have set it up using the following command:

0,5,10,15,20,25,30,35,40,45,50,55 * * * * find /var/spool/archive -name "?f*" -mtime +1 -exec rm -f {} \;

Every 5 minutes, it checks /var/spool/archive for any files with "f" as the second character in the file name, and deletes it if is more than one day old.

This works fine, but due to the large number of files, I would like to slice it a little thinner.

Is there a way to do the same thing, but delete every file older than 4 hours, rather than a full day?

Thanks for your help!
 
0,5,10,15,20,25,30,35,40,45,50,55 * * * * find /var/spool/archive -name "?f*" -mtime +0.12 -exec rm -f {} \;

Too bad I.T. is not cash business

Luc Foata
Unix sysadmin, Oracle DBA
 
0,5,10,15,20,25,30,35,40,45,50,55 * * * * find /var/spool/archive -name "?f*" -mtime +0.16 -exec rm -f {} \;

Too bad I.T. is not cash business

Luc Foata
Unix sysadmin, Oracle DBA
 
Thank you!

I got to wondering after I posted whether it was acceptable to use fractions with mtime....Thanks for the validation!

Allen
 
Allen:

If you have a large number of files, you might consider improving the efficiency of your find statement by using xargs instead of -exec:

find /var/spool/archive -name "?f*" -mtime +1 -exec rm -f {} \;

could be:

find /var/spool/archive -name "?f*" -mtime +1 -print|xargs rm -f

Regards,

Ed
 
Interesting...

Does using xargs eliminate the need to terminate the find command (\;)?

Thanks for your help

Allen
 
Allen:

Yes. If I'm not mistakes, the {} \; means to substitute each object found and perform the -exec - rm in this example. No need for that when the output of find pipe to xargs rm eliminating exec.

xargs will shove as many files to rm as it can without overflowing the command line buffer.

Regards,

Ed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top