LinuxCommandLibrary

pgrep

TLDR

Find process by name

$ pgrep [nginx]
copy
Find with full command line match
$ pgrep -f "[python script.py]"
copy
List process names and PIDs
$ pgrep -l [ssh]
copy
Find processes by user
$ pgrep -u [username] [process]
copy
Find newest matching process
$ pgrep -n [chrome]
copy
Find oldest matching process
$ pgrep -o [firefox]
copy
Count matching processes
$ pgrep -c [apache]
copy
Exact name match
$ pgrep -x [bash]
copy

SYNOPSIS

pgrep [-flnoxc] [-u user] [-g group] [-P ppid] [pattern]

DESCRIPTION

pgrep searches for processes by name or attributes, returning their PIDs. It's a more focused alternative to piping ps through grep.
Pattern matching is against the process name by default (executable name). The -f flag extends matching to the full command line including arguments. Regular expressions are supported.
Selection filters narrow results by user, group, terminal, or parent process. These can be combined for precise targeting. The -v flag inverts selection, finding processes that don't match.
Newest (-n) and oldest (-o) options return single matches, useful when multiple instances exist. Count mode (-c) reports how many processes match without listing them.
Output formatting options control delimiter and detail level. These enable integration with shell scripts and other tools.

PARAMETERS

-l, --list-name

List PID and process name.
-a, --list-full
List PID and full command line.
-f, --full
Match against full command line.
-x, --exact
Match exactly (not substring).
-n, --newest
Select newest (most recent) match.
-o, --oldest
Select oldest (first) match.
-c, --count
Count matches instead of listing.
-d DELIM, --delimiter DELIM
Output delimiter (default: newline).
-u USER, --euid USER
Match effective user ID.
-U USER, --uid USER
Match real user ID.
-g GROUP, --pgroup GROUP
Match process group.
-G GROUP, --group GROUP
Match real group ID.
-P PPID, --parent PPID
Match parent process ID.
-t TERM, --terminal TERM
Match controlling terminal.
-v, --inverse
Invert match (negation).
-i, --ignore-case
Case-insensitive matching.

CAVEATS

Matches are substring by default - "sh" matches bash, fish, zsh. Use -x for exact matching. Process names may be truncated. Zombie processes may still match. Pattern is a regular expression. pgrep cannot find itself.

HISTORY

pgrep was introduced in Solaris 7 (1998) and ported to Linux as part of procps. It provides a cleaner interface than the traditional `ps aux | grep pattern` approach, avoiding the common "grep matching itself" problem. The tool is part of the procps-ng package on most Linux systems.

SEE ALSO

pkill(1), ps(1), pidof(1), kill(1)

Copied to clipboard