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.
Similar Reads
ln command in Linux with Examples The 'ln' command in Linux is a powerful utility that allows you to create links between files. These links can either be hard links or soft (symbolic) links. If you're unfamiliar with these concepts, check out our detailed guide on Hard and Soft Links in Linux to understand their differences, use ca
3 min read
ncal Command in Linux with Examples ncal is a command-line tool to view a calendar on the terminal in Linux. It is similar to the cal command in Linux. The major difference between both the commands is the output they display. It shows the current month calendar of the current year with the date highlighted. But with command options,
2 min read
sync command in Linux with Examples sync command in Linux is used to synchronize cached writes to persistent storage. If one or more files are specified, sync only them, or their containing file systems. Syntax: sync [OPTION] [FILE]... Note: Nothing is being shown in the screenshots just because sync command makes the cache in the bac
1 min read
uname command in Linux with Examples Linux, renowned for its open-source flexibility and powerful performance, offers a range of commands that reveal the inner workings of your system. Among these, the 'uname' command stands out as a versatile tool that provides key details about your Linux machine. Here, we will learn the basics of th
4 min read
lsmod command in Linux with Examples lsmod command is used to display the status of modules in the Linux kernel. It results in a list of loaded modules. lsmod is a trivial program which nicely formats the contents of the /proc/modules, showing what kernel modules are currently loaded. Syntax: lsmod Example: Run lsmod at the command lin
1 min read