1757033 Members
2411 Online
108858 Solutions
New Discussion юеВ

find -exec rm question

 

find -exec rm question

I'm running a nightly cleanup script to clear files from a specific directory. The script works as expected, however, now I have a sub-directory which is also being cleared. This is the command I'm using

find /sb/SB.EXC/data/EDI/alltests -type f -mtime +1 -exec rm {} \;

What I want is to clear files in the ...alltest directory without affecting files in the ...alltest/AFEN directory. Any help with amending or creating a new command line is greatly appreciated.
3 REPLIES 3
Jeff_Traigle
Honored Contributor

Re: find -exec rm question

I think if you add the -prune option, you'll get what you want.
--
Jeff Traigle
James R. Ferguson
Acclaimed Contributor

Re: find -exec rm question

Hi :

# cd /sb/SB.EXC/data/EDI/alltests && find . ! -path "./AFEN*" -type f -mtime +1 -exec rm {} +

Notice that we terminate the '-exec rm {}' with a "+". This greatly improves performance by spawning a 'rm' command with multiple arguments. For 'find' this variation of the '-exec' option acts as if you had used 'xargs' to bundle up many arguments for execution.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: find -exec rm question

>Jeff: I think if you add the -prune option, you'll get what you want.

Since -prune is very complicated and rm is dangerous, here is what should work:
find /sb/SB.EXC/data/EDI/alltests -name AFEN -prune -o -type f -mtime +1 -exec echo rm {} +

Remove the "echo" when you are satisfied it does what you want.

>JRF: ! -path "./AFEN*"

-prune is faster than a -path compare for the zillions of files in AFEN.

>For 'find' this variation of the '-exec' option acts as if you had used 'xargs' to bundle up many arguments for execution.

This may be faster than xargs since find(1) packs the arg list full (Mb) but xargs is very limited (~2000?).