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 this task.

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

Further reading:

How to Count Words in a File

Learn how to count words in a file.

Write to a Text File in Bash

Explore various ways to utilize the feature of writing to a file directly from a Bash terminal.

Looping Through the Content of a File in Bash

In this tutorial, we'll see how to loop through the contents of a file, line by line. This might sound like a trivial task, but there are some caveats that we need to be aware of.

2. Setup

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

$ cat programming.txt

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. However, 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’s printed the number 10, which indicates the total number of lines, and the name of the file, 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

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

$ cat programming.txt | wc -l

4. sed

sed is a stream editor that’s 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

From the results, we can see that the command has printed the number of lines only. However, this approach isn’t 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

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

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 brief article, we 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.

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

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.