“less” command displays the text one screen-full at a time similar to the “more” command. You can use the “less” command on a file or pass the output from a command to the “more” command using pipe.
The “less” command more powerful than the “more” command. This is because, the “more” command will have to read the entire file before it can show you the initial screen text. On the other hand, the “less” command does not have to read the entire file before it can show you the initial set of lines on the terminal.
Viewing a file
Viewing a file with just a few lines will display the contents and the less command exists. Viewing a large file with “less” command would display the text one screen-full at a time.
$ less us-states.txt
|q||Quit more command.|
|spacebar||Move text one screen forward.|
|b||Move text one screen backward.|
|ENTER||Move text one line forward.|
|:||Last line at the button of the terminal is activate to enter commands.|
|:f||Displays the file name, lines displayed, total lines, bytes etc.|
|:n||Jump “n” lines forward on the screen.|
|:ng||Make nth line the top line on the screen|
|/[string]||Search for text. Replace [string] with the desired text.|
Display line numbers when viewing a file
Use option -N to display the line numbers
$ less -N us-states.txt
Force “less” to display line numbers every time
This is a simple task. Create an alias and add to your bash profile.
alias less=”less -N”
You can add the above alias line to your bash profile file to make it survive logouts and reboots.
Squeeze multiple blank lines into one (-s option)
$ less -s file.txt
Make less scroll 20 lines at a time
$ less -20 file.txt
Open file from a specific line number ( +number)
$ less +10 file.txt
Using “less” with “ps -ef”
$ ps -ef | less
Using “less” with “dmesg”
$ dmesg| less
Display help information
$ less --help
Display version information
$ less --version