Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Viewing and Searching Files

Linux nl command

Reference for the Linux nl command: number lines with control over which lines count, width, separator and start value, and how it differs from cat -n.

nl copies its input to standard output and puts a line number in front of each line. Unlike cat -n, it does not number blank lines by default, and it can number only the lines you choose: those matching a regular expression, or all of them.

It also understands "logical pages" with header, body and footer sections, but most people only use the numbering options.

Terminal
nl [OPTION]... [FILE]...

Try it

Bash Default numbering and styles

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Format, start value and patterns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
nl [-b STYLE] [-n FORMAT] [-w WIDTH] [-s SEP] [-v START] [-i INCR] [FILE]...
Option Description
-b STYLE Which body lines to number: a all, t non-empty (default), n none, pREGEX only lines matching the regex.
-n FORMAT ln left-justified, rn right-justified (default), rz right-justified with leading zeros.
-w NUMBER Width of the number column (default 6).
-s STRING Text placed between the number and the line (default a tab).
-v NUMBER First line number (default 1).
-i NUMBER Increment between numbers (default 1).
-p Do not restart numbering at each logical page.
-h STYLE, -f STYLE Same as -b, for page headers and footers.

Returns: 0 on success; non-zero if a file cannot be read.

Usage notes

  • Blank lines are skipped, not removed. They stay in the output with an empty number column, so the layout is preserved.
  • -b pREGEX uses a basic regular expression, written directly after p with no space. Quote it so the shell leaves it alone.
  • Default separator is a tab, so the output can be split with cut at the first field.
  • Numbering never wraps. A number wider than -w simply widens the column.
  • Logical pages: lines consisting only of \:\:\:, \:\: or \: (the delimiters) mark header, body and footer sections. Numbers restart after each page unless -p is given. Ordinary text files never contain these lines.
  • cat -n numbers every line and uses a fixed format. Use nl when you need padding, a start value or selective numbering.
  • Alternatives: awk '{ print NR ": " $0 }' gives full control, and grep -n numbers only matching lines using their original positions.
  • Reading standard input: with no file, or -, nl numbers what it receives, so ls | nl is a quick way to make a pick list.