GICT knowledge base
How to Use the nl Command to Number File Lines in Linux
Published
When working with text files in Linux, you may need to add line numbers for easier reference or debugging. The nl command is specifically designed for this purpose. Unlike similar commands such as cat -n, nl provides flexible options for numbering lines, controlling formatting, and choosing which lines to number. This guide will help you understand and use the nl command to number file lines in Linux, whether for code review, configuration files, or general text processing.
Prerequisites
- Linux system with access to the command line
- Basic understanding of terminal commands
- A sample text file to practice with (create one using
nanoorechoif needed)
What is the nl Command?
The nl (number lines) command in Linux reads a file and displays its contents with line numbers added. You can use it to identify and reference specific lines in logs, source code, or any text file. The command supports options for skipping blank lines, choosing numbering styles, and formatting the output.
Basic Usage of nl
To display a file with line numbers, run:
nl filename.txt
This outputs the contents of filename.txt with non-empty lines numbered by default.
Number All Lines Including Blank Lines
To number every line, even if it is blank, use:
nl -b a filename.txt
The -b a option tells nl to number all lines (“a” stands for “all”).
Number Only Non-Empty Lines (Default)
To explicitly number only non-empty (text) lines, use:
nl -b t filename.txt
This is the default behavior. The -b t option means “number text lines only.” Blank lines are not numbered.
Start Line Numbering from a Specific Number
To start numbering from a different line number, such as 101:
nl -v 101 filename.txt
The -v option sets the starting line number.
Customize Number Format and Width
To change how the line numbers appear, use -n and -w options:
nl -n rz -w 4 filename.txt
-n rz:Right-aligned, zero-padded numbers-w 4:Line numbers are four characters wide (e.g., 0001, 0002)
Number Output from Standard Input
You can use nl with piped input:
cat filename.txt | nl
Or enter text interactively (press Ctrl+D to end):
nl
Type your text here. Press Ctrl+D when done.
Advanced Section Numbering
nl can treat input as header, body, and footer sections (separated by lines containing only \: by default). You can control numbering for each section using -b, -h, and -f:
nl -b t -h n -f n filename.txt
This example numbers only the body section, skipping numbering headers and footers.
Change the Number Separator
By default, nl separates line numbers with a tab. To change this, use -s:
nl -s ". " filename.txt
This separates the line number and text with a dot and a space.
Practical Examples
- Number all lines in a file and save the result:
nl -b a data.txt > data_numbered.txt - Number the output of a command, such as grep:
grep "pattern" logfile.txt | nl - Display code with three-digit, left-aligned line numbers:
nl -n ln -w 3 script.py
Additional nl Options
-d CC:Set custom delimiters for header/body/footer separation (default is \:).-l NUMBER:Number of adjacent blank lines to consider as one (useful for formatting).
Conclusion
The nl command is a flexible tool for numbering file lines in Linux. Its various options allow you to tailor the output for code review, documentation, text processing, or data analysis tasks. Try out different combinations to see what best suits your workflow, and make referencing file content much easier and more organized.
Related guides
How to Set Up zram on a Small VPS
Configure zram on Ubuntu with kernel and swap checks. Understand compressed memory limits, monitor real use and avoid unsafe swap-file changes.
LinuxHow to Find Services That Need a Restart After Updates with needrestart
Find services using outdated libraries with needrestart on Ubuntu. Read restart reports, review automatic restart policy and plan maintenance safely.
LinuxHow to Limit a Linux Service’s Memory with systemd
Limit a Linux service with systemd MemoryHigh, MemoryMax and swap controls. Check cgroup support, diagnose pressure and remove only your own policy.
LinuxHow to Protect SSH with CrowdSec on Ubuntu
Protect SSH with CrowdSec on Ubuntu. Connect log detection to firewall enforcement and test temporary blocking without losing administrator access.
