nixCraft Poll

Topics

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

Posted by Vivek Gite [Last updated: October 15, 2007]

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'

Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. Neil Greenwood Says:

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

  2. Binny V A Says:

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

  3. Jerod Santo Says:

    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. vivek Says:

    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
  5. Jerod Santo Says:

    Vivek:

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

  6. nitin Says:

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

  7. Ganesh Says:

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

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , ,

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.