4 ms·
I love the '-exec' flag for find
by spahl 17y ago
I love the '-exec' flag for find
- wooby 17y agoMe too. Find also has -delete, so you don't have to type -exec rm -rf '{}' ';'
- acg 17y agoIt's worth noting that -delete is more efficient than the above, and indeed safer. Efficient because additional exec's are removed, for the most part find can perform the unlink itself. Safer because a replacement version for rm cannot be inserted into the user's path. Before -delete was added to find I always found perl faster: find2perl scratch -type f -size 0 -eval 'unlink' | perl or find2perl scratch -type f -size 0 -exec rm {} \; | perl The last case does not actually call rm but is hard-wired to use unlink(). It may still be faster.
- makecheck 17y ago...except, that you should rarely use it. :) It is much, much faster to pipe commands to "xargs". For instance: find . -name core -print0 | xargs -0 rm -f Since "find" doesn't know when your command is multi-file, it has to run the command once per file; whereas, "xargs" can run it as few times as necessary. It works well for any command that accepts "a list of files". In the example above, the -print0/-0 are a safety net to null-delimit filenames so that it doesn't matter if any of the paths contains a space.
- spahl 17y ago...except, when I want to execute a command once per file. :-) If I want this I have the choice between: '-exec sed -i s/a/b/g {} \;' in find or '| xargs -IX sed -i s/a/b/g X'
- gaius 17y agoAlso the -n and -P options to xargs. So you would do something like $ find . -name 'blah*' |wc -l 2000 $ find . -name 'blah*' |xargs -n 250 -P 8 ./mangle That'll spawn 8 copies of mangle, each with 250 blah files on its command line.