nl — Number lines
Linux / Basic Linux commands / nl.html
Prints a text file with line numbers added.
Basic Information
| Command Name | nl |
|---|---|
| One-line Introduction | Prints a text file with line numbers added. |
| Provided by | GNU coreutils |
| Official Documentation | https://man7.org/linux/man-pages/man1/nl.1.html |
| Supported Operating Systems | Linux, macOS, BSD, WSL |
| Installation Status | Installed by default |
| Category | Linux basic commands (tags: Linux basic commands (tags: nl, line number, numbering, cat)) |
What is this command?
nl prints input with line numbers. It resembles cat -n but gives finer control over blank lines and number formatting.
-b selects which lines are numbered, -w sets the field width and -s the separator. It is useful when quoting code with line references.
What problems does it solve?
- Quoting code or configuration with line numbers
- Referring to a specific log line
- Numbering only non-empty lines
- Controlling number width and separator
Key features
Numbering
Adds a sequential number before each line.
- When to use: Citing documents or code
- What you get: You can reference exact lines.
- Limitations: The original file is not modified.
Blank line control
-b a or -b t selects all or non-empty lines.
- When to use: Reviewing files with many blank lines
- What you get: Numbering stays contiguous and readable.
- Limitations: By default only non-empty lines are numbered.
Formatting
-w and -s change width and separator.
- When to use: Producing a consistent output format
- What you get: Output stays aligned.
- Limitations: The options can feel numerous at first.
Installation and usage
Check whether it is installed
nl --version
sudo apt install coreutils # Debian/Ubuntu sudo dnf install coreutils # Fedora
Real-world examples
Basic numbering
Number non-empty lines.
nl config.yml | head
Number all lines
Number blank lines too.
nl -b a notes.txt | head
Custom format
Change width and separator.
nl -w 3 -s ': ' app.py | sed -n '1,10p'