nl command in Linux with example

In this tutorial, I use nl command to number lines of files in Linux.

What does nl command mean?

nl – number lines of files

Syntax

nl [OPTION]… [FILE]…

In 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

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 the simple command in Linux. It is uses number lines of files.

,

About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created DevopsRoles.com site to share the knowledge that I have learned. 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 *

Comment moderation is enabled. Your comment may take some time to appear.

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