Count number of lines
How To

Count Lines in a File in Ubuntu Command Line

Sagar Sharma
Sagar Sharma

Table of Contents

While dealing with large files, you may find yourself in a situation where you want to count the lines of a file.

And in this tutorial, I will walk you through multiple methods of how you can count the lines of a file in the Ubuntu command line.

How to count lines of a file

While using Linux, there are endless possibilities of how you want to accomplish the task, but in this tutorial, I will walk you through the following methods:

  • Using the wc command (easy and recommended)
  • Using the sed command
  • Using the awk command

And to make things relevant throughout this guide, I will be using a text file named LU.txt that contains the following:

List of the best Ubuntu-based distros:
Ubuntu flavors 
Linux Mint
Pop!_OS
KDE Neon
elementaryOS
Zorin OS
Linux Lite
Voyager
Feren OS
LXLE
UbuntuDDE Remix

12 lines in total.

WC reads for word count which simply means the whole purpose of this command is to get you the word count of the file.

And to count the lines of a file using the wc command, you have to use the -l flag:

wc -l filename

In my case, the file is LU.txt:

wc -l LU.txt
Count lines of a file using the wc command

As you can see, it gave me the correct result by showing the number of lines to 12!

But what if you want to ignore the filename and want to print the lines of the file only? It is pretty simple.

You can use the cat command and pipe the wc command to it:

cat Filename.txt | wc -l
Find number of lines of a file using the cat command

Pretty neat. Right?

2. Count lines of a file using the sed command

Being a non-interactive text editor, sed has a reputation for being complex for normal users but if you want to count lines, execution is going to be super simple.

To count lines of a file using the sed command, you have to use -n ‘$=’ flags as shown:

sed -n '$=' Filename

Here, the -n and = are used to indicate each line with a number means if there are 12 lines, then, it will print from 1 to 12 which is not the ideal behavior we want.

And there comes the use of $ which will only print the total number of lines.

In my case, it gave me the following output:

sed -n '$=' LU.txt
Count the lines of a file using the sed command

3. Count lines of a file using the awk command

Don't freak out! This time, you don't have to fear the awk command.

To count the lines of a file, awk has a special built-in NR variable which can be used in the following manner:

awk 'END { print NR }' Filename
Count lines of a file using the awk command

As you can see, it gave me the correct output.

Here's how to count files in the directory

While dealing with large directories, counting the number of files may come in handy.

And for that purpose, we made a detailed guide on how to count files in the directory:

How to Count Files in Directory in Linux [5 Examples]
Here are several ways to count the number of files in a directory in Linux command line.

I hope you will find this helpful.



Sagar Sharma

Sagar Sharma

A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.