More elegant "ps aux | grep -v grep"

Jakub M. picture Jakub M. · Feb 21, 2012 · Viewed 177k times · Source

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)

Answer

Johnsyweb picture Johnsyweb · Feb 21, 2012

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.