How to Count Number of Lines in a File in Linux (wc and nl Command Examples)

by Himanshu Arora on February 13, 2013

Linux commands wc and nl will help you to identify the number of words, lines, bytes, etc, in a file. This tutorial explains how to use these two very useful command with various examples.

The basic text file that will be used in examples throughout this article is shown below :

$ cat sort.txt
UK
Australia
Newzealand
Brazil
America

Linux nl Command Examples

The nl utility in Linux is used to number lines of a file.

Here is the syntax and description from man page :

SYNOPSIS
nl [OPTION]… [FILE]…

DESCRIPTION
Write each FILE to standard output, with line numbers added. With no FILE, or when FILE is -, read standard input.

1. A basic example

Here is a basic example that explains how nl command can be used to number lines of a file.

$ cat sort.txt
UK
Australia
Newzealand
Brazil
America

$ nl sort.txt
     1	UK
     2	Australia
     3	Newzealand
     4	Brazil
     5	America

So we see that using nl command, all the lines of file sort.txt got numbered.

2. Increment line numbers with any value using -i option

The option -i can be used to override the default increment of 1 in line numbers.

Here is an example where we have used -i to increase the line number increment to 5 :

$ nl -i5 sort.txt
     1	UK
     6	Australia
    11	Newzealand
    16	Brazil
    21	America

Instead of the default 1,2,3… the line numbers are now displayed in increments of 5 (i.e 1,6,11…)

3. Add string after line numbers using -s option

By default, the nl command adds only line numbers. But, through -s option, any string can be added that can act as a separator between line numbers and the line text.

Here is an example:

$ nl -s. sort.txt
     1.UK
     2.Australia
     3.Newzealand
     4.Brazil
     5.America

So we see that the character ‘.’ was added after line numbers.

4. Use a different column for line numbers using -w option

Columns for line number display can be changed using -w option.

Here is an example :

$ nl -w1 sort.txt
1	UK
2	Australia
3	Newzealand
4	Brazil
5	America

$ nl -w2 sort.txt
 1	UK
 2	Australia
 3	Newzealand
 4	Brazil
 5	America

$ nl -w3 sort.txt
  1	UK
  2	Australia
  3	Newzealand
  4	Brazil
  5	America

$ nl -w4 sort.txt
   1	UK
   2	Australia
   3	Newzealand
   4	Brazil
   5	America

$ nl -w5 sort.txt
    1	UK
    2	Australia
    3	Newzealand
    4	Brazil
    5	America

$ nl -w6 sort.txt
     1	UK
     2	Australia
     3	Newzealand
     4	Brazil
     5	America

The exhaustive output above gives a good idea as to how the display column for line numbers can be changed.

5. Use STYLE for numbering lines using -b option

Various STYLEs are available for line numbering. From the man page :

STYLE is one of:

  • a – number all lines
  • t – number only nonempty lines
  • n – number no lines
  • pBRE – number only lines that contain a match for the basic regular expression, BRE

In the example below, I have used a regular expression ‘pA’ as a STYLE with option -b. This regular expression matches the lines beginning with ‘A’ and so nl command numbers only those lines.

$ nl -bpA sort.txt
       UK
     1	Australia
       Newzealand
       Brazil
     2	America

So we see that only the lines beginning with ‘A’ were numbered.

6. Use different FORMAT for inserting line numbers using -n options

There are various FORMATs available for inserting line numbers. From the man page :

FORMAT is one of:

  • ln – left justified, no leading zeros
  • rn – right justified, no leading zeros
  • rz – right justified, leading zeros

Here is an example that demonstrated all the above formats:

$ nl -nln sort.txt
1     	UK
2     	Australia
3     	Newzealand
4     	Brazil
5     	America

$ nl -nrn sort.txt
     1	UK
     2	Australia
     3	Newzealand
     4	Brazil
     5	America

$ nl -nrz sort.txt
000001	UK
000002	Australia
000003	Newzealand
000004	Brazil
000005	America

Please note that you can also use sed command to count the number of lines in a file.

Linux wc Command Examples

The wc utility in Linux is used print information like number of newlines, words, byte counts of a file.

Here is the syntax and description from man page :

SYNOPSIS
wc [OPTION]… [FILE]…
wc [OPTION]… –files0-from=F

DESCRIPTION
Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified. With no FILE, or
when FILE is -, read standard input. A word is a non-zero-length sequence of characters delimited by white space.

1. A basic example

Here is a basic example of Linux wc command :

$ cat sort.txt
UK
Australia
Newzealand
Brazil
America

$ wc sort.txt
 5  5 41 sort.txt

The three numbers produced in output correspond to number of lines, number of words and number of bytes. These three numbers are followed by name of the file.

2. Display word count through -w option

The word count of a file can be displayed explicitly through -w option.

Here is an example :

$ wc -w sort.txt
5 sort.txt

So we see that number of words were printed followed by the file name.

3. Display length of longest line through -L option

The wc command provides an option -L that can be used to display the length of longest line in the file.

Here is an example :

$ wc -L sort.txt
10 sort.txt

So we see that length of the longest line (‘Newzealand’ in our case) was displayed in the output.

4. Display number of newlines through -l option

The wc command provides an option -l through which number of newlines can be displayed in the output.

Here is an example :

$ wc -l sort.txt
5 sort.txt

So we see that there were 5 newlines in the file sort.txt

5. Display number of bytes through -c option

Total number of bytes in a file can be displayed by using -c option of the wc command.

Here is an example :

$ wc -c sort.txt
41 sort.txt

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 15 comments… read them below or add one }

1 shweta February 13, 2013 at 11:39 am

hi,
it’s really helpful…….. :)
learn somthing new as always……….

2 Jalal Hajigholamali February 13, 2013 at 1:26 pm

Hi,

Thanks a lot…….

3 Chris F.A. Johnson February 13, 2013 at 2:15 pm

To count the number of lines in a file using sed:

sed -n ‘$=’ “$file”

Using awk:

awk ‘END { print NR }’ “$file”

Using bash4:

mapfile x < "$file"
echo"${#x[@]}"
unset x

4 Amol February 13, 2013 at 11:12 pm

Thanks, Nice Post :)

5 Sunil Rawat February 14, 2013 at 12:14 am

Thanks its very helpful

Pls let me assist how to add a single user in two different group

6 rahoolm February 14, 2013 at 2:38 am

Too good. Learnt a new thing!

7 pathum March 15, 2013 at 7:56 am

Nice Post.thanks…

8 Abhinav Gaur October 19, 2013 at 2:07 am

If I just need the ‘number of lines’ as output and not the ‘name of file’ along with it, as is the case for ‘wc’ command, then what command can I use?

9 Chris F.A. Johnson October 19, 2013 at 11:48 am

wc -l < FILENAME

10 Abhinav Gaur October 19, 2013 at 11:53 am

@Chris F.A. Johnson

Thanks. It worked.

11 Amrutha Penmasta November 1, 2013 at 3:59 pm

Hi,

I need to know the number of lines in the file and insert the number as a trailer at the end of the same file. Can you please help with it?

Thanks!

12 Chris F.A. Johnson November 3, 2013 at 2:31 pm

size=$( sed -n ‘$=’ “$file” )
echo “$size” >> “$file”

13 Amrutha Penmasta November 4, 2013 at 9:56 am

Thanks Chris. Works perfectly!

14 Amrutha Penmasta November 4, 2013 at 10:11 am

Chris wondering if there is a way of formatting data as per below:

On the first line I have 5 fields separated by pipe (|). But from line 2 the data is not formatted.

Field1|Field2|Field3|Field4|Field5
Amt:1234
Direction:Pay
Account:abcd
Date:11/01/2013
|Tran references below:
46389748937
4837693700

486783749278
3682637769
| | NEW|OLD|7869689.00

Can we get the output to the below?

Field1|Field2|Field3|Field4|Field5
Amt:1234 Direction:Pay Account:abcd Date:11/01/2013|Tran references below: 46389748937 4837693700 486783749278 3682637769| | NEW|OLD|7869689.00

Can the data be formatted so they are in the same line from line2, fields just separated by pipe (|).

I am very new to sed, not sure if this can be done.

15 jayaprakash March 27, 2014 at 11:18 pm

Thank you

Leave a Comment

Previous post:

Next post: