Grep



On its own grep -v grep is not going to do a lot.
The -v option to grep means to ignore results containing the following pattern. But there is no search pattern specified and no file listed.

But grep -v grep is a pattern you might use in the middle of a series of piped commands.

So for example - if you wanted to see how many instances of bash were running and you ran a command like:
ps aux | grep bash

You would get a list of all running processes that include the word bash. Which would include the currently "ps aux | grep bash" command you just entered.

If you want to exclude that result, you would include grep -v grep like this:
ps aux | grep bash | grep -v grep
Or this:
ps aux | grep -v grep | grep bash

Both have the same effect!

The only minor difference would be if you have grep aliased to something like alias grep='grep --color=auto -n' in your .bashrc. Then the 2nd version will leave the instances of "bash" highlighted/colourised in the final output because grep bash was the last pattern in the piped sequence.

Whereas the first one, the instances of "bash" will not be highlighted/colourised because the last pattern in the sequence of pipes was grep -v grep, which filters the results to exclude anything from the previous results that includes "grep". So there will be nothing to highlight!

For more information about grep and it's many options - take a look at the grep man-pages using man grep, or its info pages using info grep
 

Staff online

Members online


Top