4
ps aux  | grep firefox

Lists all processes having the string "firefox"

ps aux  | grep -v firefox

Lists all the processes without the string "firefox"

ps aux | grep -v grep | grep firefox ?

What does this second grep does ? grep itself is a command then why we are grepping another grep ?

3 Answers3

18

When you do a command such as

ps aux  | grep firefox

Then the grep process itself may show in the output because the word you are looking for is present. e.g. on my machine I run chrome and the similar results:

% ps aux | grep chrome
sweh      3384  0.0  0.0  11128  1024 pts/1    S+   07:08   0:00 grep chrome
sweh     23698  0.0  0.0   6384   620 ?        S    Jul04   0:00 /usr/lib/chromi

We can see process 3384 is the grep command and matches because the word chrome shows up.

To avoid this some people then add a second | grep -v grep to remove that line.

There is a cheat though...

ps aux | grep '[f]irefox'

grep '[f]irefox' matches exactly the same lines as grep firefox, but now the grep command will never match itself because the word doesn't literally appear on that command.

ilkkachu
  • 138,973
9

The ps command will output all your currently running processes. The first grep will remove the grep process from this list. The second will extract any firefox process in the filtered list.

This is probably a partial attempt to get the process ID (PID) of the firefox process, possibly just to see if it's running, or to terminate it.

If this is the case, I strongly advise you to use pgrep (or pkill, if it's process termination you're after) instead:

if pgrep firefox 2>/dev/null; then
  echo "firefox is already running"
else
  echo "starting firefox..."
  firefox &
done

To terminate firefox:

pkill firefox

That is, don't go the long way around finding its PID first.

See also the manuals for pgrep and pkill on your system.

Kusalananda
  • 333,661
  • Upvoted, since pgrep is way better than | grep -v grep and the cheat mentioned by Stephen. Links to their manual pages are appreciated though. – Franklin Yu Dec 02 '16 at 01:38
  • @FranklinYu There are slightly different implementations of pgrep and pkill available depending on the Unix one is using. One should always read the manual installed on one's own machine. – Kusalananda Nov 28 '18 at 17:36
4

Analyzing ps aux | grep -v grep | grep firefox

ps aux will give you the output of processes. The first grep (grep -v grep) will remove any line that will be feeded in it from ps aux that will contain the word grep. After the output will be grepped again (grep firefox) for the word firefox and give you the output.

ilkkachu
  • 138,973
igiannak
  • 750
  • The commands after second pipe. grep firefox , Wouldn't that add another grep process in the list after filtering grep processes through commands next to first pipe ? – Kranthi Kiran Jul 12 '16 at 11:01
  • you eliminate the occurance of grep with grep -v grep. like @ilkkachu said try to ps aux | grep -v grep | grep hello output will be null – igiannak Jul 12 '16 at 11:05
  • Seems I understand that. But again doubt that because you are adding another grep in pipe at the last. Why doesn't last grep process list there ? Probably that's how Linux is designed to work I suppose. – Kranthi Kiran Jul 12 '16 at 11:09
  • 2
    The only thing that looks for the processes currently running, is the ps. When it runs, either there is a grep (or several) in the list it sees and prints, or there isn't. The output of ps goes to grep as just a string of text. At that point if doesn't matter if new processes appear, ps didn't see them, so they are not in the text seen by the grep processes. Then both grep commands remove part of their input text and output the rest. – ilkkachu Jul 12 '16 at 11:13