Cybersecurity Updates & Tools

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.