5 different ways to count the number of lines from a file

While working on a file, you may be required to count the number of lines, or else you want to know how many lines have been written to a specific text file. So to find it, you will see five different ways to count the number of lines in a Linux, which will be very easy to use.

Before proceeding ahead, let me create a dummy file that will hold a number of new lines of data to test out the command capability.

Create a sample file to count the number of lines

One of the easiest ways to create a long list of files is to redirect the output of /etc filename to filename.txt. We have recently covered an article on exa: Next-gen listing command in Linux for a pro, which you can use as an alternative to the ls command to list out the files.

If you are not familiar with the exa command, then you may run the ls -l command to create dummy data from /etc directory.

    exa -l /etc > filename.txt
         OR
    ls -l /etc > filename.txt

1. wc command to count the total number of lines from a file

There are several commands available to count the number of lines in Linux but wc command is one of the most preferred commands to count the number of lines from the respective file.

By default, wc command is bound to print newline, word, and byte counts from a corresponding file. To change the default print options, you can use -l or –lines to find the total number of lines belonging to an individual file.

    wc -l filename.txt
      OR
    wc --lines filename.txt
wc command to count the total number of lines from a file
wc command to count the total number of lines from a file

As you can see, I have 249 lines of data present in a “filename.txt”. Even if the file contains a new line that is empty, it will get counted, so make sure to delete the blank line for the precise number.

At once, you can specify multiple filenames to find the number of lines. Similar to the below example, when you have n number of files and want to know the number of lines per individual file with the total figure, then you should invoke the following command: 

    wc -l filename.txt sample.txt demo.txt
Find the number of lines from multiple files
Find the number of lines from multiple files

If you don’t want to print out the filename next to the total number of lines, then pass the below command, which will redirect the content of the file to wc instead of passing the file as a parameter:

    wc -l < filenames.txt
No file name is added to the output
No file name is added to the output

2. cat command to count the number of lines from a file

You may be using the cat command to read a file, but only a few of you know that you can enable the cat command to display the number of lines from a file.

Similar to that, there are more hidden options which you can learn about by following this link: How to use cat and tac commands in Linux

To print the number of files along with file content using the cat command you can use the -n or –number option.

    cat -n filename.txt
cat command to count the number of lines in a file
cat command to count the number of lines in a file

3. nl command to count the number of lines in a file

Why do we always use options of other commands to know the number of lines present in a specific file? Then I should introduce you dedicated command to display the number of lines which is similar to the cat command but no options are required. To print the number of lines present in a file named “filename.txt”, use the nl command:

The nl command is dedicatedly used to add the number of lines to corresponding files.

Let’s see how we could add the number line to our filename.txt. It’s quite simple. You just need to add the nl command and pass the file name.

    nl filenames.txt
nl command to add number line to file
nl command to add number line to file

As you can see the output is identical to the cat command

4. grep command to count the number of lines in a file

The above two commands are acceptable when you want to read a file with the number line to find the actual location of specific data.

But what if I just want to find the total number of lines consisting in a specific file, and importantly, without printing data on STDOUT?

In this scenario, you can use the allrounder grep utility to extract the total number of lines in the output without displaying the filename. Not only that, you can also use it in bash scripting to find the total number of lines in a millisecond.

    grep -e "$" -c filename.txt
       OR
    grep -e "^" -c filename.txt
grep command to count the number of lines
grep command to count the total number of lines
  • -e: enable extended regex support
  • "$" or "^": allow regex to count data from the ($) first or (^) last line
  • -c: option is to use count number
  • filename.txt: dummy file for data

Anyway, if you want to have data along with number lines, then you can use -n or --line-number.

    grep -n '$' filename.txt
            OR
    grep -n '^' filename.txt
grep command to print number line with data
grep command to print number line with data

5. sed command to count the number of lines in a file

sed stream editor command can manipulate files as per your intend. Similar to the grep command, it prints the total number of lines present in the file. It is capable of doing much more than counting the number of lines in a file, although that is its most obvious use.

The usage of sed to count the number of lines in a file is as follows.

 sed -n '$=' filename.txt 
sed command to count total number of lines
sed command to count the total number of lines
  • -n: suppress to print file data
  • $=: dollar sign with equal will display the total number of lines

With the usage of the above command, you can find the total number of lines, but if you want data to also print, run the below command and make sure to replace filename with the correct one:

sed -n '{=;p}' filename.txt | sed "N;s/\n/ /g"
sed command to print number line with all data
sed command to print number line with all data
  • -n: suppress to print file data
  • ‘{=;p}’: “=” count line, ‘;’ is used to separate different commands, “p” print pattern
  • filename.txt: specify the filename
  • sed “N;s/\n/ /g”: To make output readable you can again use sed command using pipe command to clear newline space and put the data inline

Quite simple, huh?

TL;DR version of 5 different ways to count the number of lines in a file

  • Count the number of lines using the wc command
    wc -l filename.txt
               OR
    wc --lines filename.txt
  • Count the number of lines using the cat command
     cat -n filename.txt
  • Count the number of lines using the nl command
      nl filename.txt
  • Count the total number of lines using the grep command without printing data
    grep -e "$" -c filename.txt
       OR
    grep -e "^" -c filename.txt
  • Count the number of lines with data using the grep command
    grep -n '$' filename.txt
            OR
    grep -n '^' filename.txt
  • Count the total number of lines using the sed command without printing data
     sed -n '$=' filename.txt 
  • Count the number of lines with data using the sed command
    sed -n '{=;p}' filename.txt | sed "N;s/\n/ /g"

Wrap up

That’s all for now!

You have learned five different ways to count the number of lines in Linux using utilities like wc, cat, nl, grep, and sed commands. Intentionally, I didn’t cover the awk method to count numbers. If anyone knows, then please put it in a comment section for other readers.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments