nl command in Linux with example

Introduction

In this tutorial, we will explore how to use the nl command in Linux to number lines of files. The nl command is a powerful tool for adding line numbers to the contents of a file or standard input. This can be incredibly useful for referencing specific lines more easily or for organizing and presenting content more clearly. Let’s delve into practical examples to demonstrate how the nl command can be effectively utilized in various scenarios.

What does the nl command mean?

The nl command stands for “number lines,” and it is used in Linux to add line numbers to the contents of files or standard input. This functionality is particularly useful for referencing specific lines more easily in scripts or documents.

nl command syntax

nl [OPTION]… [FILE]…

Some common options for the “nl” command include:

  • -b <type>: Specifies the numbering style. The <type> can be a (all lines), t (non-empty lines), or n (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

nl command in Linux with an 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!

, ,

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.