nl Command
Beginner Text Processing man(1)Number lines of files
👁 10 views
📅 Updated: Mar 15, 2026
SYNTAX
nl [OPTION]... [FILE]...
What Does nl Do?
The nl command numbers lines of files, providing more formatting control than cat -n. It supports different numbering styles, formats, and can selectively number only non-empty lines or lines matching specific patterns.
nl divides input into logical sections (header, body, footer) and can apply different numbering rules to each section. This makes it useful for formatting source code listings, reports, and documents.
While cat -n provides basic line numbering, nl offers customizable number format, width, separator string, and selective numbering — making it the better choice for producing formatted, professional output.
nl divides input into logical sections (header, body, footer) and can apply different numbering rules to each section. This makes it useful for formatting source code listings, reports, and documents.
While cat -n provides basic line numbering, nl offers customizable number format, width, separator string, and selective numbering — making it the better choice for producing formatted, professional output.
Options & Flags
| Option | Description | Example |
|---|---|---|
| -b a | Number all lines including blank lines | nl -ba file.txt |
| -b t | Number only non-empty lines (default) | nl file.txt |
| -b p | Number only lines matching a regex pattern | nl -bp'^function' code.php |
| -n ln | Left-justified line numbers | nl -nln file.txt |
| -n rz | Right-justified with leading zeros | nl -nrz file.txt |
| -w | Set width of line number field | nl -w3 file.txt |
| -s | Set separator string after number | nl -s': ' file.txt |
Practical Examples
#1 Number all lines
Numbers every line including blank lines.
$ nl -ba script.sh
Output:
1 #!/bin/bash
2
3 echo "Hello"
#2 Number with leading zeros
Right-justified numbers with leading zeros, 4 digits wide.
$ nl -nrz -w4 data.txt
Output:
0001 First line
0002 Second line
#3 Number only non-empty lines
Numbers only lines with content, leaving blank lines unnumbered (default).
$ nl config.conf#4 Custom separator
Uses ") " as separator for numbered list formatting.
$ nl -s') ' -w2 todo.txt
Output:
1) Buy groceries
2) Clean house
#5 Number matching lines only
Numbers only lines that start with "def " (function definitions).
$ nl -bp'^def ' module.py#6 Number lines for code review
Numbers all lines with 5-digit zero-padded numbers — useful for referencing specific lines.
$ nl -ba -nrz -w5 app.pyTips & Best Practices
Use nl for numbered lists: nl -s'. ' -w2 creates numbered lists like 1. First item. Use in scripts to format output.
nl vs cat -n: cat -n is simpler but numbers all lines with fixed format. nl offers customizable numbering style, width, separator, and selective numbering.
Default skips blank lines: By default, nl does not number blank lines. Use -ba if you need all lines numbered (like cat -n behavior).
Frequently Asked Questions
How do I number all lines including blank ones?
Use nl -ba file.txt. The -ba flag means "body: number all lines". Without it, blank lines are skipped.
How do I add leading zeros to line numbers?
Use nl -nrz file.txt. The rz format means right-justified with leading zeros. Add -w N to set the number width.
What is the difference between nl and cat -n?
nl offers customizable numbering format, width, separators, and can selectively number lines matching patterns. cat -n is simpler but less flexible.
Related Commands
More Text Processing Commands
Master Linux with Professional eBooks
Curated IT eBooks covering Linux, DevOps, Cloud, and more
Browse Books →