Hacker News new | ask | show | jobs
by ehartsuyker 3845 days 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.
4 comments

`xargs -0' was designed to properly handle the output of `find -print0' -- no while loop needed.

`find -print0 | xargs -0 grep some_pattern'

And you also want to prevent `while` loop in shell script. See http://unix.stackexchange.com/q/169716/38906 for more details.
Use "find ... -exec command '{}' '+'" instead of "find | xargs command".
avoid read-while loop... there are problems with filenames starting/ending with spaces... better use

    find ... -print0 | xargs -0 ... [eventually with -n1]
And you need non-standard feature `-print0` and `xargs -0` there. Better way should be `find ... -exec cmd {} +`.
Is 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).
Systems 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.

Sorry 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.