We use cookies to improve our services and provide a better experience. By continuing to use this site, you agree with our Cookies Policy.

Tutorials
More than 400 step-by-step articles to guide you through online project development.
HomeTutorialsHosting ServicesCloud ServersAll Tutorials

How to Count Number of Lines or Processes in Linux

In order to count how many lines there are in the file or to get the number of processes, we can use Linux shell command wc. Let’s say we have a file with the following entries:

 

one
two
three
four
five
six
seven
eight
nine
ten

 

We can say there are 10 lines, but if there would be more then 10 lines we should use wc. It can be used as follows:

 

cat file.txt | wc -l

10

This command will go through each line in the file and counter will sum up them. Now let’s say we have such entries in the file:

 

one
two
three
three
three
three
seven
eight
nine
ten

 

To count how many words we have there of three, we can use the same shell counter, but we will also have to filter them. In this case, we will be using command grep. The full command should look as follows:

 

cat file.txt | grep three | wc -l

 

There we are going through entire file and searching for words three. The needed words is being counted by wc -l. In our example, we should get number 4 in the output.

 

We can use the same method to count how many processes there are running of particular user. Generally, we can check all processes with command ps aux. But if we have hunderds of other processes running in the same server, it’s getting harder to find specific processes.

 

Let’s say we want to get number of apache processes. Then we need to execute the following command:

 

ps aux | grep apache | wc -l

 

With the  help of mentioned command we are listing all processes and searching for lines containing word apache. All affected lines is being counted and we should see specific number in the output.

 

Rate this Tutorial:
No CommentsLeave a Comment

Other (3)

Popular Keywords