Can boolean operators be used with find and xargs?

I have a directory of 5000 files, some of which are mistakenly written with a syntax error. I use the following code to determine which files have an error:

ls -1 | while read a; do grep -q '^- ' $a || echo $a; done

At first I tried to use a combination of findand xargs, but I could not figure out how to add the logic that I needed.

My use case is not related to I / O and ends quickly enough. But I was curious to see if the same operation would be performed without using a bash loop. Despite the usability of Bash, I tend to rely heavily on pipelines for loops, which often lead to staggeringly slow operation .

+4
source share
3 answers

Logic logic can be used with find:

find -maxdepth 1 -type f \( -exec grep -q '^- ' {} \; -o -print \)

The parameter -ois a logical OR. If the command being executed -execreturns a nonzero return value -print, it will display the file name.

+3
source

Here is another way to do this using grep -L:

find -maxdepth 1 -type f -exec grep -L '^- ' {} \;

The above code will list all the files in the directory that DO NOT contain a line starting with dash + space -in their contents.

To make the code above recursive (i.e. expand the search in all subdirectories), simply delete the part -maxdepth 1.

From man grepabout option -L:

-L, --files-without-match ; , . .

+2

grep:

grep -d skip -L '^- ' *

. find, .
, grep -L '^- ' -R . ( -R POSIX, GNU, BSD/macOS grep).

-L, Jamil Said, , ( ) , .

-d skip ( -d POSIX, GNU, BSD/macOS grep).


. hek2mgl , , *, , , /usr/bin/grep: Argument list too long.
( , grep -R ., .)

. getconf ARG_MAX, , , > - . .

5000 , , , . , macOS - / [1] .
Linux .

, xargs :

printf '%s\0' * | xargs -0 grep -d skip -L '^- '

, -0 NUL- POSIX, GNU, BSD/macOS xargs.

, xargs , grep , .


[1] macOS 10.12 262,144 (256 ); , 250,000 , 250000 / 5000 == 50 _ + ( ) 49 .
, Ubuntu 16.04 8 : 2,097,152 (2 ).

+2

Source: https://habr.com/ru/post/1666945/


All Articles