Number Lines in a File with the Linux nl Command
Learn how to use the Linux nl command to number non-blank or every line, customize increments and separators, match lines with regular expressions, and format output.
The Linux nl command reads text and writes it to standard output with line numbers. It is useful for inspecting files, comparing text, and sending numbered content through a shell pipeline.
nl normally displays a numbered copy of the input; it does not modify the original file in place.
Basic nl Syntax
nl [options] [file]
For example:
nl notes.txt
When a file is not supplied, nl reads from standard input:
printf 'first\nsecond\n' | nl
The numbered text is printed to standard output, so it can be viewed directly, piped to another command, or redirected to a different file.
Default Line-Numbering Behavior
With no options, nl numbers ordinary non-blank body lines. A blank line is a line with no visible text. Blank lines are normally left without numbers and do not advance the displayed number sequence.
Suppose notes.txt contains:
alpha
beta
gamma
Run:
nl notes.txt
The result is similar to:
1 alpha
2 beta
3 gamma
The first numbered body line starts at 1, and each subsequent numbered body line normally increases by 1.
Number Every Line with -b a
The -b option selects the body-line numbering style. The style a means “all,” so every physical input line receives a number, including blank lines.
nl -b a notes.txt
Using the same file, the output is similar to:
1 alpha
2
3 beta
4 gamma
This differs from the default because the blank line receives number 2, and the following lines continue with 3 and 4.
Change the Numbering Increment
An increment is the amount added after each numbered line. Use -i NUMBER to change it.
nl -i 5 notes.txt
Numbered lines use a sequence such as 1, 6, 11, and 16. Other examples include:
nl -i 2 notes.txt
nl -i 10 notes.txt
The increment affects the generated numbers, not the physical spacing or content of the source lines.
Change the Number Separator
A separator is the string inserted between the formatted number and the original line content. Set it with -s STRING.
nl -s ': ' notes.txt
Output resembles:
1: alpha
2: beta
3: gamma
Other separators are possible:
nl -s '. ' notes.txt
nl -s $'\t' notes.txt
nl -s ' -- ' notes.txt
The shell syntax $'\t' produces a tab in shells that support ANSI-C quoting, such as Bash.
Number Only Lines Matching a Regular Expression
A regular expression is a pattern used to select text. The form -b pREGEXP tells nl to number only body lines matching the expression.
To number only lines whose first character is an uppercase T:
nl -b 'p^T' notes.txt
The ^ anchor means “the beginning of the line.” Therefore, ^T matches a line starting with T. Nonmatching lines remain visible but have no generated number.
Quote the pattern when it contains spaces, wildcard-like characters, parentheses, brackets, or other shell-special characters. Quoting prevents the shell from changing the expression before nl receives it.
nl -b 'p^Task: ' notes.txt
nl -b 'p^[A-Z][a-z]' notes.txt
If a pattern does not select the expected lines, first check whether it matches the actual text. Differences in capitalization, leading spaces, and punctuation commonly cause a mismatch.
Format the Line Numbers
The -n FORMAT option controls line-number alignment and zero padding. A line-number format changes the presentation of the number, not the source text.
nl -n ln notes.txt
nl -n rn notes.txt
nl -n rz notes.txt
ln places numbers at the left side of their number field without leading zeros. rn aligns numbers on the right without leading zeros and is commonly used by the default presentation. rz aligns numbers on the right and pads them with zeros.
The active number width determines how many positions are available. With rz, the padding makes numbers line up consistently for machine-oriented or fixed-width output.
Common nl Options
Use nl in Pipelines
nl accepts both named files and standard input. This makes it useful with commands that generate text.
printf 'first\n\nthird\n' | nl -b a
Expected output is similar to:
1 first
2
3 third
You can combine numbering with another command through a pipe:
printf 'alpha\nbeta\n' | nl -s ': ' | less
When you need a saved numbered copy, redirect output to a different file:
nl -b a notes.txt > numbered-notes.txt
This leaves notes.txt unchanged and writes the numbered output to numbered-notes.txt.
Troubleshooting nl
Blank lines do not have numbers
This is the normal default behavior: nl numbers non-blank body lines only. Use:
nl -b a FILE
A pattern does not number the expected lines
The regular expression may not match the actual line content, or the shell may have interpreted part of the pattern. Check capitalization, leading spaces, and anchors. Quote the pattern:
nl -b 'p^T' FILE
Numbers have unexpected spacing or no leading zeros
The -n setting controls alignment and padding. Select the required style:
nl -n ln FILE
nl -n rn FILE
nl -n rz FILE
The original file becomes empty
This usually happens when output was redirected to the input file. Write to a distinct file instead:
nl FILE > numbered-FILE
The command is not found
Check whether the executable is available and whether your PATH is configured correctly:
command -v nl
If no path is printed, install or restore the operating system's standard core command-line utilities package.
Exam-Relevant Notes
nl FILEnumbers non-blank body lines by default and normally starts at 1.-b anumbers all lines, including blank lines.-i NUMBERchanges the amount added between successive numbered lines.-s STRINGchanges the text between the number and the source content.-b pREGEXPnumbers only body lines matching a regular expression.-n ln,-n rn, and-n rzselect left alignment, right alignment, and right alignment with zero padding respectively.nlwrites to standard output and does not edit the input file in place.- Redirect numbered output to a different file to avoid truncating the input.
Related Linux Skills
For more command-line context, see Linux command-line topics, Bash, and finding the full path of shell commands.