Remove grep command while grepping using ps command

I use ps command to find out all running process on my Linux and Unix system. The ps command shows information about a selection of the active processes on shell. You may also pipe out ps command output through grep command to pick up desired output.

Example: Remove grep command while grepping using ps

Remove grep command and prevent grep from showing up in ps results
Let us run a combination of ps command and grep command to find out all Perl processes:
$ ps aux | grep perl
Sample output:

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

In above example, I am getting the grep process itself. To ignore grep process from the output, type any one of the following command at the CLI:
$ ps aux | grep '[p]erl'
$ ps aux | grep perl | grep -v grep
Sample outputs:

vivek     4611  0.0  0.7  10044  6068 ?        Ss   02:40   0:00 /usr/local/bin/perl5 apps/monitor/
root      4853  0.0  0.7  10044  6068 ?        Ss   02:40   0:00 /usr/local/bin/perl5 /usr/share/webmin/ /etc/webmin/miniserv.conf

The above output indicate that I prevented ‘grep’ from showing up in ps results. In other words, we learned to remove grep command from ps output.

Understanding above commands

You don’t want display grep command as the process in ps output, i.e., you want to prevent ‘grep’ from showing up in ps results.

  • In first command I used regex. It says find the character ‘p’ followed by ‘erl’ i.e. the expression β€˜[p]erl’ matches only β€˜perl’ not ‘[p]erl’, which is how the grep command itself is now shown in the process list.
  • The second command uses the -v option to invert the sense of matching, to select non-matching lines.

Say hello to pgrep

You can look up process based upon name to get PID. This is only useful when looking for process names and PIDs The syntax is:
pgrep process-name
pgrep -a process
pgrep -l process
pgrep -u user -a process

To find the process ID of the sshd daemon:
$ pgrep -u root sshd

To list PID and full command line pass the -a to the pgrep command:
$ pgrep -a sshd
Sample outputs:

1101 /usr/sbin/sshd -D
5494 /usr/sbin/sshd -D
6041 /usr/sbin/sshd

To just list PID and process name pass the -l to the pgrep command:
$ pgrep -l firefox
7981 firefox


You learned how to avoid grep command from showing up in ps command results under Linux, macOS, *BSD or Unix-like systems. Pretty useful for excluding grep from process list when running ps. For more information see ps and grep command man pages by typing the following commands:
man ps
man grep

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 24 comments so far... add one
CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
24 comments… add one
  • Neil Greenwood Oct 16, 2007 @ 6:46

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

  • Binny V A Oct 16, 2007 @ 9:07

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

  • Jerod Santo Oct 16, 2007 @ 14:09

    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?

  • 🐧 nixCraft Oct 16, 2007 @ 17:07


    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
  • Jerod Santo Oct 17, 2007 @ 13:43


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

  • nitin Nov 18, 2007 @ 9:21

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

    • dneproshin Nov 1, 2010 @ 19:57

      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.

      • Adrien May 15, 2013 @ 11:38

        Thanks for the explanation

      • Amit Sep 3, 2013 @ 8:37

        This is very clever, thank you!

  • Ganesh Nov 28, 2007 @ 5:20

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

  • Vyas Oct 12, 2009 @ 8:12

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

  • johnnyG Mar 22, 2010 @ 22:11

    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

  • Alexander Haeussler Aug 19, 2010 @ 15:31

    ps -C perl

  • Sergani Mar 5, 2012 @ 22:58

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

  • jawsnnn May 31, 2012 @ 9:48

    Why does ps -ef show grep in the output anyway?
    My rudimentary understanding of the following command
    ps -ef | grep ‘’
    says that the output of the first command (ps -ef) should be provided as input to the second command (grep ‘’) 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?

    • Jeremy Aug 31, 2012 @ 20:04

      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

      Within the scripts I have:

      isRunning=`ps -elf | grep | 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.

    • aaa Dec 14, 2012 @ 12:17

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

  • David P Sep 7, 2012 @ 19:01

    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

  • Mahdi Jun 9, 2013 @ 20:28

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

  • sri Oct 16, 2013 @ 8:36

    Thanks much dude. It helped

  • Przemo Jan 15, 2015 @ 15:54

    Nice !

  • Mce Apr 28, 2015 @ 14:27

    Cool, thanks for the info! This is very helpful and I feel awfully ridiculous for not having thought of the second one as I use regex all the time and often rather complex ones… lol!

    I’ve used the first method many times but it is just plain unwieldy and not to mention, I also often forget to type it out; however, it usually doesn’t matter to functionality and is just a mere annoyance. However, when you’re just checking the exit value of grep to use in a conditional, then that quite matters.

    I do have to say that the second method is an elegant and more importantly short and sweet. And after all, being a long time linux/unix admin and a coder, I find that is always a desirable thing!

    Thanks again for the tip!

  • Marcos Aug 14, 2015 @ 8:28

    Nice a rather annoying output… thanks !!

  • Roger Aug 3, 2016 @ 19:38

    Very good post. Thanks a lot.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum