less Command Display Line Numbers

I‘m a new Linux user. How can I display line numbers using the less command on Linux or Unix-like system using bash/ksh shell?

You can easily display line numbers using less command. All you have to do is pass either -N or --LINE-NUMBERS option to the less command. This option forces less to show a line number at the beginning of each line in the screen.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux or Unix operating system
Est. reading time 2 minutes

How to display line numbers in less

The basic syntax is as follow for both GNU/Linux and BSD version of less found on FreeBSD/macOS and other operating systems:
less -N filename
## OR ##
less --LINE-NUMBERS filename

In this example, show line numbers for /etc/passwd file using GNU/less, type:

less -N /etc/passwd

OR

less --LINE-NUMBERS /etc/passwd

Sample outputs:

Fig. 01: less displaying the line numbers

Fig. 01: less displaying the line numbers

Suppresses line numbers

The default (to use line numbers) may cause less to run more slowly in some cases, especially with a very large input file. Suppressing line numbers with the -n option will avoid this problem. In other words we can hide displaying line numbers:
less -n /path/to/file

How to toggle line number without quitting less

Say you started less as follows:
less /etc/hosts
Now you want line numbers? What would you do? Press:
-N
Finally hit the ENTER key.

How do I force less to display line number by default?

The less command can read various options from the environment variable “LESS”. For example, to avoid typing less -N filename each time less is invoked, you might type following setenv command on tcsh/csh shell

setenv LESS "-N"

OR if you use sh/bash/ksh, use the export command

LESS="-N"; export LESS

Now, run less as follows:
$ less filename
See “Linux Set Environment Variable Command Tutorial” for details.

Conclusion

GNU and BSD less is a program similar to more command, but it has many more options. Many less commands are based on both more and vi text editor. See less(1) Linux command man page for more information:
man less


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 2 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
2 comments… add one
  • Saurabh Lambe Mar 17, 2021 @ 13:54

    Thanks for the article! How to set line number from inside the less prompt? I mean what command to run when I’m already inside less.

    • 🐧 Vivek Gite Mar 18, 2021 @ 8:28

      We can toggle line numbers without quitting less by typing -N followed by the enter key.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum