pgrep Command in Linux: Find and Filter Running Processes

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
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:
pgrep sshIf there are running processes with names matching “ssh”, their PIDs are printed one per line. If no matches are found, the output is empty.
1039
2257
6850
31279The 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:
pgrep -l ssh1039 sshd
2257 ssh-agent
6850 ssh
31279 ssh-agentShow Full Command Lines (-a)
The -a option prints the full command line and arguments for each matched process:
pgrep -a ssh1039 /usr/sbin/sshd -D
6850 ssh user@example.comThis 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:
pgrep -d ' ' ssh1039 2257 6850 31279Count Only (-c)
The -c option prints only the count of matching processes:
pgrep -c -u markMatching 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 $:
pgrep -l '^ssh$'6850 ssh^) 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:
pgrep -f "python3 app.py"Match by Parent PID (-P)
The -P option matches only child processes of a given parent PID:
pgrep -P 1234This 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:
pgrep -u rootTo specify multiple users, separate their names with commas:
pgrep -u root,markFilter by Terminal (-t)
The -t option matches processes attached to a specific terminal:
pgrep -t pts/2Combining 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:
pgrep -l -u mark gnomeYou can also combine short options with a single dash:
pgrep -lu mark gnomeNewest and Oldest Process
To display only the most recently started process, use -n (newest). To display only the oldest, use -o:
pgrep -lnu markInvert 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:
pgrep -v -u markQuick Reference
| Command | Description |
|---|---|
pgrep ssh | List PIDs of processes matching “ssh” |
pgrep -l ssh | List PIDs and process names |
pgrep -a ssh | List PIDs and full command lines |
pgrep -f "python3 app.py" | Match against full command line |
pgrep -u mark | List processes owned by user “mark” |
pgrep -u root,mark | Match processes owned by multiple users |
pgrep -P 1234 | Match child processes of PID 1234 |
pgrep -t pts/2 | Match processes on terminal pts/2 |
pgrep -n ssh | Show only the newest matching process |
pgrep -o ssh | Show only the oldest matching process |
pgrep -v -u mark | Show processes NOT owned by “mark” |
pgrep -c -u mark | Count 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:
if pgrep -x nginx > /dev/null; then
echo "nginx is running"
fiWhy 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.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

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