Coder Perfect

Inverting a grep expression

Problem

The following grep phrase lists all.exe and.html files in the current directory and subdirectories successfully.

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

How can I flip this result to list only the files that aren’t.html or.exe? (In other words,!=.)

Asked by sMaN

Solution #1

Use the -v or —invert-match command-line options.

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

Answered by Eric Fortis

Solution #2

grep -v

or

grep --invert-match

You can also use search to accomplish the same goal:

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

More info here.

Answered by darioo

Solution #3

To reverse the results, use the grep command with the -v option.

Answered by Rob Sobers

Solution #4

The -v option to grep achieves inversion, as previously described. Let me add the (hopefully funny) caveat that you could have figured this out by grepping through the grep help text yourself:

grep --help | grep invert

Answered by jmd_dk

Solution #5

 grep "subscription" | grep -v "spec"  

Answered by Anja Ishmukhametova

Post is based on https://stackoverflow.com/questions/4373675/how-to-invert-a-grep-expression