5 Ways to Count the Number of Lines in a File

On Linux, you can do a single task in several ways. Likewise, if you want to count the number of lines in single or multiple files, you can use different commands. In this article, I’ll share five different ways including that you can use to print a total number of lines in a large file.

[ You might also like: How to Copy Large Number of Files in Linux ]

1. Count Number Of Lines Using wc Command

As wc stands for “word count“, it is the most suitable and easy command that has the sole purpose of counting words, characters, or lines in a file.

Let’s suppose you want to count the number of lines in a text file called distros.txt.

$ cat distros
View File Contents
View File Contents

You can use "-l" or "--line" option with wc command as follows:

$ wc -l distros.txt
Count Lines in File
Count Lines in File

You can see that wc also counts the blank line and print the number of lines along with the filename. In case, you want to display only the total number of lines, you can also hide the filename by redirecting the content of the file to wc using a left-angle bracket (<) instead of passing the file as a parameter.

$ wc -l < distros.txt
Print Total Lines in File
Print Total Lines in File

Moreover, to display a number of lines from more than one file at the same time, you need to pass the filenames as arguments separated by space.

$ wc --lines distros.txt distros.txt distros.txt
Count Lines in Multiple Files
Count Lines in Multiple Files

In another way, you can also make use of the cat command to redirect the file content to the wc command as input via pipe ('|').

$ cat distros.txt | wc -l
Redirect File Content
Redirect File Content

Though it will also count the number of lines in a file, here the use of cat seems redundant.

2. Count Number Of Lines Using Awk Command

Awk is a very powerful command-line utility for text processing. If you already know awk, you can use it for several purposes including counting the number of lines in files.

[ You might also like: How to Delete Empty Lines in Files Using Grep, Sed, and Awk ]

However, mastering it may take time if you’re at a beginner level. Hence, if you just want to use it to count the total number of lines in a file, you can remember the following command:

$ awk 'END {print NR}' distros.txt
Count Lines in File Using Awk
Count Lines in File Using Awk

Here, NR is the number of records or say line numbers in a file being processed at the END section.

3. Count Number Of Lines Using Sed Command

Sed is a also very useful tool for filtering and editing text. More than a text stream editor, you can also use sed for counting the number of lines in a file using the command:

$ sed -n '$=' distros.txt
Count Lines in File Using Sed
Count Lines in File Using Sed

Here, '=' prints the current line number to standard output. So, combining it with the -n option, it counts the total number of lines in a file passed as an argument.

4. Count Number Of Lines Using Grep Command

Using yet another useful pattern search command grep, you can get the total number of lines in a file using '-e' or '--regexp' and '-c' or '--count' options.

$ grep -e "$" -c distros.txt
Or 
$ grep -e "^" -c distros.txt
Count Lines in File Using Grep
Count Lines in File Using Grep

Here, '$' is a regular expression representing the end of a line and the '^' start of a line. You can use either of the regular expression.

[ You might also like: How to Find Files Containing Specific Text String in Linux ]

5. Count Number Of Lines Using nl and cat Commands

Instead of directly getting the total no of lines, you can also print the file content and get the total number of lines by peeking at the last line number. For such purpose, nl is a simple command to print data with numbered lines.

$ nl distros.txt
Print Numbering Lines in File
Print Numbering Lines in File

For large files, it does not seem like a suitable method to display all data in a terminal. So what you can also do is pipe the data to tail command to just print only some of the last numbered lines.

$ nl distros.txt | tail -n2
List Numbering Lines in File
List Numbering Lines in File

Likewise, a cat command with '-n' can also be used to print file content with line numbers.

$ cat -n distros.txt | tail -n1
Print File Content With Line Numbers
Print File Content With Line Numbers
Conclusion

After learning five ways to count a number of lines, you must be wondering which is the best way for you? In my opinion, whether you’re a beginner or advanced user of Linux, the wc command is the easiest and fastest approach.

However, if you’re in process of learning other powerful tools like grep, awk, and sed, you can also practice these commands for such purpose to hone your skills.

1 thought on “5 Ways to Count the Number of Lines in a File”

Got something to say? Join the discussion.