Search code examples
bashloopsfindxargs

Can logical operators be used with find and xargs?


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.


Solution

  • 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.