nl - Linux


The nl command in Linux is used to number the lines of files, providing an easy way to add line numbers to text files. This is particularly useful for processing source code or configuration files, where reference to specific lines is often necessary. Whether analyzing log files or coding scripts, nl helps in maintaining clarity and ease of navigation through the document.


The basic syntax of the nl command is:

nl [options] [file...]
  • [options]: Optional parameters to manipulate how nl functions.
  • [file…]: One or more files to process. If no file is specified, or if ‘-‘ is used as a file, nl reads from standard input.


  • -b [style]: Specifies which lines to number.
    • 'a' numbers all lines (default).
    • 't' numbers only non-empty lines.
    • 'n' does not number lines (used for headers and footers).
  • -n [format]: Specifies the line number format.
    • 'ln' left justified, no leading zeros.
    • 'rn' right justified, no leading zeros (default).
    • 'rz' right justified, with leading zeros.
  • -w [width]: Sets the line number field width to [width] digits. The default is 6.
  • -s [separator]: Defines the text that separates line numbers from the text. The default is a tab.
  • -v [start_number]: Sets the starting line number (default is 1).
  • -i [increment]: Line number increment at each line (default is 1).
  • -l [num]: Group of lines counted as a single line (default is 1).


  1. Basic Numbering: Add line numbers to all lines in a file.
    nl myfile.txt
  2. Numbering Non-empty Lines: Number only non-empty lines.
    nl -b t myfile.txt
  3. Custom Separator and Format: Use : as a separator and zero-padded numbers.
    nl -w 4 -n rz -s ": " myfile.txt
  4. Combining with Other Commands: Pipe output from another command to number each line.
    cat myfile.txt | nl

Common Issues

  • Incorrect Line Numbering: When the -b option isn’t chosen correctly, e.g., numbering empty lines that should be skipped (-b t for non-empty lines).
  • Output Formatting: The default width may not accommodate large files, causing unaligned output. Use -w to increase it.


nl can be integrated with tools like grep for enhanced text processing:

nl log.txt | grep '404' | nl

This example numbers all lines in log.txt, greps lines containing ‘404’, and numbers the filtered output again.

  • cat: Used often with nl to display file contents.
  • awk: Can also be used for numbering lines or manipulating text.
  • more or less: Utilized for paging through numbered text.

For further reading and more detailed information, refer to official GNU documentation or man pages (man nl).