nl command in Linux with Examples
Last Updated :
11 Oct, 2024
Linux offers a wide range of commands for text formatting and editing. While editing a text file, you might want to display the lines with line numbers appended before them, and here comes the role-play of the nl command in Linux. nl command is a Unix/Linux utility that is used for numbering lines, accepting input either from a file or from STDIN. It copies each specified file to STDOUT, with line numbers appended before the lines.Â
Syntax
nl [OPTION]... [FILE]...
where,
- OPTION: Various options for customizing the line numbering format.
- FILE: The file(s) to be processed. If no file is specified, nl reads from the standard input.
Key Options
| Option |
Description |
-b NUMBER or -bNUMBER |
Used for numbering body lines |
-i NUMBER or -iNUMBER |
Line number increment at each line |
-n FORMAT or -nNUMBER |
Insert line numbers according to the specified FORMAT (e.g., ln, rn, rz) |
-v NUMBER or -vNUMBER |
Change the first line number of the given input |
-l NUMBER or -lNUMBER |
Count a group of NUMBER consecutive empty lines as one |
-s STRING or -sSTRING |
Add any STRING after every logical line number |
-w NUMBER or -wNUMBER |
Use a different number of columns for line numbers |
Examples:
Note: Consider the following file as input in all the examples.

1. To display a file with line numbers:
The simplest use of the nl command is to number all non-empty lines in a file.
$ nl geekfile.txt

2. To number all lines (including empty lines also):
To include empty lines in the numbering, use the -b a option:
$ nl -b a geekfile.txt

3. Count multiple, consecutive, non-empty lines as one:
-l option is used to count all non-empty lines as a single logical line to nl command. Linux considers NUMBER consecutive empty lines as a single logical line for numbering, and only numbers the last one. If any empty consecutive lines less than NUMBER occur, it will discard them.
$ nl -l 1 geekfile.txt
## is same as
## $ nl geekfile.txt
The example below considers 3 consecutive empty lines as a single logical line.Â
$ nl -b a -l 3 geekfile.txt
Here, -b a option is used to consider all logical lines (whether empty or non-empty) as input to nl command.

4. Override default increment:
The default increment pattern in Linux is 1. This can be changed using the -i option. The first line number is 1 and cannot be changed using -i.
$ nl -i 3 geekfile.txt
or
$ nl -i3 geekfile.txt

5. To make the starting line number different:
The default line number is 1. This can be changed using the -v option.
nl -v 4 geekfile.txt
or
nl -v4 geekfile.txt

6. Add a string literal after line numbers:
Any STRING literal can be added after a line number using the -s option.
$ nl -s "..." geekfile.txt

7. Change column for line numbers:
Different columns can be used to display the file output using the -w option. The default column number is 1.
$ nl -w2 geekfile.txt
$ nl -w4 geekfile.txt
$ nl -w6 geekfile.txt
or
$ nl -w 2 geekfile.txt
$ nl -w 4 geekfile.txt
$ nl -w 6 geekfile.txt
Notice the change in column number (increase in indentation from left) in the example illustrated below.

8. To number all logical lines that match the specified REGEX:
-b pREGEXP option can be used to append line numbers before those lines only that match the given pattern. The following command will number those lines that begin with F.
$ nl -b pF geekfile.txt

9. To print the lines using a different number format:
The numbering formats can be specified using the -n option. The default numbering format is rn. The available options are:
- ln: left-justified, no leading zeros
- rn: right-justified, no leading zeros
- rz: right-justified with leading zeros
$ nl -n ln geekfile.txt
$ nl -n rn geekfile.txt
$ nl -n rz geekfile.txt

Conclusion
The nl command is a powerful and versatile tool in the Linux command-line arsenal. If you’re a developer, system administrator, or someone working with text files, mastering the nl command will make text processing and line numbering more efficient. With various options for customizing the output, such as setting the starting line number, changing increments, and applying regular expressions, nl can be tailored to fit a wide range of use cases.
nl command in Linux – FAQs
What does the nl command do?
The nl command numbers lines in a text file, displaying the content with line numbers appended before each line.
How do I include empty lines when numbering with nl?
Use the -b a option to number all lines, including empty ones.
Can I change the starting line number in nl?
Yes, the -v option allows you to specify a different starting number.
How can I number only lines that match a specific pattern?
Use the -b pREGEXP option to number only those lines that match the given regular expression.
How can I adjust the column width for line numbers?
The -w option allows you to set the width of the line numbers.