How to Count lines in a file in UNIX/Linux

Question: I have a file on my Linux system having a lot of lines. How do I count the total number of lines in the file?

Using “wc -l”

There are several ways to count lines in a file. But one of the easiest and widely used way is to use “wc -l”. The wc utility displays the number of lines, words, and bytes contained in each input file, or standard input (if no file is specified) to the standard output.

The syntax is:

# wc -l [filename]

So consider the file shown below:

$ cat file01.txt
this is a sample
file
with
some sample
data

1. The “wc -l” command when run on this file, outputs the line count along with the filename.

$ wc -l file01.txt
       5 file01.txt

2. To omit the filename from the result, use:

$ wc -l 

3. You can always provide the command output to the wc command using pipe. For example:

$ cat file01.txt | wc -l
       5

You can have any command here instead of cat. Output from any command can be piped to wc command to count the lines in the output.

Using awk

If you must want to use awk to find the line count, use the below awk command:

$ awk 'END{print NR}' [filename]

Example output:

$ awk 'END{print NR}' file01.txt
5

Using sed

Use the below sed command syntax to find line count using GNU sed:

$ sed -n '$=' [filename]

Example output:

$ sed -n '$=' file01.txt
5

Using grep

Our good old friend "grep" can also be used to count the number of lines in a file. These examples are just to let you know that there are multiple ways to count the lines without using "wc -l". But if asked I will always use "wc -l" instead of these options as it is way too easy to remember.

$ grep -c ".*" [filename]

Example Output:

$ grep -c ".*" file01.txt
5

With GNU grep, you can use the below grep syntax:

$ grep -Hc ".*" [filename]

Here is another version of grep command to find line count.

$ grep -c ^ file01.txt
5

Example Output:

$ grep -Hc ".*" file01.txt
file01.txt:5

Some more commands

Along with the above commands, its good to know some rarely used commands to find the line count in a file.

1. Use the nl command (line numbering filter) to get each line numbered. The syntax for the command is:

$ nl [filename]

Example output:

$ nl file01.txt
     1 this is a sample
     2 file
     3 with
     4 some sample
     5 data

Not so direct way to get line count. But you can use awk or sed to get the count from last line. For example:

$ nl file01.txt | tail -1 | awk '{print $1}'
5

2. You can also use vi and vim with the command ":set number" to set the number on each line as shown below. If the file is very big, you can use "Shift+G" to go to the last line and get the line count.

3. Use the cat command with -n switch to get each line numbered. Again, here you can get the line count from the last line.

$ cat -n file01.txt
     1 this is a sample
     2 file
     3 with
     4 some sample
     5 data
$ cat -n file01.txt | tail -1 | awk '{print $1}'
5

4. You can also use perl one lines to find line count:

$ perl -lne 'END { print $. }' file01.txt
5
Related Post