in reply to Re^3: need help for search and replace oneliner
in thread need help for search and replace oneliner
perlrun has two examples of using find -print0 combined with perls -0 command line option:
The second one is rather synthetic, but the first one is actually useful.
Of course, it can be written shorter as find . -name '*.orig' -print0 | xargs -0 rm. The fine difference is that the perl solution uses exactly one process on the right hand side of the pipe, whereas xargs may invoke rm several times. That happens when xargs is fed with so much data that it does not fit into the O/S limits for command line arguments. With no input, there is only one process on the right hand side of the pipe. With a little input, xargs spawns one rm process. With much input, rm will be spawned again and again, each time with as much arguments as possible within the limits of the O/S.
Even shorter: find . -name '*.orig' -exec rm '{}' \; This should work with more find implementations, as -print0 is a GNU extension. But it spawns the rm process for every single file found. No problem on a modern, nearly idle, single user workstation, but it will hurt on an old, loaded server or on an embedded system.
And now, true lazyness: findx . -name '*.orig' -- rm. It behaves exactly like find . -name '*.orig' -print0 | xargs -0 rm, because my findx is just a stupid little wrapper around find and xargs. All arguments up to the first -- are passed to find, followed by -print0. All arguments following the first -- are passed to xargs, -0 is injected as the first argument. Typically, I combine findx with grep to find a piece of text in a bunch of files: findx haystack -type f -- grep needle. Add xargs, and you can find and edit in one go: findx haystack -type f -- grep -l needle | xargs joe.
Alexander
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: need help for search and replace oneliner
by parv (Parson) on Jul 12, 2009 at 03:22 UTC |