Exclude grep From ps Results on Linux

The ps command in Linux displays information about running processes on a system. It provides a snapshot of current processes, including process ID (PID), user ownership, CPU and memory usage, and the command that started the process. When using ps with grep to filter processes, the grep command itself often appears in the results, which can be unwanted.

Common Problem with ps and grep

When searching for specific processes using ps | grep, the grep command itself appears in the output because it's also a running process at that moment:

$ ps aux | grep ssh
root      1027  0.0  0.1  47320  3304 ?        Ss   Jan11   0:00 /usr/sbin/sshd -D
user      2156  0.0  0.0  12916   936 pts/0    S+   15:30   0:00 grep --color=auto ssh

The second line shows the grep process itself, which is usually not desired in the results.

Methods to Exclude grep from Results

Method 1: Using grep -v

The simplest approach is to use grep -v grep to exclude lines containing "grep":

$ ps aux | grep ssh | grep -v grep
root      1027  0.0  0.1  47320  3304 ?        Ss   Jan11   0:00 /usr/sbin/sshd -D

Method 2: Using Character Class Trick

A clever technique uses a character class in the grep pattern. This makes the grep command's own command line not match the pattern:

$ ps aux | grep '[s]sh'
root      1027  0.0  0.1  47320  3304 ?        Ss   Jan11   0:00 /usr/sbin/sshd -D

The pattern [s]sh matches "ssh" but the grep command line shows grep '[s]sh', which doesn't match the pattern.

Method 3: Using awk

Use awk to exclude lines containing "grep":

$ ps aux | awk '/ssh/ && !/grep/ {print}'
root      1027  0.0  0.1  47320  3304 ?        Ss   Jan11   0:00 /usr/sbin/sshd -D

Method 4: Using pgrep

The pgrep command is specifically designed for finding processes without the grep problem:

$ pgrep -fl ssh
1027 /usr/sbin/sshd -D

The -f option matches against the full command line, and -l shows the command name.

Comparison of Methods

Method Command Pros Cons
grep -v ps aux | grep ssh | grep -v grep Simple, widely known Longer command, two grep processes
Character class ps aux | grep '[s]sh' Elegant, single command Less intuitive for beginners
awk ps aux | awk '/ssh/ && !/grep/' Flexible, powerful Requires awk knowledge
pgrep pgrep -fl ssh Purpose-built, clean output Different output format

Advanced Filtering

For more complex filtering, you can combine multiple exclusion patterns:

$ ps aux | grep -E 'ssh|http' | grep -v -E 'grep|vim'

This searches for processes containing "ssh" or "http" while excluding lines with "grep" or "vim".

Best Practices

  • Use pgrep when you only need process IDs or simple process information

  • Use the character class trick [p]rocess_name for quick one-liners

  • Use grep -v grep when the command needs to be clear and readable

  • Consider ps -C process_name to search by command name directly

Conclusion

Excluding grep from ps results is a common need when filtering processes. The character class trick [p]attern is often the most elegant solution, while pgrep provides the cleanest output for process searching. Choose the method that best fits your specific use case and scripting requirements.

Updated on: 2026-03-17T09:01:38+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements