Sample output
$ grep -rl --null --include='*.js' 'fetch(' . | xargs -0 -r grep -L 'catch'
./src/routes/users.js
./src/routes/order history.js
Four files in this project make network calls, and these two have no error handling anywhere in them. The space in order history.js came through intact because the names travelled between the two greps as zero-terminated strings, not as words.
When you would reach for it
Any audit that is really a question about absence: source files that call an API but never handle its failure, modules that import a library but are missing the license header, config files that set a timeout but no retry. A single grep answers “where does X appear” and cannot answer “where does X appear without Y”, because the second condition is about the whole file, not a line. Chaining a file-name filter into an inverse file-name filter gives you that set operation without writing a script.
Gotchas
- Leave out the
-ron xargs and a first grep that finds nothing still runs the second one, with no filenames. grep then reads standard input, which GNU xargs points at/dev/null, finds no match there, and prints(standard input)as a result. On macOS, xargs already skips empty input and accepts-rfor compatibility, so the same command works on both. grep -Lsets its exit status by whether any line matched, not by whether it printed a filename. With GNU grep 3.8, the version on Debian 12, it exits 1 when every file it was given is listed and 0 when at least one file had a match, sogrep -L catch file.js && echo missingprints the opposite of what it says. A few GNU releases around 2018 to 2020 briefly flipped this. Test the output, not$?.- Without
--nulland-0, xargs splits on whitespace, andorder history.jsarrives as two files calledorderandhistory.js. You get twoNo such file or directoryerrors and, worse, a file silently missing from the answer. - The test is per file and purely textual. A file whose only
catchis in a comment, or that handles errors in a different function from the one making the call, counts as handled. Treat the result as a short list to read, not a verdict.
Variants
$ grep -rl --null 'fetch(' . | xargs -0 grep -l --null 'json' | xargs -0 -r grep -L 'catch'
Add a condition by adding a stage. Every middle stage uses -l --null so it can feed the next, and only the last one switches to -L
$ git grep -lz 'fetch(' -- '*.js' | xargs -0 -r grep -L 'catch'
Inside a repository, search only tracked files. git grep skips anything in .gitignore, so node_modules and build output stay out of the results without a long list of exclusions
$ grep -rL --include='*.js' 'SPDX-License-Identifier' .
The one-condition case needs no pipeline. Every JavaScript file missing a license header, listed directly