Ubuntu – the meaning of the `|` symbol in the shell

bashcommand lineprocess

What does the | symbol mean in sudo ps -ef | grep processname command?

Also can anyone please explain this command? I have used this command only for getting the PID and killing that process, but I also saw sudo ps -ef | grep processname | grep -v grep and I am under the impression that -v grep is like killing the previous generated PID for grep. If that's so how does it work?

Best Answer

ps -ef | grep processname

It first runs sudo ps -ef and passes the output to the second command.

The second command filters all lines that contain word "processname".

ps -ef | grep processname | grep -v grep lists all lines containing processname and not containing grep.

According to man grep

-v, --invert-match
              Invert the sense of matching, to select non-matching lines.  (-v
              is specified by POSIX.)

According to man ps

ps displays information about a selection of the active processes.

-e     Select all processes.  Identical to -A.

-f     Do full-format listing. This option can be combined with many
          other UNIX-style options to add additional columns.  It also
          causes the command arguments to be printed.  When used with -L,
          the NLWP (number of threads) and LWP (thread ID) columns will be
          added.  See the c option, the format keyword args, and the
          format keyword comm.

You can combine parameters -ef means same as -e -f.

Actually ps -ef | grep processname list all occurrences of the process called processname.