From d1d127b9f68cd3618d13194735033bc36f447486 Mon Sep 17 00:00:00 2001 From: Michael Diamond Date: Fri, 21 Feb 2020 19:53:50 -0800 Subject: [PATCH] Created SC2011 (markdown) --- SC2011.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 SC2011.md diff --git a/SC2011.md b/SC2011.md new file mode 100644 index 0000000..c0ed1ee --- /dev/null +++ b/SC2011.md @@ -0,0 +1,23 @@ +## Use `find -print0` or `find -exec` to better handle non-alphanumeric filenames. + +### Problematic code: + +```sh +ls | xargs wc -w +``` + +### Correct code: + +```sh +find . -maxdepth 1 -print0 | xargs -0 wc -w +``` + +```sh +find . -maxdepth 1 -print0 -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. + +See [[SC2012]] for more details on this issue. \ No newline at end of file