If you’re familiar with the grep command in Linux, you know it searches for a pattern and prints the matching text. But what if you need to filter process information on your system?
Enter pgrep, a command-line tool designed precisely for this purpose. This tutorial will demonstrate the basics of pgrep using straightforward examples. Please note, all examples here were tested on Ubuntu 20.04 LTS and Debian 10.
Understanding the Linux pgrep Command
The pgrep command in Linux is used to search for processes based on specific criteria such as name and user attributes. Below is its syntax:
pgrep [options] pattern
According to the tool’s man page:
pgrep looks through the currently running processes and lists the process IDs which match the selection criteria to stdout. All criteria have to match.
Here are some question-and-answer style examples to help you grasp how pgrep operates:
Q1: How to Find the Process ID Owned by a Specific User?
Use the -u option to specify the user. For instance, here’s how to find the process ID of ‘gedit’ owned by user ‘himanshu’:
pgrep -u himanshu gedit
Q2: How to Display the Count of Matching Processes?
To get the number of processes without listing their IDs, use the -c option. Here’s how to find the count of processes owned by ‘himanshu’:
pgrep -c -u himanshu
83
Q3: How to Use a Custom Delimiter in Output?
By default, process IDs are separated by newlines. You can change the delimiter with the -d option. For example, using a colon as a delimiter:
pgrep -u himanshu -d:
1793:1794:1807:1811:1813:1817:1820:1914:1917:1922:1925:1936:1938:1954:1974:1978:1980:1982:1993:1999:2008:2009:2012:2020:2024:2034:2036:2043:2048:2049:2051:2052:2055:2064:2068:2073:2074:2085:2088:2093:2094:2095:2098:2101:2104:2117:2125:2161:2162:2168:2173:2182:2201:2213:2233:2245:2266:2279:2388:2409:2430:2456:2473:2564:2647:3085:3108:3178:3284:3297:3320:3325:3467:3487:3980:4040:4658:5668:5721:5777:6271:6281:6512:6808
Q4: How to Make pgrep Search Case Insensitively?
By default, pgrep is case-sensitive. To make it case-insensitive, use the -i option:
pgrep -i gedit
Q5: How to List Process Names Alongside IDs?
To display process names with their IDs, use the -l option. Here’s an example:
pgrep -u himanshu -l
1793 systemd 1794 (sd-pam) 1807 gnome-keyring-d ...
Q6: How to List Full Commands of Processes?
To view the complete command used to initiate each process, apply the -a option:
pgrep -u himanshu -a
1793 /lib/systemd/systemd --user 1794 (sd-pam) 1807 /usr/bin/gnome-keyring-daemon --daemonize --login ...
Q7: How to Display Only the Newest Process?
To show only the most recently launched process, use the -n option:
pgrep -u himanshu -n -l
7163 thunderbird
Q8: How to Display Only the Oldest Process?
Use the -o option to show only the oldest process:
pgrep -u himanshu -o -l
1793 systemd
Conclusion
As demonstrated, pgrep is an incredibly useful command for managing processes. Once familiar with these options, explore the tool’s man page for further learning.
Frequently Asked Questions (FAQ)
What does pgrep stand for?
pgrep stands for “process grep,” a tool used to search and filter processes based on various criteria.
Can pgrep search multiple patterns at once?
Yes, you can search multiple patterns by combining them with logical operators. See the man page for more details.
Is pgrep installed by default on all Linux distributions?
pgrep is generally included with most Linux distributions, but if it’s missing, you might need to install the procps package.
What is the difference between pgrep and ps?
While both commands are used to list processes, pgrep is tailored for searching through processes quickly, whereas ps provides more detailed information and is used for more comprehensive reports.
Can pgrep be used in scripts?
Absolutely, pgrep is often used in scripts for process management due to its capability to filter and manipulate process lists efficiently.