Cyberithub

How to count number of lines in a File from Linux Terminal

Advertisements

In this article, we will see how to count number of lines in a File from Linux Terminal. Sometimes you might have seen a case where you have a file and you would like to know how large that file is by checking the total number of lines in that file. Fortunately, Linux has number of open source tools or utilities that can be used to count the number of files in a file. Here we will see some of the best tools that can be used to perform this task. You can choose to use any of the utilities or commands as per your requirements and needs.

How to count number of lines in a File from Linux Terminal

How to count number of lines in a File from Linux Terminal

Also Read: How to count number of words in a File from Linux Terminal

There are multiple ways to count number of lines in a file from terminal in which one of the most preferred one is through wc command. wc is a free and open source utility in Linux used to count lines, words and characters in a file. So let's suppose if you have a file called words.txt and you are looking to count number of lines in that file then you need to use wc -l words.txt command as shown below.

cyberithub@ubuntu:~$ wc -l words.txt
109 words.txt

Similarly, if you want to count the number of words in words.txt file then you need to use wc -w words.txt command as shown below.

cyberithub@ubuntu:~$ wc -w words.txt
1253 words.txt

Likewise, if you want the count the number of characters in the file then you need to use wc -m words.txt command as shown below.

cyberithub@ubuntu:~$ wc -m words.txt
8118 words.txt

And, if you want all the information i.e number of lines, number of words and number of characters in a single command then you just need to use wc words.txt command as shown below.

cyberithub@ubuntu:~$ wc words.txt
109 1253 8118 words.txt

In case if you want to count the total number of lines from multiple files then you can do that as well using wc command. For example, to count total number of lines in both words.txt and example.py file, you can use wc -l words.txt example.py command as shown below.

cyberithub@ubuntu:~$ wc -l words.txt example.py
109 words.txt
12  example.py
121 total

Similarly, to count the total number of words in both the files, you need to use wc -w words.txt example.py command as shown below.

cyberithub@ubuntu:~$ wc -w words.txt example.py
1253 words.txt
108  example.py
1361 total

Finally, to count the total number of characters, use wc -m words.txt example.py command as shown below.

cyberithub@ubuntu:~$ wc -m words.txt example.py
8118 words.txt
851  example.py
8969 total

Like earlier, you can also summarize all the counts from words.txt and example.py file by using wc words.txt example.py command as shown below.

cyberithub@ubuntu:~$ wc words.txt example.py
109 1253 8118 words.txt
12  108  851  example.py
121 1361 8969 total

Another command that you can use to perform the same task is awk. You can also use awk utility to count number of lines in a file. For example - to count the number of lines in the same file words.txt, you need to use awk 'END{print NR}' words.txt command as shown below. You may notice that the output of awk command is exactly the same as wc command.

cyberithub@ubuntu:~$ awk 'END{print NR}' words.txt
109

Alternatively, you can use sed utility to get the total number of lines in words.txt file by using sed -n '$=' words.txt command as shown below.

cyberithub@ubuntu:~$ sed -n '$=' words.txt
109

grep is another useful utility that can be used to count the number of lines in a file. You can use grep in two ways. You can either use grep -e "$" -c words.txt command to count the number of lines.

cyberithub@ubuntu:~$ grep -e "$" -c words.txt
109

Or, you can use grep -e "^" -c words.txt command as shown below. Both ways can be used interchangeably.

cyberithub@ubuntu:~$ grep -e "^" -c words.txt
109

You also have the option to use cat, tail and awk command together to get the number of lines in words.txt file using below command.

cyberithub@ubuntu:~$ cat -n words.txt | tail -n1 | awk '{ print $1; }'
109

Leave a Comment