The nl command in Linux is an abbreviation for number lines. If you’ve ever had a large text document and needed to add line numbers to it, the nl command is your saving grace. Rather than going through the painstaking process of numbering each line manually, or importing your text document into a GUI text editor, you can simply use nl from the command line and be done in a matter of seconds.
Just like pretty much any other command on a Linux system, nl is very versatile and supports a wide array of various formatting options. So, it’s easy to get your documents numbered with lines exactly the way you want. You’ll just need to know a few options that you can use with the command.
In this tutorial, you’ll learn how to use the nl command in Linux through examples. Follow along below to learn about the various options that you can use with this command.
In this tutorial you will learn:
- How to use the nl command on Linux
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
|Conventions||# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Frequently Used Options
The nl command will add sequential numbers to all or some of the lines in a file. Sound simple? Well, it is once you know the syntax. In the examples below, we will go over some of the most frequently used options with the command. This will show you how to adapt the command options depending on the formatting of your file.
As a simple example, we’ve created a text document that contains ten different names of common Linux distributions (one per line). With the nl command, we will show you how to add line numbers to this text document.
nl command in Linux Basic and Advanced Examples
- To add line numbers to a file, simply pass the name of the file to the nl command. By default, line numbers will be added to every line in the file. See the output below as an example.
$ nl distros.txt 1 AlmaLinux 2 Arch Linux 3 CentOS 4 Debian 5 Fedora 6 Gentoo 7 Manjaro 8 openSUSE 9 Red Hat 10 Ubuntu
This is pretty handy already, but notice that by default, the output adds a tab character and then a line number, and then finally the text itself. This is a great way to quickly see what line a certain string of data appears on. But it may not be so useful in some other situations. You’ll see below how to change the formatting of this output.
- Let’s try changing the formatting a little. One common way to format line numbers is to put a period after the number. To do so with the nl command, we’ll use the
-soption. We will also specify
". ", to indicate that we want the line number to be proceeded by a dot and a space.
$ nl -s ". " distros.txt 1. AlmaLinux 2. Arch Linux 3. CentOS 4. Debian 5. Fedora 6. Gentoo 7. Manjaro 8. openSUSE 9. Red Hat 10. Ubuntu
- What if we want to manipulate the space that precedes the line number? This can be done with the
-w(width) option. To get rid of the space entirely, we’ll specify a width of 1. Note that we are still using the
-soption from the previous example, which will combine both formatting changes.
$ nl -w 1 -s ". " distros.txt 1. AlmaLinux 2. Arch Linux 3. CentOS 4. Debian 5. Fedora 6. Gentoo 7. Manjaro 8. openSUSE 9. Red Hat 10. Ubuntu
- By default, the nl command won’t number blank lines. In other words, only lines with text on them will be numbered. If you need to number blank lines as well, that can be done with the
$ nl -b a distros.txt 1 AlmaLinux 2 Arch Linux 3 CentOS 4 Debian 5 Fedora 6 7 8 Gentoo 9 Manjaro 10 openSUSE 11 Red Hat 12 Ubuntu
- If you would like to start numbering at some number other than 1, you can use the
-voption with nl and specify some other number. As an example, we will start numbering the lines at 100.
$ nl -v 100 distros.txt 100 AlmaLinux 101 Arch Linux 102 CentOS 103 Debian 104 Fedora 105 Gentoo 106 Manjaro 107 openSUSE 108 Red Hat 109 Ubuntu
- To include leading zeros for each line, you can use the
-n rzoption with nl.
$ nl -n rz distros.txt 000001 AlmaLinux 000002 Arch Linux 000003 CentOS 000004 Debian 000005 Fedora 000006 Gentoo 000007 Manjaro 000008 openSUSE 000009 Red Hat 000010 Ubuntu
There are, of course, many more options and styles that you can use with the nl command. It would be very redundant to cover them all here, since there are so many slight variations. We recommend that you use some of the options covered in this guide and add your own tweaks to the command in order to get the formatting exactly as you want with your line numbers.
You can always use the man command to read more about the nl command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.
In this tutorial, we learned all about the nl command on Linux. The nl command is useful when you need to add line numbers to a file. Regardless of how your file is formatted or what kind of requirements you have with your line numbering, we’ve given you all the options you will need with the nl command in Linux.