Skip to content

nl Command Cheat Sheet

The nl command numbers lines in a file. It is more powerful than cat -n.


Synopsis

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

Basic Usage

Number Non-Blank Lines

By default, nl only numbers lines that have text.

nl file.txt

Number All Lines (-ba)

Including blank lines (like cat -n).

nl -ba file.txt

Formatting Numbers

Width (-w)

Set width of line numbers (default 6).

nl -w 3 file.txt

Leading Zeros (-n rz)

Format as: rz (right justified, zero filled).

nl -n rz -w 3 file.txt
# Output:
# 001 line one
# 002 line two

Left Justified (-n ln)

nl -n ln file.txt

Separator (-s)

Change the separator between number and text (default is Tab).

nl -s ". " file.txt
# Output:
# 1. line one
# 2. line two

Advanced: Logical Pages

nl treats files as logical pages with Header, Body, and Footer sections. Delimiters: - \:\:\: : Header start - \:\: : Body start - \: : Footer start

You can configure nl to restart numbering at each page.

nl -p file_with_sections.txt
(No reset used by default).


Notes

  • Useful for generating code listings for documentation.
  • Increment: Use -i 5 to increment numbers by 5 (e.g., 1, 6, 11...).