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.
|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
less --LINE-NUMBERS /etc/passwd
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:
Now you want line numbers? What would you do? Press:
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.
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:
🐧 2 comments so far... add one ↓
|Category||List of Unix and Linux commands|
|Disk space analyzers||ncdu • pydf|
|Firewall||Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04|
|Network Utilities||NetHogs • dig • host • ip • nmap|
|OpenVPN||CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04|
|Package Manager||apk • apt|
|Processes Management||bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time|
|Searching||grep • whereis • which|
|User Information||groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w|
|WireGuard VPN||Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04|