4 ms·
Is there a reason to prefer `while read; ...;done` over find's -exec or piping into xargs?
by gnuvince 2y ago
Is there a reason to prefer `while read; ...;done` over find's -exec or piping into xargs?
- xolox 2y agoBoth `find -exec` and xargs expect an executable command whereas `while read; ...; done` executes inline shell code. Of course you can pass `sh -c '...'` (or Bash or $SHELL) to `find -exec` or xargs but then you easily get into quoting hell for anything non-trivial, especially if you need to share state from the parent process to the (grand) child process. You can actually get `find -exec` and xargs to execute a function defined in the parent shell script (the one that's running the `find -exec` or xargs child process) using `export -f` but to me this feels like a somewhat obscure use case versus just using an inline while loop.
- retrogeek 2y agoI will sometimes use the "| while read" syntax with find. One reason for doing so is that the "-exec" option to find uses {} to represent the found path, and it can only be used ONCE. Sometimes I need to use the found path more than once in what I'm executing, and capturing it via a read into a reusable variable is the easiest option for that. I'd say I use "-exec" and "| while read" about equally, actually. And I admittedly almost NEVER use xargs.