Quick Shell Tip: Remove grep command while grepping something using ps command

by on October 15, 2007 · 21 comments· LAST UPDATED October 15, 2007

in , ,

Generally you use ps command to find out all running process. You may also pipe out ps command output via grep command to pickup desired output.

Basically you don't want display grep command as the process.

Let us run combination of ps and grep command to find out all perl processes:
$ ps aux | grep perl

vivek      4611  0.0  0.7  10044  6068 ?        Ss   02:40   0:00 /usr/bin/perl apps/monitor/gwl.pl
root      4853  0.0  0.7  10044  6068 ?        Ss   02:40   0:00 /usr/bin/perl /usr/share/webmin/miniserv.pl /etc/webmin/miniserv.conf
vivek      5166  0.0  0.0   2884   748 pts/0    R+   03:06   0:00 grep perl

In above example you are getting the grep process itself. To ignore grep process from output, type any one of the following:
$ ps aux | grep perl | grep -v grep
$ ps aux | grep '[p]erl'

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 21 comments… read them below or add one }

1 Neil Greenwood October 16, 2007 at 6:46 am

Also works when you’re grepping for something in the output of the history command.


2 Binny V A October 16, 2007 at 9:07 am

Nice one. I am not going to use this – I really don’t mind the grep command appearing. Still, its clever.


3 Jerod Santo October 16, 2007 at 2:09 pm

This has been an annoyance of mine for awhile now. However, its a bit too syntactically taxing for the marginal gain.

Does anybody know how to set up an alias to accomplish the same goal?


4 nixCraft October 16, 2007 at 5:07 pm


alias not possible as passing args interpreted by shell at the time of creation. But something as follows should help out:

function pps(){ ps aux | grep "$@" | grep -v 'grep'; }
pps perl


5 Jerod Santo October 17, 2007 at 1:43 pm


Thanks a lot. Added that function to my .bashrc and it works like a charm!


6 nitin November 18, 2007 at 9:21 am

$ ps aux | grep ‘[p]erl’
can you explain how this work? regexp ?


7 dneproshin November 1, 2010 at 7:57 pm

Putting one letter inside square brackets won’t change the meaning of grep expression. However, it removes the grep command from the matched lines because the expression ‘[p]erl’ matches only ‘perl’, not ‘[p]erl’, which is how the grep command itself is now shown in the process list.


8 Adrien May 15, 2013 at 11:38 am

Thanks for the explanation


9 Amit September 3, 2013 at 8:37 am

This is very clever, thank you!


10 Ganesh November 28, 2007 at 5:20 am

Thanks for ur wonderful suggestion in adding to .bashrc……… Good… keep it up


11 Vyas October 12, 2009 at 8:12 am

Thanks man! Good suggestion , I got fed up by looking the grep in process list (itself).


12 johnnyG March 22, 2010 at 10:11 pm

I use this function to view or grep all processes on my mac. For Linux you might have to change the switches on the ps command.

So using using pss command without arguments, you will get a full list of all processes.

if you use pss with a single argument, the function will grep all the processes and it will double space the output.

function pss () {
if [ -n “$1″ ]
ps -ajx | grep -i “$1″ | grep -v “grep” | sed G
ps -ajx


13 Alexander Haeussler August 19, 2010 at 3:31 pm

ps -C perl


14 Sergani March 5, 2012 at 10:58 pm

“$ ps aux | grep perl | grep -v grep” made my day, thanks!


15 jawsnnn May 31, 2012 at 9:48 am

Why does ps -ef show grep in the output anyway?
My rudimentary understanding of the following command
ps -ef | grep ‘abc.sh’
says that the output of the first command (ps -ef) should be provided as input to the second command (grep ‘abc.sh’) which means that when ps -ef is triggered, the grep command does not exist.
My understanding is obviously flawed. Can someone throw light on what actually happens?


16 Jeremy August 31, 2012 at 8:04 pm

Jawsnn, I cannot answer that question. I am curious myself, but I just found another anomaly.

I am inside of my script and I want to check to make sure my script isn’t running anywhere else. It is a script that shouldn’t run concurrently because its doing system stuff. For simplicity, lets say the script is testScript.sh

Within the scripts I have:

isRunning=`ps -elf | grep testScript.sh | grep -v grep | grep -v vi | wc -l`

When I run this command outside the script (with or without the wc -l), it only ever shows 1 process when my script is running. However, inside the script it shows as two processes. It was a “wait” on the first process with a “pipe_w” on the second. This only seems to happen when I run the command within the script with the same name of the process I am running. If I am grepping for a process with another name, I do not see the same issue. This makes no sense to me.


17 aaa December 14, 2012 at 12:17 pm

Both processes are started immediately, that’s the point of using pipe.


18 David P September 7, 2012 at 7:01 pm

Thanks that worked out well. As an added bonus for color reverse the above command to look like this in bash.rc:

function pps(){ ps aux | grep -v ‘grep’| grep “$@”; } pps perl

and uncomment this line in the same file:
alias grep=’grep –color=auto’
Now pps lighttpd gives a nice highlighted response


19 Mahdi June 9, 2013 at 8:28 pm

Ao! Thank you very much, this blog is awesome!


20 sri October 16, 2013 at 8:36 am

Thanks much dude. It helped


21 Przemo January 15, 2015 at 3:54 pm

Nice !


Leave a Comment

Tagged as: , ,

Previous post:

Next post: