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

Posted on in Categories Linux, Shell scripting, Tip of the day, UNIX last updated November 3, 2017

How to prevent grep from showing up in ps results

I use ps command to find out all running process. You may also pipe out ps command output through grep command to pick up desired output.

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/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, I am getting the grep process itself. To ignore grep process from the output, type any one of the following:
$ ps aux | grep '[p]erl'
$ ps aux | grep perl | grep -v grep
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.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Share this on (or read 24 comments/add one below):

24 comment

    1. 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.

  1. 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

  2. Question:
    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?

    1. 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.

  3. 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 “[email protected]”; } pps perl

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

  4. 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!

Leave a Comment