Today,in this post we will learn, How to show line numbers with less command in Linux.In our previous post we have described about how to show line number with cat command.
Less command is similar to more
command but it has more features.As the name suggest, it read less output rather than reading the entire input file before starting.
Sometimes while reading output with less
command,we also want to see the line numbers.It is helpful when you have line numbers with less command output.
To show line numbers with less command,use the option -N
or --LINE-NUMBER
Syntax:
less -N file-name OR less --LINE-NUMBER file-name
How to set permanently the output with line number in less command
Most of the Linux Distro has bash/sh as a default shell.Hence to set the environment in bash/sh shell in linux.
Set environment for particular user
Edit the .bashrc file in user’s home directory for particular user. And add the line
LESS="-N"; export LESS
Replace username with existing user account in Linux system
vi /home/username/.bashrc LESS="-N"; export LESS
To make environment effective after editing .bashrc file without restart. Run the command
source /home/username/.bashrc
Note: For particular user, you can also edit .bash_profile instead of .bashrc
Now run the command
less file-name
Set environment for ALL users
You should first login as root in system
sudo su - or su -
Edit the file /etc/profile .And add the line LESS="-N"; export LESS
vi /etc/profile LESS="-N"; export LESS
Activate the newly updated environment.
source /etc/profile
Now run the command
less file-name
For more understanding, read the man page of less by using command man less
(Read this troubleshooting,in case man command not found)