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

by Vivek Gite on October 15, 2007 · 11 comments

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
Output:

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
OR
$ ps aux | grep '[p]erl'

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

We're here to help you make the most of sysadmin work. So, subscribe!

{ 11 comments… read them below or add one }

1 Neil Greenwood October 16, 2007

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

Reply

2 Binny V A October 16, 2007

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

Reply

3 Jerod Santo October 16, 2007

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?

Reply

4 vivek October 16, 2007

Jerod,

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

Reply

5 Jerod Santo October 17, 2007

Vivek:

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

Reply

6 nitin November 18, 2007

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

Reply

7 dneproshin November 1, 2010

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.

Reply

8 Ganesh November 28, 2007

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

Reply

9 Vyas October 12, 2009

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

Reply

10 johnnyG March 22, 2010

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" ]
then
ps -ajx | grep -i “$1″ | grep -v “grep” | sed G
else
ps -ajx
fi
}

Reply

11 Alexander Haeussler August 19, 2010

ps -C perl

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 8 + 3 ?
Please leave these two fields as-is:
Are you a human being? Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: