If the version of find being used supports it, I usually use the -delete option as it is usually even faster. Implementations differ but usually the -exec option of find does a fork() and then an execvp(), and then waits on the child process. It starts a child process for each file which makes it slow. xargs does the same thing but calls vfork() and concatenates the argument list and usually only creates one child process. The -delete option simply does an rmdir() or unlink() as it is walking the filesystem tree and it is usually as fast or faster than xargs in my experience. Also by using -delete you don't run into the number of arguments hard limit that xargs imposes (or rather execvp(3)). By default, -delete deletes both files and directories, so if the intention is only to delete files, the -type parameter must also be specified. Of course, if it is a script that is being run often, it doesn't hurt to profile and determine which is best.
I usually do:
find . -type f -mtime +30 -delete
Last edited by hnrz; 06-24-2011 at 04:25 AM..
The Following User Says Thank You to hnrz For This Useful Post: