Less command is used to display contents of any output of command or a file in a single page at a time. Using it you can navigate to backward and forward through the file or output.
Less will read file page by page instead of entire files so it will load fast compare to text editors like
less is used to view contents of large files.
Use of Less
Below is the syntax to use the
less [OPTIONS] filename
Let’s take an example, we will view the content of file
It will show output as given below:
Navigating to the File Content
Once you use the less command to view content of a large file, you will see a colon
(:) at end of terminal.
If you would like to go to next pages just press the
f key or
space bar in keyboard. If you want to go to specific line then enter the number followed by
f key. Alternatively, you can press
Down arrow key to scroll forward by a line a
Up arrow to scroll backward.
b key to go previous page. You can move up for a specific number of line by entering number followed by
b key. You also can search for a specific pattern using forward slash
(/) followed by pattern whatever you want to search. By pressing the
Enter key less will search forward for match cases. If you would like to search backwards use
(?) followed by pattern.
You will see
(END) once once the file reach to the end of file. Press
q key to quite the less.
You can pass few options along with less command. For example, if you want to see line number with less then, type as below:
less -N filename
When less exits, the content of a file will be cleared from the screen. To keep content on the screen use
less -X filename
The most useful option is
+F. Mostly, this is used to view the changes in file content, like log file.
less +F /var/log/auth.log
The less program provides number of commands to search and navigate to entire file content. You can get list of all command just by typing
Following are the most common used commands to navigate in file content while use less:
|Space bar or f||Move Forward one page.|
|Down arrow, Enter, e, or j||Move forward one line.|
|Up arrow,y or k||Move backward one line.|
|b||Move Backward one page.|
|/pattern||Search forward for matching patterns.|
|?pattern||Search backward for matching patterns.|
|N||Repeat previous search in reverse direction.|
|n||Repeat previous search.|
|g||Go to the first line in the file.|
|Ng||Go to the N-th line in the file.|
|G||Go to the last line in the file.|
|p||Go to the beginning of fthe ile.|
|Np||Go to N percent into file.|
You have explain how to use the
less command in Linux. If you have any query or suggestion, feel free to comment below.
Leave a Reply