In this tutorial, I use the nl command to number lines of files in Linux. Now, let’s go nl command in Linux for example.
The “nl” command in Linux is used to add line numbers to the contents of a file or standard input. It can be helpful when you need to reference specific lines or when you want to organize and present the content with line numbers.
What does the nl command mean?
nl – number lines of files
Syntax
nl [OPTION]… [FILE]…
Some common options for the “nl” command include:
-b <type>
: Specifies the numbering style. The<type>
can bea
(all lines),t
(non-empty lines), orn
(no lines).-i <increment>
: Sets the line number increment. The<increment>
can be any positive integer.-v <number>
: Sets the starting line number. The<number>
can be any positive integer.-w <width>
: Specifies the field width for line numbers.
On the man page, the describes it
- nl – number lines of files.
- man nl – More details information about nl command.

nl command in Linux with example
I have created a file nl_command.txt as below
[vagrant@DevopsRoles ~]$ cat nl_command.txt HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:199x. Devops Roles. Devops Roles. Devops Roles. Devops Roles. Hello world :)
By default, the nl command doesn’t number empty lines
[vagrant@DevopsRoles ~]$ nl nl_command.txt
1 HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:199x.
2 Devops Roles.
3 Devops Roles.
4 Devops Roles.
5 Devops Roles.
6 Hello world :)
How to number empty lines.
[vagrant@DevopsRoles ~]$ nl -b a nl_command.txt 1 HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:199x. 2 Devops Roles. 3 Devops Roles. 4 Devops Roles. 5 Devops Roles. 6 7 Hello world :)
How to numbering formats
[vagrant@DevopsRoles ~]$ nl -n ln nl_command.txt 1 HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:199x. 2 Devops Roles. 3 Devops Roles. 4 Devops Roles. 5 Devops Roles. 6 Hello world :) [vagrant@DevopsRoles ~]$ nl -n rz nl_command.txt 000001 HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:199x. 000002 Devops Roles. 000003 Devops Roles. 000004 Devops Roles. 000005 Devops Roles. 000006 Hello world :)
Customized numbering separator
[vagrant@DevopsRoles ~]$ nl nl_command.txt 1 HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:199x. 2 Devops Roles. 3 Devops Roles. 4 Devops Roles. 5 Devops Roles.6 Hello world :)
[vagrant@DevopsRoles ~]$ nl -s : nl_command.txt 1:HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:199x. 2:Devops Roles. 3:Devops Roles. 4:Devops Roles. 5:Devops Roles.6:Hello world :)
Conclusion
nl command is a simple command in Linux. It uses the number of lines of files. You can refer to the manual page for the “nl” command by typing man nl
in the terminal for more information and additional options available on your specific Linux distribution.
Thank you for reading the DevopsRoles page!