Linux: Exclude grep from grepped ps output

📄 Wiki page | 🕑 Last updated: Apr 8, 2024

Grepping the ps output will usually include the grep process itself, i.e. if you run:

ps aux | grep test

In the output you'll see something like this:

n        1390753  0.0  0.0   6332  2048 pts/1    S+   00:07   0:00 grep test

To exclude this line, we can either (ab)use the fact that grep works with regular expressions:

ps aux | grep [t]est

This works because [t]est will match t followed by est, and grep line in the output (grep [t]est) now contains t in brackets, so it will get excluded.

Alternatives

pgrep command (from procps package) is a popular alternative to ps+grep combination. Since there's only one process, it doesn't have this issue.

By default pgrep only gives you process IDs, but you can get the full command line with -a option, i.e. pgrep -a nginx

1139 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
1143 nginx: worker process
1145 nginx: worker process
1146 nginx: worker process
1147 nginx: worker process

Ask me anything / Suggestions

If you have any suggestions or questions (related to this or any other topic), feel free to contact me. ℹī¸


If you find this site useful in any way, please consider supporting it.