191

When I check list of processes and 'grep' out those that are interesting for me, the grep itself is also included in the results. For example, to list terminals:

$ ps aux  | grep terminal
user  2064  0.0  0.6 181452 26460 ?        Sl   Feb13   5:41 gnome-terminal --working-directory=..
user  2979  0.0  0.0   4192   796 pts/3    S+   11:07   0:00 grep --color=auto terminal

Normally I use ps aux | grep something | grep -v grep to get rid of the last entry... but it is not elegant :)

Do you have a more elegant hack to solve this issue (apart of wrapping all the command into a separate script, which is also not bad)

4
  • 4
    For what it's worth, this is an ancient FAQ. See item 3.10 at faqs.org/faqs/unix-faq/faq/part3
    – tripleee
    May 16, 2013 at 18:33
  • 1
    Thanks for reference. That's their method: ps ux | awk '/name/ && !/awk/ {print $2}'
    – Jakub M.
    May 16, 2013 at 19:55
  • What is the grep -v grep part doing?
    – Jwan622
    Apr 21, 2017 at 0:10
  • 2
    @Jwan622 grep -v grep excludes grep from grep results. If grep is used in combination with ps, then grep process (with grep arguments) will be shown as well, cluttering your results. grep -v grep is a common way to avoid that Apr 29, 2017 at 3:10

9 Answers 9

291

The usual technique is this:

ps aux | egrep '[t]erminal'

This will match lines containing terminal, which egrep '[t]erminal' does not! It also works on many flavours of Unix.

7
  • 1
    This works for me for an arbitrary string, but not for a username, e.g. ps aux | grep '[r]oot' . Does anyone know why?
    – kxsong
    Oct 1, 2014 at 19:41
  • 2
    @kxsong: | grep '[t]erminal' selects any line containing the word 'terminal' without putting the word 'terminal' into the process list. What are you trying to achieve with | grep '[r]oot' and how is it not working? There is likely to be a better solution.
    – johnsyweb
    Oct 1, 2014 at 20:33
  • 3
    Correct me if I am wrong, but this also should work on any position of the grepped character: ps aux| grep "te[r]minal"
    – meso_2600
    Mar 23, 2016 at 9:54
  • 2
    brilliant hack (I think I should call it that, since the aux / grep authors probably did not think of this scenario.)
    – Michahell
    Apr 12, 2016 at 14:12
  • 2
    @JamieJag: Well... like I said in my post, grep '[t]erminal' will match lines containing terminal. The output from ps aux will have a line with grep '[t]erminal' (with the square brackets), which does not contain the string terminal (without the same).
    – johnsyweb
    May 9, 2016 at 23:41
60

Use pgrep. It's more reliable.

10
  • pgrep wont work if I look for example for ps aux | grep 'ssh options'
    – Jakub M.
    Feb 21, 2012 at 11:26
  • 12
    Jakub M.: What about pgrep -f ?
    – hillu
    Sep 30, 2013 at 8:26
  • 4
    @jakub-m By default pgrep only matches the pattern against the process name. To match against the entire command, use the -f flag. Mar 21, 2014 at 21:52
  • 3
    pgrep only returns process IDs.
    – Melab
    Dec 15, 2015 at 18:11
  • 1
    older versions took pgrep -fl (but couldn't see full cmdline without -f matching full cmdline, details: serverfault.com/a/619788/44183). But yes if you need any other info beside pid, cmdline, you need ps. Can combine them: ps -p $(pgrep -f foo) Jul 30, 2017 at 9:00
29

This answer builds upon a prior pgrep answer. It also builds upon another answer combining the use of ps with pgrep. Here are some pertinent training examples:

$ pgrep -lf sshd
1902 sshd

$ pgrep -f sshd
1902

$ ps up $(pgrep -f sshd)
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      1902  0.0  0.1  82560  3580 ?        Ss   Oct20   0:00 /usr/sbin/sshd -D

$ ps up $(pgrep -f sshddd)
error: list of process IDs must follow p
[stderr output truncated]

$ ps up $(pgrep -f sshddd) 2>&-
[no output]

The above can be used as a function:

$ psgrep() { ps up $(pgrep -f $@) 2>&-; }

$ psgrep sshd
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      1902  0.0  0.1  82560  3580 ?        Ss   Oct20   0:00 /usr/sbin/sshd -D

Compare with using ps with grep. The useful header row is not printed:

$  ps aux | grep [s]shd
root      1902  0.0  0.1  82560  3580 ?        Ss   Oct20   0:00 /usr/sbin/sshd -D
5
  • See github.com/blueyed/oh-my-zsh/blob/… (but it's specific to Zsh).
    – blueyed
    Mar 23, 2015 at 1:11
  • @blueyed, I have updated the answer with the bash function definition.
    – Asclepius
    Mar 23, 2015 at 20:31
  • Note also -d option to specify a separator; e.g. ps -fp$(pgrep -d , getty) Feb 22, 2016 at 17:19
  • 1
    I use this: ps uxp `pgrep <process>` Note that p must be the last parameter (i.e., pux won't work)
    – KFL
    Nov 28, 2018 at 8:48
  • This does not color the search process name like grep does
    – alper
    Aug 8, 2020 at 12:39
10

You can filter in the ps command, e.g.

ps u -C gnome-terminal

(or search through /proc with find etc.)

3
  • 1
    Note that this works with GNU's ps (Linux), but not with the BSD ps.
    – Josh
    Feb 5, 2013 at 23:06
  • This is incorrect even with GNU ps. ps -C <command> will match the exact command. When used with the a or x options it will report all processes, because a and x list processes in addition to the set of processes matched by other means.
    – Animism
    Dec 12, 2013 at 22:19
  • This works only if the process name is specified exactly. It doesn't work for a partial match, e.g. logind for systemd-logind, or to match arguments.
    – Asclepius
    Oct 21, 2016 at 5:31
6

One more alternative:

ps -fC terminal

Here the options:

 -f        does 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.

 -C cmdlist     Select by command name.
                This selects the processes whose executable name is
                given in cmdlist.
2
  • 6
    A minor drawback, and one which isn't really related to OP's question, is that this won't show you things like Tomcat, which is actually run as java with a laundry list of arguments. Dec 30, 2013 at 23:25
  • -C option was already suggested in @Andreas Frishe's answer posted over year and a half earlier… Oct 9, 2017 at 12:47
5

Disclaimer: I'm the author of this tool, but...

I'd use px:

~ $ px atom
  PID COMMAND          USERNAME   CPU RAM COMMANDLINE
14321 crashpad_handler walles   0.01s  0% /Users/walles/Downloads/Atom.app/Contents/Frameworks/Electron Framework.framework/Resources/crashpad_handler --database=
16575 crashpad_handler walles   0.01s  0% /Users/walles/Downloads/Atom.app/Contents/Frameworks/Electron Framework.framework/Resources/crashpad_handler --database=
16573 Atom Helper      walles    0.5s  0% /Users/walles/Downloads/Atom.app/Contents/Frameworks/Atom Helper.app/Contents/MacOS/Atom Helper --type=gpu-process --cha
16569 Atom             walles   2.84s  1% /Users/walles/Downloads/Atom.app/Contents/MacOS/Atom --executed-from=/Users/walles/src/goworkspace/src/github.com/github
16591 Atom Helper      walles   7.96s  2% /Users/walles/Downloads/Atom.app/Contents/Frameworks/Atom Helper.app/Contents/MacOS/Atom Helper --type=renderer --no-san

Except for finding processes with a sensible command line interface it also does a lot of other useful things, more details on the project page.

Works on Linux and OS X, easily installed:

curl -Ls https://github.com/walles/px/raw/python/install.sh | bash
1
  • 1
    This is awesome!! Jan 4, 2022 at 18:11
3

Using brackets to surround a character in the search pattern excludes the grep process since it doesn't contain the matching regex.

$ ps ax | grep 'syslogd'
   16   ??  Ss     0:09.43 /usr/sbin/syslogd
18108 s001  S+     0:00.00 grep syslogd

$ ps ax | grep '[s]yslogd'
   16   ??  Ss     0:09.43 /usr/sbin/syslogd

$ ps ax | grep '[s]yslogd|grep'
   16   ??  Ss     0:09.43 /usr/sbin/syslogd
18144 s001  S+     0:00.00 grep [s]yslogd|grep
0

Depending on the ultimate use case, you often want to prefer Awk instead.

ps aux | awk '/[t]erminal/'

This is particularly true when you have something like

ps aux | grep '[t]erminal' | awk '{print $1}'  # useless use of grep!

where obviously the regex can be factored into the Awk script trivially:

ps aux | awk '/[t]erminal/ { print $1 }'

But really, don't reinvent this yourself. pgrep and friends have been around for a long time and handle this entire problem space much better than most ad hoc reimplementations.

-3

Another option is to edit your .bash_profile (or other file that you keep bash aliases in) to create a function that greps 'grep' out of the results.

function mygrep {
grep -v grep | grep --color=auto $1
}

alias grep='mygrep'

The grep -v grep has to be first otherwise your --color=auto won't work for some reason.

This works if you're using bash; if you're using a different shell YMMV.

1
  • 1
    What's with the alias and the function? Just do function grep { command grep -v grep | command grep --color=auto "$@"; } (also note the fix of the argument and the quoting). However, this is broken in that any non-ps invocation of grep will no longer work (the arguments are passed incorrectly). Anyway, a much more useful function would be one which modifies the regex to make it not match itself, rather than filter out grep from the grep results separately. And of course, inventing new solutions to a problem which was adequately solved decades ago is not very productive.
    – tripleee
    Oct 2, 2015 at 4:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.