The practice of counting lines in the file is usually adopted by the developers to determine the length of their code or the program. They do so to find out the efficiency of the program, the program having fewer lines performing the same task compared to the program of greater lines is assumed to be more efficient.

In Linux, there are different methods to count the number of lines in the files, all of these methods are discussed in this article in detail.

How to count lines in the file in Linux

We have a text file in the home directory with the name “myfile.txt”, to display the contents of the text file, use the command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-Count-Lines-in-the-file-of-Linux-1.png" data-lazy- height="335" src="data:image/svg xml,” width=”1038″>

Method 1: Using the wc command

The one method to count the number of lines is by using the “wc” command with the “-l” flag which is used to display the count of lines:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-Count-Lines-in-the-file-of-Linux-2.png" data-lazy- height="111" src="data:image/svg xml,” width=”980″>

You can also use the wc command with the cat command to display the count of lines of a file:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-Count-Lines-in-the-file-of-Linux-3.png" data-lazy- height="121" src="data:image/svg xml,” width=”998″>

Method 2: Using the awk command

Another method to count the lines of the file in Linux is by using the command of awk:

$ awk ‘END{print NR}’ myfile.txt

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-Count-Lines-in-the-file-of-Linux-4.png" data-lazy- height="127" src="data:image/svg xml,” width=”741″>

Method 3: Using the sed command

The “sed” command can also be used in Linux to display the line count of the file, the use of the sed command for the purpose of displaying a number of lines is mentioned below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-Count-Lines-in-the-file-of-Linux-5.png" data-lazy- height="109" src="data:image/svg xml,” width=”607″>

Method 4: Using the Grep command

The “grep” command is used to search, but it can be used for counting the number of lines as well as to display them, for this purpose, run the following command and replace the “myfile.txt” with your file name in the command:

$ grep -c “.*” myfile.txt

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-Count-Lines-in-the-file-of-Linux-6.png" data-lazy- height="92" src="data:image/svg xml,” width=”582″>

In the above command, we have used the “-c” flag which counts the number of lines and “.*” is used as a regular pattern or we can say to find out the strings in the file, another way to use the grep command such that it also displays file name in output is the use of the “-H” flag:

$ grep -Hc “.*” myfile.txt

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-Count-Lines-in-the-file-of-Linux-7.png" data-lazy- height="93" src="data:image/svg xml,” width=”612″>

Method 5: Using the nl command

The number line command (nl) is used to display the numbered bullets with the lines of the file:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-Count-Lines-in-the-file-of-Linux-8.png" data-lazy- height="329" src="data:image/svg xml,” width=”1122″>

If you want to display just the number of lines, then use the awk command with the nl command:

$ nl myfile.txt | tail -1 | awk ‘{print $1}’

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-Count-Lines-in-the-file-of-Linux-9.png" data-lazy- height="105" src="data:image/svg xml,” width=”920″>

Method 6: Using the Perl language command:

Perl language command can also be used for the counting of the lines of the files in Linux, to use Perl command to count the lines of the file “myfile.txt”, execute the command:

$ perl -lne ‘END { print $. }’ myfile.txt

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-Count-Lines-in-the-file-of-Linux-10.png" data-lazy- height="91" src="data:image/svg xml,” width=”790″>

Method 7: Using While loop

Another most commonly used method to count the number of lines of the large files is using the while loop. Type the following bash script in the text file, and save it with .sh extension:

#!/bin/bash

echo “Enter the file name”

read file_name

count=0

while read

do


  ((count=$count 1))

done < $file_name

echo $count

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-Count-Lines-in-the-file-of-Linux-11.png" data-lazy- height="233" src="data:image/svg xml,” width=”505″>

Execute the bash file using the bash command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-Count-Lines-in-the-file-of-Linux-12.png" data-lazy- height="143" src="data:image/svg xml,” width=”484″>

In the above output, on the execution of the command, it asks for the file name whose lines are to be counted, types the file name, in our case, it is “myfile.txt”, so it displays the results.

Conclusion

To calculate the productivity of the programmers, the main parameter is the length of their code, which can be measured by counting the lines of the code file. In Linux, we can count lines in different ways that are discussed in this article, the most commonly used method is the wc command method.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/hammad–150×150.jpg61c81f2374c37.jpg" height="112" src="data:image/svg xml,” width=”112″>

Hammad Zahid

I’m an Engineering graduate and my passion for IT has brought me to Linux. Now here I’m learning and sharing my knowledge with the world.