
pgrep Command in Linux
The pgrep command in Linux searches the process by name and returns its process ID (PID). It finds running processes and prints their process IDs (PIDs) if they match the specified criteria. Moreover, it simplifies the process of finding running processes without manually parsing the ps output.
Table of Contents
Here is a comprehensive guide to the options available with the pgrep command â
Syntax of pgrep Command
The syntax of the pgrep command in Linux is as follows −
pgrep [options] [pattern]
In the above syntax, the [options] field is used to specify various options to change the commandâs output. The [pattern] field is used to specify the pattern, a search term used to match process names.
pgrep Command Options
The options of the Linux pgrep command are listed below −
Flag | Option | Description |
---|---|---|
-a | --list-full | Uses full process name to match |
-c | --count | Counts of matching processes |
-d delimiter | --delimiter delimiter | Specifies a custom separator for process IDs in the output. The default is a newline. (pgrep only.) |
-f | --full | Searches the entire command line instead of just the process name. |
-g pgrp,... | --pgroup pgrp,... | Filters processes by the given process group IDs. Group 0 represents pgrep's or pkill's own process group. |
-G gid,... | --group pgrp,... | Selects processes based on their real group ID. Accepts both numeric and symbolic values. |
-i | --ignore-case | Matches case-insensitively |
-l | --list-name | Displays the process name along with its ID. (pgrep only.) |
-n | --newest | Chooses only the most recently started process that matches. |
-o | --oldest | Chooses only the earliest started process that matches. |
-P ppid,... | --parent ppid,... | Filters processes by their parent process ID. |
-s sid,... | --session sid,... | Selects processes with the given session ID. Session ID 0 refers to pgrep's or pkill's own session. |
-t term,... | --terminal term,... | Filters processes associated with a specific controlling terminal. The terminal name should be given without the /dev/ prefix. |
-u euid,... | --euid euid,... | Matches processes by effective user ID. Both numeric and symbolic values are supported. |
-U uid,... | --uid uid,... | Matches processes by real user ID. Accepts both numeric and symbolic values. |
-v | --inverse | Reverses the match, selecting processes that do not meet the criteria. |
-x | --exact | Ensures only exact matches for process names (or full command line if -f is used). |
-h | --help | Displays the usage help. |
-V | --version | Outputs version information and exit |
Examples of pgrep Command in Linux
This section explores the usage of the pgrep command in Linux with examples −
- Finding all PIDs of a Process
- Displaying Process Names with Process IDs
- Listing all Processes Owned by a User
- Matching only the Exact Process Name
- Searching for a Process using an Entire Command Line
- Displaying the Most Recently Started Process
- Displaying the Oldest Process
- Listing Processes Associated with Specific Terminal
- Displaying Process with the Specified Parent ID
- Displaying All Processes Except the Specified Process
- Displaying the Process Count
- Displaying Usage Help
Finding all PIDs of a Process
To display the PIDs of all the running processes of the specified patterns, use the pgrep command in the following way −
pgrep bash

Displaying Process Names with Process IDs
To display the process name with the process ID, use the -l or --list-name option −
pgrep -l bash

Listing all Processes Owned by a User
To list all the processes owned by a user, use the pgrep command with the -u or --euid option and the username −
pgrep -u sam

The above displays only the PIDs. To display the names along with PIDs, use the following command −
pgrep -l -u sam

Matching only the Exact Process Name
To match the exact process name, use the -x or --exact option −
pgrep -x python3

The -x option ensures that only processes named exactly python3 are matched. If a process name contains additional characters, for example, python3.9 or python3_script, it will not be listed.
Searching for a Process using an Entire Command Line
To search for a process using an entire command line, use the -f or --full option −
pgrep -f "python3 myscript.py"

Displaying the Most Recently Started Process
To display the most recently started process, use the -n or --newest option −
pgrep -n bash

The above command displays the newest bash process.
Displaying the Oldest Process
To display the oldest process, use the -o or --oldest option −
pgrep -o bash

The above command displays the earliest started bash process.
Listing Processes Associated with Specific Terminal
To list processes associated with a specific terminal, use the -t or --terminal option −
pgrep -t tty2

Displaying Process with the Specified Parent ID
To display the process with the parent PID, use the -P or --parent option with the pgrep command −
pgrep -P 2542

Displaying All Processes Except the Specified Process
To display all the processes except the specified one, use the -v or --inverse option −
pgrep -v firefox

Displaying the Process Count
To display the process count of the specified process, use the -c or --count option with the pgrep command −
pgrep -c bash

Displaying Usage Help
To print the usage help of the pgrep command, use the -h or --help option −
pgrep -h
Conclusion
The pgrep command in Linux helps find process IDs (PIDs) based on process names or command-line patterns. It simplifies process searches by eliminating the need to manually parse ps output.
Various options allow filtering by user, parent process, group, session, or exact matches. Additional flags can modify the output format, display process names, or refine searches using command-line arguments. In the examples, we demonstrated how to list PIDs, match exact names, find processes by the user, and filter results based on criteria like terminal association or process hierarchy.