less command is one of the commands which is used to display and read file contents into the terminal in Linux and Unix systems. less command makes it very easy to navigate, search in a file with a keyboard shortcut. less command first created for Unix systems where there is no GUI and every task was completed via command line. less is only used to view and navigate a file or document but can not edit and save it. vi, vim, nano, etc. are complete text editors that provide display, search, and advanced edit options. less is generally used to read text files and log files. less name comes from the lesser which means print less information from the given file to read easily.
less Command Syntax
The less command has the following simple syntax.
less OPTION FILE
- OPTION provides different parameters and options for less command.
- FILE is the file which will be read and displayed. Alternative to file another command output can be redirected too.
Even less command provides a lot of options one of the most popular and useful options are like below.
|-? or –help||Display help about less|
|-f or –force||Force non-regular files to be opened|
|-I or –ignore-case||Ignore case|
|-n or –line-numbers||Suppress, do not show line numbers|
Read File with less
The less command mainly used to read text and log files. In the following example we will read the log file named
$ less /var/log/syslog
From the screenshot, we can see that the content is displayed on the screen interactively. You can use the navigation and search commands for less. Also, the file name currently viewed is displayed in the bottom-left corner.
Quit less Interactive Mode
When using the less command it will be automatically entered the interactive mode which will hide the bash shell. In order to go back to the bash shell and close or quit the less interactive mode, the q shortcut can be used as below.
Navigate with less
After the less interactive mode is opened the following commands can be used to search, navigate in the interactive mode easily. Even bookmarks can be created and navigated later.
|Down Arrow Key , Enter , e , j||Move down one line|
|Up Arrow Key , y , k||Move up one line|
|Space Bar , f||Move down one page|
|b||Move up one page|
|Right Arrow Key||Scroll horizontally to the right|
|Left Arrow Key||Scroll horizontally to the left|
|g||Go to the first line|
|G||Go to the last line|
|10g||Go to line number 10|
|80g||Go to line number 80|
|50p , 50%||Go to 50 percentage of the document|
|90p , 90%||Go to 90 percentage of the document|
|/linux||Search the term linux to forward or downward|
|?linux||Search the term linux to backward or upward|
|n||In search go/jump to next occurrence|
|N||In search go/jump to the previous occurrence|
|m“a||Save current location as a bookmark to the letter a|
|‘“a||Return to bookmark specified with the letter a|
|h||Display help information|
|q||Quit or exit|
Follow Given File and Print Changes To The Terminal
By default, less command will read the given file content and print to the current terminal in with an interactive mode. But some files may change during the usage of the less command or we may need to follow the given file for new content. Especially log files are used to put new content in every second for a busy application. The less command can be used to follow and print the new content when new content is added into a file. We will use the
-X option in order to follow a file interactively.
$ less -X /var/log/syslog
Show Contents In The Bash Terminal
By default the less command opens the interactive terminal in order to show given file content. After exiting from the less interactive terminal the content will be cleared from screen. If you need to leave the file content on the bash shell you can use the
-X option like below.
$ less -X /var/log/syslog
Show Line Numbers
By default the less command only shown the file content without adding any extra data or information. But in some cases adding line numbers for every line for the file content can be beneficial. You can shown line numbers by using the
-N option like below.
$ less -N /var/log/syslog
Redirect Command Output to less
Up to now we have read the content from a file. The less command can be also used to read content from a command output. We will just redirect the command output by using the pipe
| operator like below.
$ cat /var/log/syslog | less