You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Michael Diamond edited this page Feb 22, 2020
·
3 revisions
Use find -print0 or find -exec to better handle non-alphanumeric filenames.
Problematic code:
ls | xargs -n1 wc -w
Correct code:
find . -maxdepth 1 -print0 | xargs -0 -n1 wc -w
find . -maxdepth 1 -exec wc -w {} \;
Rationale:
Using -print0 separates each output with a NUL character, rather than a newline, which is safer to pipe into xargs. Alternatively using -exec avoids the problem of piping and parsing filenames in the first place.