Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 791
Zhro Avatar asked Mar 10 '23 03:03

Zhro


2 Answers

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.

like image 164
hek2mgl Avatar answered Mar 20 '23 01:03

hek2mgl


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.

like image 33
Jamil Said Avatar answered Mar 20 '23 02:03

Jamil Said