Everything You Need to Know About the Less CommandPosted about 2 years ago 863
How To Use The Less Command
If there are more lines in the file than space on the screen then a single colon (:) will appear at the bottom and you will have a number of options to move forward through the file.
The less command can also be used with output piped through another command.
ps -ef | less
The above command will show a list of running processes one page at a time.
You can press either the space bar or the "f" key to scroll forward.
Changing The Number Of Lines That Are Scrolled Through
By default, the less command will scroll a single page at a time.
You can change the number of lines that are scrolled when you press the space and "f" key by pressing the number immediately before pressing the key.
For example, enter "10" followed by either the space or "f" key will cause the screen to scroll by 10 lines.
To make this the default you can enter the number followed by the "z" key.
For example, enter "10" and then press "z". Now when you press the space or "f" key the screen will always scroll by 10 lines.
A rather bizarre inclusion is the ability to press the escape key immediately prior to the space bar. The effect of this is to continue scrolling even when you have reached the end of the output.
To scroll one line at a time press either the "return" key, "e" or "j". You can change the default so that it scrolls a specified number of lines by entering a number before the specified keys. For example, enter "5" followed by the "e" key will make the screen scroll 5 lines every time "return", "e" or "j" are pressed. If you accidentally press an uppercase "J" the same result will occur except that if you hit the bottom of the output it will continue scrolling.
The "d" key allows you to scroll down a specified number of lines. Again by entering a number before "d" will change the default behavior so that it scrolls the number of lines you specify.
To scroll back up the list you can use the "b" key. Unlike the more command, this can work with both files and piped output. Entering a number before pressing the "b" key scrolls back up the specified number of lines. To make the "b" key permanently scroll by the specified number of lines enter the number you wish to use followed by the "w" key.
The "y" and "k" keys work similarly to the "b" and "w" keys except the default isn't to scroll one window at a time but one line at a time back up the screen. If you accidentally press uppercase "K" or uppercase "Y" the result will be the same unless you hit the top of the output in which case the scrolling will continue beyond the beginning of the file.
The "u" key also scrolls back up the screen but the default is half the screen.
You can also scroll horizontally using the left and right arrow keys.
The right arrow scrolls half a screen to the right and the left arrow scrolls half a screen to the left. You can continue scrolling right over and over but you can only scroll left until you hit the beginning of the output.
Redisplay The Output
If you are viewing a log file or any other file that is constantly changing you might want to refresh the data.
You can use a lowercase "r" to repaint the screen or an uppercase "R" to repaint the screen discarding any output that has been buffered.
You can press an uppercase "F" to scroll forward. The benefit of using the "F" is that when the end of the file is reached it will keep trying. If a log is updating whilst you are using the less command any new entries will be displayed.
Move To A Specific Position In A File
If you want to go back to the beginning of the output press lowercase "g" and to go to the end press uppercase "G".
To go to a specific line enter a number before pressing the "g" or "G" keys.
You can move to a position which is a certain percentage through a file. Enter a number followed by the "p" or "%" key. You can even enter decimal points because let's face it, we all need to go to position "36.6%" through a file.
Marking Positions In A File
You can set a marker in a file using the "m" key followed by any other lowercase letter. You can then return to the marker by using the single quote "'" key followed by the same lowercase letter.
This means you can specify a number of different markers through the output which you can return to easily.
Searching For A Pattern
You can search for text within the output using the forward slash key followed by the text you wish to search or a regular expression.
For example /"hello world" will find "hello world".
If you want to search back up the file you have to replace the forward slash with a question mark.
For example ?"hello world" will find "hello world" previously output to the screen.
Load A New File Into The Output
If you have finished looking at a file you can load a new file into the less command by pressing the colon key (:) followed by the "e" or "E" key and the path to a file.
For example ":e myfile.txt".
How To Exit Less
To exit the less command press either the "q" or "Q" keys.
Useful Command Line Switches
The following runtime switches may or may not be useful to you:
- less -bN - The N stands for a number and loads the specified number of kilobytes into memory. By default, the value is 64 kilobytes but you can specify any number you wish. If you enter -1 then the entire file will be loaded into memory which may or may not be a good idea depending on the size of the file.
- less -B - By default, the less command allocates the required memory buffers by default when using piped output. You can use the -B switch to prevent autobuffering.
- less -c or less -C - By default the screen repaints by scrolling up the screen. To clear the screen from the top down use the -c or -C switches.
- less -e - Causes less to exit when it hits the end of the file for the second time
- less -E - Causes less to exit when it hits the end of the file for the first time
- less -f - Open special files such as directories using less
- less -F - Causes less to exit if a file is less than one screens worth of data
- less - g - Only highlight the last item found when searching
- less -G - Suppress highlighting altogether when searching
- less -hN - Specify the maximum number of lines the less command can scroll back
- less -i - Ignore case when searching unless uppercase characters are found in the search pattern
- less -I - Ignore case when searching
- less -jN - The N stands for a number. This determines where on the screen a line is placed when searched for. For example, searching for "hello world" will place the line found with "hello world" in it on line 1 if less -j1 is used.
- less -J - This displays a little asterisk in the left column (status column) which shows when a piece of text which you have searched for has been found.
- less -m - Displays the number of bytes through a file instead of a colon at the bottom of the screen
- less -M - Displays the line numbers of the output. For example "lines 1-23"
- less -n - Suppress line numbers
- less -N - Display line numbers on each line
- less -o - This is used with piped output only. It outputs each page of the piped output to the file one page at a time. If the file exists it will ask whether you want to overwrite it.
- less -O - This is the same as -o except that it won't ask for confirmation before overwriting a file.
- less -p - This starts less at the first occurrence of the pattern specified.
- less -P "text" - This replaces the message at the bottom of the screen to the text specified
- less -q - This prevents the bell from buzzing when you reach the end of the file. Other reasons for the bell to ring such an invalid key press remain.
- less - Q - Suppresses all noises
- less -s - This condenses blank lines. For example, if a file has 4 consecutive blank lines and you use the less -s command only 1 blank line will be displayed.
- less -S - This causes long lines to be truncated rather than wrap them onto the next line
There is much more to the less command than you would expect. You can read the full documentation by typing "man less" into a terminal window or by reading this manual page for less.
An alternative to less and more is the tail command which shows the last few lines of a file.