Quick take: The nl command numbers the lines of a file with fine control over format. By default it numbers only non-blank lines; nl -ba numbers every line, and options let you set the width, separator, and starting number.

Introduction

The nl command (number lines) adds line numbers to text, with far more control than cat -n. You can choose whether to number blank lines, set the number format and separator, and control the starting value — useful for producing numbered listings and reports.

Syntax

The basic syntax of the nl command is:

nl [OPTIONS] [FILE]

Common Options and Parameters

The most useful options and parameters for the nl command:

OptionDescription
-b aNumber all lines (default is non-blank only, -b t).
-b tNumber only non-empty lines.
-n FORMATNumber format: ln, rn, or rz (zero-padded).
-w NSet the number column width.
-s SEPSet the separator between number and text.
-v NStart numbering at N.
-i NIncrement by N each line.

Practical Examples

Real nl commands you can run today:

# Number non-blank lines (default)
nl file.txt
# Number every line including blanks
nl -ba file.txt
# Zero-padded numbers
nl -ba -nrz file.txt
# Use a custom separator
nl -ba -s ': ' file.txt
# Start at 100
nl -ba -v 100 file.txt

Tips and Best Practices

  • By default nl skips blank lines; add -ba to number every line like cat -n does.
  • Use -nrz for zero-padded numbers (001, 002) when you need aligned, fixed-width output.
  • For a quick numbered view with no formatting needs, cat -n is simpler; reach for nl when you need control.

Final Thoughts

nl numbers lines with precision — choosing which lines to count, the number format, the separator, and the starting value. It is the configurable alternative to cat -n, ideal for numbered reports and listings. For a no-frills numbered view, cat -n still wins on brevity.

FAQ: nl Command in Linux

How do I number the lines of a file?+

Run nl file. By default it numbers only non-blank lines. Use nl -ba file to number every line including blank ones, like cat -n.

What is the difference between nl and cat -n?+

cat -n numbers all lines simply. nl offers control: whether to count blank lines, the number format and width, the separator, and the starting value — useful for formatted reports.

How do I number every line including blanks?+

Use -ba (number body lines, all): nl -ba file numbers blank lines too, matching cat -n behaviour.

How do I zero-pad line numbers?+

Use the rz format: nl -ba -nrz file produces right-aligned, zero-padded numbers like 0001, which keeps columns aligned.

How do I start numbering from a specific value?+

Use -v: nl -ba -v 100 file starts numbering at 100. Combine with -i to change the increment between lines.

Need help with Linux servers or infrastructure?

Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.

Hire Me for Support