Pgrep Command in Linux
Updated on
•6 min read

This article covers the basics of the Linux pgrep command.
pgrep is a command-line utility that allows you to find the process IDs of a running program based on given criteria. It can be a full or partial process name, a user running the process, or other attributes.
The pgrep command is a part of the procps (or procps-ng) package, which is pre-installed on nearly all Linux distributions.
How to Use the pgrep Command
The syntax for the pgrep command is as follows:
pgrep [OPTIONS] <PATTERN>
The matching <PATTERN> is specified using extended regular expressions.
When invoked without any option, pgrep displays the PIDs of all running programs that match with the given name. For example, to find the PID of the SSH server, you would run:
pgrep sshIf there are running processes with names matching “ssh”, their PIDs will be displayed on the screen. If no matches are found, the output is empty.
1039
2257
6850
31279
The command returns 0 when at least one running process matches the requested name. Otherwise, the exit code
is 1. This can be useful when writing shell scripts.
If you want to send signals to the matched processes use pkill
. This command is a wrapper around the pkill, and uses same options and pattern matching.
pgrep prints each matched process ID on a newline. The -d option allows you to specify a different delimiter. For example, if you want to use a space as a delimiter, enter:
pgrep ssh -d' '1039 2257 6850 31279
The -l option tells pgrep to show the process name along with its ID:
pgrep ssh -lpgrep uses regular expressions to perform the search operation and will list all processes that contain “ssh” in their names:
1039 sshd
2257 ssh-agent
6850 ssh
31279 ssh-agent
If you want to match only the processes which names are exactly as the search pattern, you would use:
pgrep '^ssh$' -l6850 ssh
^) character matches at the beginning of the string, and the dollar $ at the end.By default, pgrep matches only against the process name. When -f option is used the command matches against full argument lists.
pgrep -f sshUse the -u option to tell pgrep to display processes being run by a given user :
pgrep -u rootTo specify multiple users, separate their names with commas:
pgrep -u root,markYou can also combine options and search patterns. For example to print all processes and their names that run under user “mark” and contains “gnome” in their names you would type:
pgrep -l -u mark gnomeTo display only the least recently (oldest) or the most recently (newest) started processes, use the -n (for newest) or the -o (for oldest) option.
For example, to find the newest process started by the user “mark”, you would enter:
pgrep -lnu markAs you can see from the example above, you can also combine the options without a space between them and with a single dash.
To reverse the matching, i.e. to show only processes that do not match the given criteria, use the -v option. The following command will print all processes that are not being run by user “mark”:
pgrep -v -u markThe -c option tells pgrep to print only the count of the matching processes. For example to find the processes that run as user “mark”, enter:
pgrep -c -u markConclusion
The pgrep command is used to find out the PIDs of a running program based on different criteria.
For more information about pgrep command, visit the pgrep man
page or type man pgrep in your terminal.
If you have any questions or feedback, feel free to leave a comment.


