> ## Content Index
> Fetch the complete content index at: https://linuxhandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# pgrep Command Examples
- URL: https://linuxhandbook.com/pgrep/
- Published: 2023-02-14T14:29:15.000Z
- Updated: 2024-02-17T14:02:54.000Z
- Description: Want to look for a process and its details? The pgrep command helps you with that. Here's how to use it.
- Author: Sagar Sharma
- Tags: Process Commands, Commands, #process-commands, #docs-col

Like the [grep command used to find strings from files and output](https://linuxhandbook.com/what-is-grep/), the pgrep command does the same for the processes.

In simple terms, the pgrep command will [get you the PID of running processes](https://linuxhandbook.com/find-process-id/).

So in this guide, I will walk you through various examples of how you can use the pgrep command.

## Using the pgrep command on Linux

To use the pgrep command, you will have to follow the given command syntax:

```
pgrep [options] pattern
```

Here, the `pattern` is where you will specify the parameters for the output.

Now, let's have a look at some examples.

### Find the PID of a specific process using the pgrep command

To find the PID of a specific process, all you have to do is follow the given syntax:

```
pgrep -u username process_name
```

So let's say I want to find the process ID of ssh for the user `sagar` so I will be using the following command:

```
pgrep -u sagar ssh
```

![get the PID of specific service using the pgrep command on linux](https://linuxhandbook.com/content/images/2023/01/get-the-PID-of-specific-service-using-the-pgrep-command-on-linux-1.png)

### Get the number of processes owned by the user 

If you want to know the number of processes being utilized by the specific user, append the username with `-c` and `-u` flag to the pgrep command as shown:

```
pgrep -c -u username
```

For example, here, I looked for the number of processes owned by a user named `sagar`:

```
pgrep -c -u sagar
```

![get the number of processes owned by user on linux using the pgrep command](https://linuxhandbook.com/content/images/2023/01/get-the-number-of-processes-owned-by-user-on-linux-using-the-pgrep-command-1.png)

And if you want to **combine the processes count of multiple users,** you can separate them by column:

```
pgrep -c -u user-1,user-2
```

For example, here, I combined the processes of two users: sagar and root:

![get process count of multiple users on linux using the pgrep command](https://linuxhandbook.com/content/images/2023/01/get-process-count-of-multiple-users-on-linux-using-the-pgrep-command-1.png)

### Search processes in a case-insensitive manner

By default, the pgrep utility operates in a case-sensitive mode so you may not be able to find the desired processes.

But you can switch to the case-insensitive mode using the `-i` flag:

```
pgrep -i process_name
```

For example, here, I looked for firefox but used all capitals and it returned the right result:

```
pgrep -i FIREFOX
```

![Search processes in a case insensitive manner using the pgrep command on linux](https://linuxhandbook.com/content/images/2023/01/Search-processes-in-a-case-insensitive-manner-using-the-pgrep-command-on-linux-1.png)

### List ongoing processes with their names and PIDs 

If you want to list every ongoing process with their names and PID, it can easily be done using the `-l` flag:

```
pgrep -u username -l
```

For example, here I listed every ongoing process relative to the user named `sagar`:

```
pgrep -u sagar -l
```

![List ongoing processes with their names and PIDs using the pgrep command on linux](https://linuxhandbook.com/content/images/2023/01/List-ongoing-processes-with-their-names-and-PIDs-using-the-pgrep-command-on-linux.png)

### Find which command was used to start the ongoing process

The pgrep utility also allows you to see the command used to start the ongoing process by using the `-a` flag:

```
pgrep -u username -a
```

So let's say I'm looking for commands that executed ongoing processes for user root, then the following command should get my job done:

```
pgrep -u root -a
```

![Find which command was used to start the ongoing process in linux](https://linuxhandbook.com/content/images/2023/01/Find-which-command-was-used-to-start-the-ongoing-process-in-linux.png)

### Find the most recently started process 

If you want to find the most recent ongoing process, it can easily be found using the `-n` flag: 

```
pgrep -n 
```

But I would recommend pairing `-n` with `-l` to have more useful output as it will add use process name too:

```
pgrep -n -u sagar -l
```

![find the most recent ongoing process on linux using the pgrep command](https://linuxhandbook.com/content/images/2023/01/find-the-most-recent-ongoing-process-on-linux-using-the-pgrep-command.png)

### Find the oldest ongoing process on Linux

In case, if you want to find the oldest ongoing process on your system, the pgrep utility can do that when executed with `-o` flag: 

```
pgrep -o
```

For example, if I want to find the oldest ongoing process for my user (sagar), I will be using the following command:

```
pgrep -u sagar -o -l
```

![Find the oldest ongoing process using the pgrep command ](https://linuxhandbook.com/content/images/2023/01/Find-the-oldest-ongoing-process-on-Linux-using-the-pgrep-command.png)

## But how about killing unnecessary processes?

Once you find out the set of unnecessary processes, you can use the various signals to kill them. 

Don't know how to? Well, we have a detailed guide on how to kill processes from the terminal in Linux:

[How to Use Kill Command in Linux?Found a misbehaving process? Here’s how to teach a lesson to it by terminating it using various commands.![](https://linuxhandbook.com/content/images/size/w256h256/2021/08/Linux-Handbook-New-Logo.png)Linux HandbookTeam LHB![](https://linuxhandbook.com/content/images/2022/05/kill-process-linux.png)](https://linuxhandbook.com/kill-process/)

And if you are interested in learning more about signals to terminate the process, we have your back:

[How to use SIGINT and other Termination Signals in LinuxTerminating executing process is more than just kill -9\. Here are some of the prominent termination signals and their usage.![](https://linuxhandbook.com/content/images/size/w256h256/2021/08/Linux-Handbook-New-Logo.png)Linux HandbookSagar Sharma![](https://linuxhandbook.com/content/images/2022/10/termination-signals-linux.png)](https://linuxhandbook.com/termination-signals/)

I hope you will find the given guide helpful and if you have any queries or suggestions, feel free to let me know in the comments.