Linux pgrep Command Tutorial for Beginners (10 Examples)
On this page
- Linux pgrep command
- Q1. How to find ID of a process owned by a specific user?
- Q2. How to make pgrep print count of matching processes?
- Q3. How to use a custom delimiter in output?
- Q4. How to make pgrep search case insensitive?
- Q5. How to make pgrep list process names as well?
- Q6. How to make pgrep list full command as well?
- Q7. How to make pgrep only display the newest process?
- Q8. How to make pgrep only display the oldest process?
You might already know about the grep command in Linux, which searches for a pattern, and then prints the matching text in the output. What if the requirement is to apply this kind of processing to fetch select information about processes currently running in the system?
Well, you'll be glad to know that there exists a command line tool - pgrep - that lets you do exactly this. In this tutorial, we will discuss the basics of pgrep using some easy to understand examples. But before we do that, it's worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine.
Linux pgrep command
The pgrep command in Linux lets users look up processes based on name and other attributes. Following is its syntax:
pgrep [options] pattern
And here's how the tool's man page explains it:
pgrep looks through the currently running processes and lists the process IDs which match the
selection criteria to stdout. All the criteria have to match.
Following are some Q&A-styled examples that should give you a good idea on how the pgrep command works.
Q1. How to find ID of a process owned by a specific user?
This can be done using the -u command line option. For example, to find the ID of the 'gedit' process owned by user 'himanshu', use pgrep in the following way:
pgrep -u himanshu gedit
Here's the output this command produced in my case:
Q2. How to make pgrep print count of matching processes?
In case you want the tool to just print the count, and not the IDs themselves, use the -c command line option. For example, to know the count of processes owned by user 'himanshu,' run the following command:
pgrep -c -u himanshu
Following is the output this command produced on my system:
Q3. How to use a custom delimiter in output?
By default, the process IDs in output are delimited by a newline. However, if you want, you can change the delimiter, something which you can do using the -d command line option.
For example, I wanted to use a colon (:) as a delimiter, so I executed the pgrep command in the following way:
pgrep -u himanshu -d:
And following is the output the command produced:
Q4. How to make pgrep search case insensitive?
By default, the pgrep search is case sensitive. However, if you want, you can make it case insensitive. For this, you have to use the -i command line option.
pgrep -i gedit
Q5. How to make pgrep list process names as well?
For this, use the -l command line option.
Here's an example:
pgrep -u himanshu -l
Here's an excerpt of the output produced on my system:
So you can see that in addition to process IDs, process names were also produced in the output.
Q6. How to make pgrep list full command as well?
In case you want pgrep to display the complete command that was used to launch each process, use the -a command line option.
pgrep -u himanshu -a
1793 /lib/systemd/systemd --user
1807 /usr/bin/gnome-keyring-daemon --daemonize --login
1811 /usr/lib/gdm3/gdm-x-session --run-script env GNOME_SHELL_SESSION_MODE=ubuntu gnome-session --session=ubuntu
1813 /usr/lib/xorg/Xorg vt2 -displayfd 3 -auth /run/user/1000/gdm/Xauthority -background none -noreset -keeptty -verbose 3
1817 /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
1820 /usr/lib/gnome-session/gnome-session-binary --session=ubuntu
1914 /usr/bin/ssh-agent /usr/bin/im-launch env GNOME_SHELL_SESSION_MODE=ubuntu gnome-session --session=ubuntu
1922 /usr/lib/gvfs/gvfsd-fuse /run/user/1000/gvfs -f -o big_writes
1936 /usr/bin/dbus-daemon --config-file=/usr/share/defaults/at-spi2/accessibility.conf --nofork --print-address 3
1938 /usr/lib/at-spi2-core/at-spi2-registryd --use-gnome-session
1974 ibus-daemon --xim --panel disable
1980 /usr/lib/ibus/ibus-x11 --kill-daemon
Q7. How to make pgrep only display the newest process?
If instead of all processes, you want pgrep to output only the most recent process, then this can be done using the -n command line option.
Here's an example:
pgrep -u himanshu -n -l
And following is the output this command produced:
I can confirm that Thunderbird was indeed the most recent process that was launched by the user 'himanshu.'
Q8. How to make pgrep only display the oldest process?
For this, use the -o command line option.
pgrep -u himanshu -o -l
And here's the output this command produced:
So you can see that pgrep is an extremely helpful command. Once you are done practicing the command line option we've discussed here, you can head to the tool's man page to learn more about it.