What Does “ps -ef | grep processname” Command Mean in Linux?

In Linux, the “ps -ef | grep processname” command is utilized to list specific running processes with their details that match a given name or pattern. It quickly finds out specific, multiple instances of a particular process that are running on the system. It is beneficial for troubleshooting or performance tuning.

This article will briefly explain the “ps -ef | grep processname” command along with practical implementation in Linux.

What Does “ps -ef | grep processname” Command Mean in Linux?

First, let’s understand the syntax of the “ps -ef | grep processname” command:

$ ps -ef | grep processname

The description of the syntax is as follows:

  • ps” is a command that displays information about running processes on the system.
  • ef” is an option for “ps” that tells it to show information about all processes, including those owned by other users, in a full format.
  • |” is a pipe symbol that sends the output of “ps -ef” to the next command.
  • grep” is a command that searches for a given pattern or text within a file or output.
  • processname” is the name of the process or a pattern that we want to search for in the list of running processes.

In the “ps -ef | grep processname” command, the first part of the command lists all the processes on the system. The second part filters the output to show only the processes that match the given process name or pattern.

How Does “ps -ef | grep processname” Command Work in Linux?

Here are a few examples of using the “ps -ef | grep processname” command with different options and arguments to filter and display the output:

Example 1: Find the ID of a Specific Process

An example is considered to find out the process id of the specific process in the terminal. For instance, “ps -ef” command is utilized by specifying the process name “nginx” in the following command:

$ ps -ef | grep nginx

The output shows a list of all processes containing the word “nginx” in their name, along with their process ID “120120”.

Example 2: Check Running Instances of a Specific Process 

To check the running process along with instances, execute the “ps -ef” command with the combination of “grep -c” and the process name. In our case, specify the process name “nginx” in the below command: 

$ ps -ef | grep -c nginx

The execution returns the “1” that shows that one “nginx” process instance is in a running state. 

Example 3: List All Processes Except the grep Command 

To list all processes except the grep command itself, utilize the “v” option by mentioning the process name “grep”:

$ ps -ef | grep -v grep

This command excludes the “grep” command from the output and shows all other running processes.

Example 4: Show Only the Process Name and PID

To display the process name with process id, use the “awk” command to print only the second and eighth columns of the output:

$ ps -ef | grep nginx | awk '{print $2, $8}'

The output prints values of second and eighth columns, which correspond to the PID and process name, respectively.

Note: To explore more examples regarding the “ps-ef” command, follow the link.

Conclusion

The “ps -ef | grep processname” command filters out a process from a list of running processes and displays information in the Linux system. With this command, users can find the process id, instances, process name, and many more. This guide has briefly explained the “ps -ef | grep processname” command with various examples in Linux.