Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

Sometimes, we want to check how large or small a given file is. The total number of lines of a given file helps us to know how big that file is. Linux, like any other operating system, provides us with several ways of achieving the task.

In this tutorial, we’ll look at the most common ways of counting the number of lines of a specified file using Bash.

2. Setup

For this quick tutorial, we’re going to use a text file called programming.txt that contains some of the top programming languages in use today:

$ cat programming.txt
JavaScript
Java
C
Python
C#
PHP
C++
Go
R
Ruby

If we count the number of lines manually, we’ll get 10. Counting manually will become tiresome and hard to achieve as the number of lines increases. Getting the total number of lines can easily be achieved by using various terminal commands.

3. wc

The wc command is used to find the number of lines, characters, words, and bytes of a file.

To find the number of lines using wc, we add the -l option. This will give us the total number of lines and the name of the file.

Let’s check the number of lines of our file using the wc -l command:

$ wc -l programming.txt
10 programming.txt

From the output, we can see that it has printed the number 10, which indicates the total number of lines, and the name of the file, which is programming.txt.

We can tell the shell to redirect the programming.txt file to standard input of the wc -l command. This will give us the number of lines without the filename.

$ wc -l < programming.txt
10

Another common way to do this with wc is to use cat and pipe the file to the command:

$ cat programming.txt | wc -l
10

4. sed

sed is a stream editor that is used to perform basic text transformation of an input file. This command is mostly used for find-and-replace functionality. We can also use it to find the number of lines of a specified file.

sed can receive different arguments in order to print the line numbers.

4.1. sed -n ‘=’

We can use the combination of sed, the -n option, and an equal sign (‘=’). The command will print the line numbers without the content of the file:

$ sed -n '=' programming.txt
1
2
3
4
5
6
7
8
9
10

From the results, we can see that the command has printed the number of lines only. However, this approach is not efficient for large files.

4.2. sed -n ‘$=’

Most of the time, we prefer to just get the overall number of lines. sed comes in handy by using the -n option and the ‘$=’ argument. The output of the command is the number of the last line of a file:

$ sed -n '$=' programming.txt
10

5. awk

The awk command treats every line as a record. The number of lines can then be printed in the END section using awk‘s built-in NR variable:

$ awk 'END { print NR }' programming.txt
10

6. cat

The cat command concatenates the files passed to it as arguments and prints on the standard output. This is one of the most used commands. Using the cat command with the -n option prints the file contents with their line numbers:

$ cat -n programming.txt
     1	JavaScript
     2	Java
     3	C
     4	Python
     5	C#
     6	PHP
     7	C++
     8	Go
     9	R
    10	Ruby

We can see that the command has printed both the line numbers and the content. Note that this approach is impractical when dealing with large files.

7. Conclusion

In this short tutorial, we’ve learned several ways of counting the number of lines in a file using Bash.

Some of the commands that print the contents of the files become inefficient and impractical when working with large files.

Using the wc -l command is the most used and also the easiest way to find line numbers of a given file.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!