Blog

03/06/2019

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.
[donotprint][/donotprint]

Display line numbers in less

The basic syntax is:

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

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 on tcsh/csh shell:

setenv LESS "-N"

OR if you use sh/bash/ksh, enter:

LESS="-N"; export LESS

Now, run less as follows:
$ less filename

See less(1) Linux command man page for more information.

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....
12/08/2019

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...