How To

pgrep Command in Linux: Find and Filter Running Processes

The pgrep command in Linux finds the PIDs of running processes based on a name or other criteria. It is part of the procps package and is pre-installed on nearly all Linux distributions.

pgrep is a companion to pkill. Both use the same pattern matching; pgrep lists the matches while pkill sends a signal to them.

How to Use the pgrep Command in Linux

The syntax is:

bashpgrep [OPTIONS] PATTERN

The pattern is matched against the process name — the short executable name in the comm field, not the full path — using extended regular expressions. The default is a partial match: pgrep ssh matches sshsshd, and ssh-agent.

To find all PIDs matching “ssh”:

bashpgrep ssh

pgrep exits with code 0 if at least one match is found, and 1 if nothing matches. This makes it reliable in shell scripts:

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

-x requires an exact name match. Without it, pgrep nginx would also match a process named nginx-old.

To show process names alongside PIDs:

bashpgrep -l ssh

To show full command lines including arguments:

bashpgrep -a ssh

Use -a when multiple processes share the same name but run with different arguments — the full command line is the only way to distinguish them.

To count matching processes instead of listing them:

bashpgrep -c -u mark

Match the Full Command Line, Parent PID, and Exact Names

By default, pgrep only matches against the short process name. Use -f to match against the full command line including arguments:

bashpgrep -f "python3 app.py"

-f widens the search significantly. A broad pattern can match processes you did not intend to include, so use it carefully.

To anchor the regex for an exact name match:

bashpgrep -l '^ssh$'

^ anchors the match to the start of the name and $ anchors it to the end. Only a process named exactly ssh will match.

To find all child processes of a specific parent PID:

bashpgrep -P 1234

This is useful for inspecting process trees when debugging a daemon that spawns workers.

Filter by User, Terminal, Newest or Oldest, and Invert

To show only processes owned by a specific user:

bashpgrep -u mark

Specify multiple users as a comma-separated list:

bashpgrep -u root,mark

To match processes attached to a specific terminal:

bashpgrep -t pts/2

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

bashpgrep -ln ssh    # newest sshd or ssh process

To invert the match and show processes that do NOT belong to a user:

bashpgrep -v -u mark

Before running pkill, always run pgrep -a first to preview exactly which processes will be affected. Sending a signal to the wrong process can be hard to recover from.

Use pgrep to search and preview; use pkill to act. Learn -a-f, and regex anchoring to avoid unintended matches. Leave a comment below if you run into any issues.

Cyber Defence

Recent Posts

unlink Command in Linux: Remove a Single File or Symlink

The unlink command in Linux removes a single file by deleting its directory entry. It is a…

20 minutes ago

How to Check Open Ports in Linux: nmap, netcat, and Bash

When troubleshooting a network connection or configuring a firewall, the first question is whether a…

46 minutes ago

ifconfig Command in Linux: Configure Network Interfaces

The ifconfig command in Linux displays and configures network interfaces. It can assign IP addresses, bring interfaces…

1 hour ago

groupdel Command in Linux: Remove a Group and Audit Files

The groupdel command in Linux removes a group from the system. It deletes the group's entry from /etc/group and /etc/gshadow,…

1 day ago

wc Command in Linux: Count Lines, Words, Characters, and Bytes

The wc command in Linux counts lines, words, characters, and bytes in files or standard input. It…

1 day ago

top Command in Linux: Monitor Processes and Resource Usage

The top command in Linux provides a real-time view of running processes and system resource usage. From…

1 day ago