4 ms·
I think the best "defensive" piece of advice you left out that everyone abuses is never pipe `find` results to `xargs.` One should always do `find ... -print0`
by ehartsuyker 11y ago
I think the best "defensive" piece of advice you left out that everyone abuses is never pipe `find` results to `xargs.` One should always do `find ... -print0` to a read-while loop because of filenames with whitespace.
- Gnouc 11y agoAnd you also want to prevent `while` loop in shell script. See http://unix.stackexchange.com/q/169716/38906 http://unix.stackexchange.com/q/169716/38906 for more details.
- elevation 11y ago`xargs -0' was designed to properly handle the output of `find -print0' -- no while loop needed. `find -print0 | xargs -0 grep some_pattern'
- _ZeD_ 11y agoavoid read-while loop... there are problems with filenames starting/ending with spaces... better use find ... -print0 | xargs -0 ... [eventually with -n1]
- Gnouc 11y agoAnd you need non-standard feature `-print0` and `xargs -0` there. Better way should be `find ... -exec cmd {} +`.
- potatosareok2 11y agoIs there a place you can use `-exec cmd {} +` but not `print0`? My experience has been I can use both or I can use none (Solaris 10 boxes).
- dumol 11y agoSystems with a non-GNU find, including the default one in Solaris 10 (I've just double-checked). Perhaps you were using the GNU find in Solaris? By the way, there are systems where 'print0' is a recognized parameter for the default find, but not '+'. RHEL 4.x comes to mind.
- potatosareok2 11y agoSorry I think we're saying same thing. On systems with non-GNU find do either `-exec cmd {} +` or `print0` work? My exp was both did NOT work. So either both work or both don't. But if I understand you correctly on RHEL 4.X `print0` works but `-exec cmd {} +` doesn't. Which is to say I disagree with op that it's better to rely on `exec cmd {} +` when it seems you're more likely to have `print0 and xargs -0` then that.
- lisivka 11y agoUse "find ... -exec command '{}' '+'" instead of "find | xargs command".