sMaN
sMaN

Reputation: 3937

How to invert a grep expression

The following grep expression successfully lists all the .exe and .html files in the current directory and sub directories.

ls -R |grep -E .*[\.exe]$\|.*[\.html]$  

How do I invert this result to list those that aren't a .html or .exe instead. (That is, !=.)

Upvotes: 337

Views: 384006

Answers (5)

jmd_dk
jmd_dk

Reputation: 13100

As stated multiple times, inversion is achieved by the -v option to grep. Let me add the (hopefully amusing) note that you could have figured this out yourself by grepping through the grep help text:

grep --help | grep invert

-v, --invert-match select non-matching lines

Upvotes: 31

Anja Ishmukhametova
Anja Ishmukhametova

Reputation: 1564

 grep "subscription" | grep -v "spec"  

Upvotes: 17

Eric Fortis
Eric Fortis

Reputation: 17350

Use command-line option -v or --invert-match,

ls -R |grep -v -E .*[\.exe]$\|.*[\.html]$

Upvotes: 469

Rob Sobers
Rob Sobers

Reputation: 21135

Add the -v option to your grep command to invert the results.

Upvotes: 37

darioo
darioo

Reputation: 47183

grep -v

or

grep --invert-match

You can also do the same thing using find:

find . -type f \( -iname "*" ! -iname ".exe" ! -iname ".html"\)

More info here.

Upvotes: 134

Related Questions