I have a directory of about 5000 files of which some were erroneously written with a syntax error. I am using the following code to identify which files have the error:
ls -1 | while read a; do grep -q '^- ' $a || echo $a; done
I initially tried to use a combination of find
and xargs
but I couldn't figure out how to add the boolean logic I needed.
My use case is not I/O bound and completes fast enough. But I was curious to see if this same operation be done without relying on a bash loop. Although comfortable with Bash, I have a tendency to rely heavily on piping into loops which often leads to mind numbingly slow performance.
You can use boolean logic with find
:
find -maxdepth 1 -type f \( -exec grep -q '^- ' {} \; -o -print \)
The -o
option is a logical OR. If the command executed by -exec
will return a non-zero return value -print
will print the filename.
Here is another way to do this, using grep -L
:
find -maxdepth 1 -type f -exec grep -L '^- ' {} \;
The code above would list all files on the directory which do NOT contain a line starting with dash + space -
in their contents.
To make the code above recursive (that is, to extend the search to all subdirectories), just remove the -maxdepth 1
part out.
From man grep
about option -L
:
-L, --files-without-match Suppress normal output; instead print the name of each input file from which no output would normally have been printed. The scanning will stop on the first match.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With