8 Ways to Count Lines in a File in Linux

wc -l < [filename] on a green background

Counting lines in a Linux file can be hectic if you don’t know the applicable commands and how to combine them. This tutorial makes the process comfortable by walking you through eight typical commands to count lines in a file in Linux.

For example, the word count, wc, command’s primary role, as the name suggests, is to count words. However, since a group of words forms a line, you can use the command to count lines besides characters and words.

All you do is redirect the input of a file to the command alongside the -l flag.

wc -l < [filename]

Apart from the wc, you can use the awk, sed, grep, nl, pr, cat and perl commands. Before that, it would help to understand data streams and piping in Linux.

The concept of Data Streams and Piping

Data streams

Three files come together to complete the request when you run a command: standard input, standard output, and error files.

The standard input, abbreviated as stdin and redirected as <, feeds the computer with data. The standard output, abbreviated as stdout and redirected as >, shows the result of running a command. If an error occurs when processing the result, we see the standard error, often abbreviated as stderr.

The primary stdin is the keyboard, while the stdout is the (monitor) screen. However, due to the flexibility of Linux and the fact that everything in Linux is a file, we can change the stdin, stdout, or stderr to suit our needs, as you will see when counting lines with the wc command.

Before that, you should understand the concept of piping in Linux.

Piping

Piping in Linux, denoted by |, means running two or more commands simultaneously on the terminal. For example, we can cat a file, let’s call the file index.txt. But instead of waiting to see the output, we redirect it to the sort command, which outputs the data alphabetically.

cat index.txt | sort

Now that you understand the main concepts applied when customizing a file’s input to get the number of lines, let’s see eight ways to count lines in a file in Linux.

Ways to Count Lines in a File in Linux

WC

The wc command returns a file’s line numbers, words, and characters, respectively.

Let’s create a file, practice.txt, and append the following lines.

We are counting file lines.
We use the wc, awk, sed, grep, and perl commands.
The process is easy because we can redirect ouptut and pipe commands.
Linux is becoming fun!

Running the wc command on the file, we get the following output:

4 31 170 practice.txt

where,

  • 4: line count,
  • 31: word count,
  • 170: number of characters,
  • practice.txt: action file.

Likewise, we can control the output using specific flags with the input redirection symbol.

Output lines only:

wc -l < practice.txt
Output
4

Output words only:

wc -w < practice.txt
Output
31

Output characters only:

wc -c < practice.txt
Output
171

Since we are redirecting the input of the practice.txt file to the command, there is no need to reprint the filename.

We can also control the output by piping the cat commands result into the wc command.

The cat command reads practice.txt‘s contents. Just as it is about to return the output, we feed the outcome into the wc -l command which tells the number of lines counted.

cat practice.txt | wc -l
Output
4

AWK

The awk utility manipulates text in various ways. For example, it counts lines in a file.

awk 'END{print NR}' practice.txt
Output
4

The command treats the input as tabular data with records and columns and works on it with two primary rules: BEGIN and END. The BEGIN rule executes before the command reads the first record of input, whereas the END rule executes once only after the input is read.

In the above file, we print the number of records (NR) after reading the practice.txt file. The result is 4, which is the line count.

PERL

Like awk, we can apply the Perl scripting language to count lines in a file. We continue to apply the END rule. However, this time around, we introduce the -lne flag.

perl -lne 'END { print $. }' practice.txt
Output
4

GREP

The grep, short for Global Regular Expression and Print, is a command to filter a pattern of strings and outputs them. It comes with several flags to control the output. For example, the -c flag prints the matching pattern’s count.

Return lines number from practice.txt:

grep -c ^ practice.txt
Output
4

Count every line:

grep -c ".*" practice.txt
Output
4

SED

The sed command is used to search, find, replace, insert, and delete text in Linux. You control the output with flags and regular expressions. Examples of the flags are -i to permanently substitute a string, and -n to suppress the reprinting of lines.

sed -n '$=' practice.txt
Output
4

The sed command processes each line of the practice.txt file. Without the -n flag, the matching lines would get printed twice: during processing and automatic post-processing of the pattern space.

NL

The nl command assigns numbers to each of the given file’s lines before returning the output. Unlike with the previous commands, this time around we will have to look at the last numbered line since the last line’s number denotes the total lines if the counting starts from 1.

nl practice.txt

We get the following output:

1 We are counting file lines.
2 We use the wc, awk, sed, grep, and perl commands.
3 The process is easy because we can redirect output and pipe commands.
4 Linux is becoming fun!

Alternatively, we can use the pr command.

PR

Although the pr command’s primary role is to prepare a file for printing, we can use it with the -n flag to count lines in a file. It divides the given file into numbered columns with header, date, and page number. We can read the number of the last record to know the file’s line count.

pr -n practice.txt
Output
2022-05-22 20:45                   practice.txt                   Page 1


    1   We are counting file lines.
    2   We use the wc, awk, sed, grep, and perl commands.
    3   The process is easy because we can redirect ouptut and pipe commands.
    4   Linux is becoming fun!

CAT

cat is one of the most flexible commands in Linux. Apart from creating and printing file contents, we can use the command with the -n flag to count lines in a file.

cat -n test.txt

Besides, we can get line count by combining the cat command with the awk and tail commands.

cat -n test.txt | tail -1 | awk '{print $1}'

Conclusion

You can count lines in a file in Linux by combining the wc, sed, cat, grep, perl, awk, nl, and pr commands with flags and other commands. First, it would be best to understand redirection and piping in Linux, as guided in this tutorial.

If you have any feedback or encountered any issues please feel free to leave a comment and we’ll get back to you as soon as we can.

 

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like
How to Install MATE Themes in Linux
Read More

How to Install MATE Themes in Linux

The MATE Desktop Environment uses GNOME 2 to provide an attractive environment while preserving the traditional GNOME essence.…