pgrep (Process Grep)
The pgrep command searches through the currently running processes and lists the Process IDs (PIDs) which match the selection criteria. It is a more efficient and script-friendly alternative to combining ps and grep.
1. Basic Usage
pgrep [options] [pattern]
2. Key Options
| Option | Name | Description |
|---|---|---|
-l |
list name | List names. Shows the process name alongside the PID. |
-a |
full list | Full command line. Displays the full command line used to start the process. |
-u |
user | Specific user. Matches only processes owned by the specified user. |
-n |
newest | Newest. Selects only the most recently started matching process. |
-x |
exact | Exact match. Matches only processes whose names exactly match the pattern. |
-c |
count | Count. Returns the total count of matching processes instead of PIDs. |
3. Practical Examples
① Finding the PID of a specific application
pgrep firefox
② Listing both PID and Process Name
pgrep -l apache
③ Finding processes owned by a specific user
pgrep -u root mysqld
④ Using with the kill command
To terminate all processes matching a pattern:
kill $(pgrep -u kim mysql)
4. [Tip] Why pgrep is better than ps | grep
When you use ps aux | grep name, the grep command itself often appears in the output. pgrep is smarter—it avoids listing itself and provides a clean list of PIDs, making it perfect for automation and shell scripts.