grep processes list avoiding the grep itself

ps axu | grep [a]pache2
Trick to avoid the form: grep process | grep - v grep
Sample Output
user@ubuntu:~$ ps axu | grep [a]pache2
root      9399  0.0  0.1 105628  7588 ?        SNs  11:55   0:01 /usr/sbin/apache2 -k start
www-data  9401  0.0  0.1 105716  5576 ?        SN   11:55   0:00 /usr/sbin/apache2 -k start
www-data  9402  0.0  0.1 105716  5572 ?        SN   11:55   0:00 /usr/sbin/apache2 -k start
www-data  9403  0.0  0.1 105668  5028 ?        SN   11:55   0:00 /usr/sbin/apache2 -k start
www-data  9404  0.0  0.1 105716  5576 ?        SN   11:55   0:00 /usr/sbin/apache2 -k start
www-data  9405  0.0  0.1 105652  4788 ?        SN   11:55   0:00 /usr/sbin/apache2 -k start
www-data 15581  0.0  0.1 105652  4788 ?        SN   12:04   0:00 /usr/sbin/apache2 -k start
www-data 15582  0.0  0.1 105652  4788 ?        SN   12:04   0:00 /usr/sbin/apache2 -k start
www-data 15583  0.0  0.1 105652  4788 ?        SN   12:04   0:00 /usr/sbin/apache2 -k start

2012-12-15 19:37:19

10 Alternatives + Submit Alt

What Others Think

This works: ps axu | grep apa\\che double backslash can be anywhere within the pattern
tedkozma · 435 weeks ago
I use something like this: auxer () { ps aux | grep -i "$(echo "$1" | sed "s/^\(.\)\(.*$\)/\[\1\]\2/")" }
redshadowhero · 434 weeks and 6 days ago
Can someone please explain why this works?
Mikecron · 434 weeks and 3 days ago
I found the explanation given by user "leper421" here: 'This works because "[h]ttpd" matches only an "h" followed by "ttpd". The line that is printed by "ps aux" is "[h]ttpd" which includes the brackets, therefor not matching. Clever.'
Mikecron · 434 weeks and 3 days ago
Can also be done with awk: $ ps axu | awk '/[a]pache2/' To extract a PID: $ ps axu | awk '/[a]pache2/ {print $1}' But wasn't pgrep written to solve this ? pgrep -l apache2 pgrep apache2
zlemini · 433 weeks and 1 day ago
Hi Mikecron, sorry for late reply, this commands works because of regular expression issues. When you use square brakets in a regexp it means: a chararcter part of this set. Example: [a-z]pache: match apache, bpache, cpache, ... , zpache [a]pache: matches only apache So when you type the command it filters all the lines containins the word "apache". If you are able to see the process list (ps -ef) in the exact moment as grep runs you would see a line containing "grep [a]pache". So why the grep process isn't shown? Because regexp([a]pache) != string([a]pache) Hope this is clear. Bye
EBAH · 433 weeks ago
Dear zlemini, I admit I didn't know pgrep. Since I work on a variety of OSes (Linux, Solaris, HP-UX) I tend to be as conservative as possible, so I prefer to use the same command on all the systems instead of variants. One example above all: one script for all OSes instead of three is easier to maintain. Anyway I'll have a look to the pgrep, I'm courious. Thanks!
EBAH · 433 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: