Skip to content

nl Linux Command Guide

The Linux nl command is used to number lines in a file. It is a simple yet powerful tool that can be used to add line numbers to a file for better organization. The nl command offers various options to customize the numbering format and location. By using the nl command, you can quickly and easily add line numbers to your files, making it easier to reference specific lines of text. This guide will walk you through the syntax of the nl command, various options available, and practical examples to help you master the use of nl in Linux.

Terminal window
nl [option] [file]
OptionDescription
-bSpecify how body lines should be numbered
-dSpecify the delimiter
-fSpecify the number of leading blank lines to be skipped
-hDisplay usage message and exit
-iSpecify line number increment
-lSpecify the number of lines to be counted before incrementing line numbers
-nSpecify number format for line numbers
-sSpecify the line number separator
-vSpecify a number to add to line numbers
-wSpecify the number of digits to use for line numbers
ParameterDescription
fileThe file to be numbered
Terminal window
nl file.txt

This command numbers all lines in the file “file.txt”.

Terminal window
nl -v 100 file.txt

Numbers all lines in the file “file.txt” starting from line number 100.

Terminal window
nl -s $'\t' file.txt

Numbers all lines in the file “file.txt” with tab (\t) as the separator.

Display Line Numbers for Non-Empty Lines Only

Section titled “Display Line Numbers for Non-Empty Lines Only”
Terminal window
nl -b t file.txt

Displays line numbers for non-empty lines in the file “file.txt”.

Numbering Lines and Indicating for Empty Lines

Section titled “Numbering Lines and Indicating for Empty Lines”
Terminal window
nl -ba file.txt

Numbers all lines in the file “file.txt” and includes line numbers for empty lines as well.

Terminal window
nl -n rz file.txt

Numbers all lines in the file “file.txt” with a special custom format for line numbers.

Terminal window
nl -h "HEADER" -body -f 1 file.txt

Inserts page headers with line numbers in the file “file.txt”.

To use the nl command in Linux, execute the following command:

Terminal window
nl --option <value>

The nl command is used to add line numbers to a file or standard input. It is helpful for referencing specific lines in a document or for generating formatted output with line numbers.

How can I number only non-empty lines with nl?

Section titled “How can I number only non-empty lines with nl?”

To number only non-empty lines in a file using nl, you can use the -ba or --body-numbering=a option. This will number only the non-empty lines of the file. Here is an example:

Terminal window
nl -ba file.txt

Can nl be used to number lines in reverse?

Section titled “Can nl be used to number lines in reverse?”

Yes, you can use the -nr or --number-format=r option with nl to number lines in reverse. This will start numbering from the last line. Here is an example:

Terminal window
nl -nr file.txt

How can I customize the format of line numbers with nl?

Section titled “How can I customize the format of line numbers with nl?”

To customize the format of line numbers with nl, you can use the -w or --number-width option followed by a number to specify the width of the line number field. Here is an example:

Terminal window
nl -w 3 file.txt

Is it possible to add a prefix to line numbers with nl?

Section titled “Is it possible to add a prefix to line numbers with nl?”

Yes, you can add a prefix to line numbers with the -p or --number-format option followed by the desired prefix. This will prepend the specified prefix to the line numbers. Here is an example:

Terminal window
nl -p '@ ' file.txt
  • Numbering lines in a file
  • Adding line numbers to a file
  • Creating or updating a table of contents
  • Numbering output of a command or script
  • Formatting text files with line numbers