pgrep Command in Linux: Find and Filter Running Processes

By 

Updated on

6 min read

Using the pgrep command to find running process IDs in Linux

pgrep is a command-line utility that finds the process IDs of running programs based on given criteria. It can match against a full or partial process name, the user running the process, or other attributes.

The pgrep command is part of the procps (or procps-ng) package, which is pre-installed on nearly all Linux distributions. This guide explains how to use pgrep to search, filter, and inspect running processes.

pgrep Syntax

txt
pgrep [OPTIONS] <PATTERN>

The <PATTERN> is matched using extended regular expressions.

Finding Processes by Name

When invoked without any option, pgrep displays the PIDs of all running programs that match the given name. For example, to find the PID of the SSH server:

Terminal
pgrep ssh

If there are running processes with names matching “ssh”, their PIDs are printed one per line. If no matches are found, the output is empty.

output
1039
2257
6850
31279

The command returns exit code 0 when at least one running process matches the requested name. Otherwise, the exit code is 1. This behavior is useful when writing shell scripts.

To send signals to matched processes, use pkill , which is a companion utility that uses the same pattern matching as pgrep.

Output Options

Show Process Names (-l)

The -l option prints the process name alongside its PID:

Terminal
pgrep -l ssh
output
1039 sshd
2257 ssh-agent
6850 ssh
31279 ssh-agent

Show Full Command Lines (-a)

The -a option prints the full command line and arguments for each matched process:

Terminal
pgrep -a ssh
output
1039 /usr/sbin/sshd -D
6850 ssh user@example.com

This is more informative than -l when you need to distinguish between processes with the same name but different arguments.

Custom Delimiter (-d)

By default, pgrep prints each PID on a new line. The -d option lets you specify a different delimiter. To separate PIDs with a space:

Terminal
pgrep -d ' ' ssh
output
1039 2257 6850 31279

Count Only (-c)

The -c option prints only the count of matching processes:

Terminal
pgrep -c -u mark

Matching Options

Exact Name Match

By default, pgrep performs a regex match against the process name (comm field). To match only processes whose names exactly match the search pattern, anchor the pattern with ^ and $:

Terminal
pgrep -l '^ssh$'
output
6850 ssh
Info
The caret (^) matches the beginning of the string and the dollar sign ($) matches the end.

Full Command Line Match (-f)

By default, pgrep matches only against the process name. When the -f option is used, the command matches against the full argument list:

Terminal
pgrep -f "python3 app.py"

Match by Parent PID (-P)

The -P option matches only child processes of a given parent PID:

Terminal
pgrep -P 1234

This is useful for finding all processes spawned by a specific parent.

Filtering by User and Terminal

Filter by User (-u)

The -u option filters results to processes owned by a given user:

Terminal
pgrep -u root

To specify multiple users, separate their names with commas:

Terminal
pgrep -u root,mark

Filter by Terminal (-t)

The -t option matches processes attached to a specific terminal:

Terminal
pgrep -t pts/2

Combining Options

Options can be combined freely. For example, to print all processes and their names running under user mark that contain gnome in their names:

Terminal
pgrep -l -u mark gnome

You can also combine short options with a single dash:

Terminal
pgrep -lu mark gnome

Newest and Oldest Process

To display only the most recently started process, use -n (newest). To display only the oldest, use -o:

Terminal
pgrep -lnu mark

Invert Matching (-v)

The -v option reverses the match and shows only processes that do not match the given criteria. To print all processes not owned by user mark:

Terminal
pgrep -v -u mark

Quick Reference

CommandDescription
pgrep sshList PIDs of processes matching “ssh”
pgrep -l sshList PIDs and process names
pgrep -a sshList PIDs and full command lines
pgrep -f "python3 app.py"Match against full command line
pgrep -u markList processes owned by user “mark”
pgrep -u root,markMatch processes owned by multiple users
pgrep -P 1234Match child processes of PID 1234
pgrep -t pts/2Match processes on terminal pts/2
pgrep -n sshShow only the newest matching process
pgrep -o sshShow only the oldest matching process
pgrep -v -u markShow processes NOT owned by “mark”
pgrep -c -u markCount matching processes

Troubleshooting

pgrep shows no output, but the process is running
Use pgrep -a to see command lines and confirm the match term. If the process name differs from what you expect, try -f to match against the full command line.

pgrep matches too many processes
Use -x for exact name matching, or anchor your regex ('^name$') to avoid partial matches.

Results differ between users
Process visibility can depend on permissions. Run the command as the same user that owns the process, or use sudo when appropriate.

Script behaves incorrectly when nothing matches
Handle exit codes explicitly: 0 means a match exists, 1 means no match. Do not rely only on printed output.

You need safer validation before signaling
Use pgrep -a first to preview matches, then run pkill only after you confirm the exact targets.

FAQ

What is the difference between pgrep and ps?
ps lists processes with detailed information about each one. pgrep is focused on searching — it returns PIDs based on a pattern and is designed for use in scripts and pipelines.

What is the difference between pgrep and pkill?
pgrep only finds and lists matching processes. pkill uses the same matching logic but sends a signal to the matched processes. Use pgrep first to confirm what will be matched, then use pkill to act on it.

How do I use pgrep in a shell script?
Check the exit code: pgrep returns 0 if at least one process matched and 1 if nothing matched. For example:

sh
if pgrep -x nginx > /dev/null; then
    echo "nginx is running"
fi

Why does pgrep match more processes than expected?
By default, pgrep does a partial match. Use -x or anchor the pattern with '^name$' to require an exact match. Use -f only when you need to match against the full command line, as it broadens the search significantly.

Conclusion

pgrep is a focused process search tool that finds PIDs based on name, user, terminal, or command pattern. Use it to safely preview which processes a command will affect before acting with pkill or kill .

For the full list of options, type man pgrep in your terminal.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page