Skip to main content

Understanding the `nl` Command in Linux

The nl command in Linux is a utility for numbering lines of files. It allows users to add line numbers to files in a customizable way. This is especially useful when analyzing files where keeping track of line numbers is important, such as scripts or code.


The basic syntax of the nl command is:

nl [OPTIONS]... [FILE]...

If no file is specified, or when the file is -, nl reads from standard input.


Here is a table of common options for nl:

--body-numbering-bSpecify line numbering for the body of the document.
--header-numbering-hSpecify line numbering for the header of the document.
--footer-numbering-fSpecify line numbering for the footer of the document.
--line-increment-iThe increment for line numbers (default is 1).
--number-format-nThe format for line numbers (ln, rn, rz).
--number-separator-sThe text to insert between the line number and the line.
--number-width-wThe number of columns for line numbers (default is 6).
--join-blank-lines-lNumber of consecutive blank lines to group as one.


Before we start with the examples, let’s create a sample text file using vim:

vim example.txt

Once inside vim, enter the following lines (or your content of choice):

This is a sample text file.
It contains several lines,
some of them are empty.

The goal is to demonstrate the nl command.

To save and exit vim, press Esc, type :wq, and hit Enter.

Example 1: Basic Line Numbering

nl example.txt

This command will display the contents of example.txt with line numbers added to non-empty lines.

Example 2: Number All Lines

nl -ba example.txt

The -ba option numbers all lines, including empty ones.

Example 3: Custom Line Increment

nl -i 10 example.txt

This command numbers every line but increments the line numbers by 10.

Example 4: Right-Aligned, Zero-Padded Line Numbers

nl -nrz -w3 -s". " example.txt

Here we use several options together:

  • -nrz: Right-align and zero-pad line numbers.
  • -w3: Use a field width of 3 for line numbers.
  • -s". ": Insert ". " between the line number and text.

Example 5: Skip Numbering Certain Lines

nl -bp'^$' example.txt

With -bp'^$', we're telling nl to number only lines that match the regular expression ^$, which represents empty lines. Hence, it will skip numbering non-empty lines.

Combining nl with Other Commands

The nl command can also be piped with other commands for more complex operations. For example, to number all lines and then redirect the output to another file:

nl -ba example.txt > numbered.txt

Or, you might combine it with grep to search for a pattern and number the results:

grep 'sample' example.txt | nl

The nl command is a simple yet powerful tool for line numbering. By using the options detailed above, users can customize the numbering to suit a wide range of needs, from simple line counting to complex formatting for code reviews or document editing.

What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle