Everything You Need to Know About the Less Command

Move about files in the shell with this useful command

The less command is a more powerful version of the more command that displays information to the terminal one page at a time. Many of the switches are the same as the ones you use with the more command, but lots of extra ones are available as well.

How to Use the 'less' Command

View any text file using the less command by typing the following into a terminal window:

less filename

If the file has more lines than space on the screen, a single colon (:) will appear at the bottom, and you'll have a number of options to move forward through the file.

The less command works with output piped through another command. For example:

ps -ef | less

The above command will show a list of running processes, one page at a time. Press either the space bar or the F key to scroll forward.

Changing the Number of Scrolled Lines

By default, the less command scrolls a single page at a time. Change the number of lines that scroll 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 to advance the screen by 10 lines.

less command

To set this value as default, enter the number followed by the Z key.

To scroll one line at a time, press either the Enter, e or j keys. Change the default so that it scrolls a specified number of lines by entering a number before the specified keys. For example, enter 5 before the e key to make the screen scroll 5 lines every time.

To scroll back up the list, use the b key. Unlike the more command, scrolling works with both files and piped output.

You can also scroll horizontally using the left and right arrow keys.

Redisplay the Output

Use a lowercase r to repaint the screen or an uppercase R to repaint the screen, discarding any output that you've buffered.

Move to a Specific Position in a File

To return to the beginning of the output, press lowercase g. To go to the end, press uppercase G.

To go to a specific line, enter a number before pressing the g or G keys.

Searching for a Pattern

Locate 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.

To search up the file, replace the forward slash with a question mark. For example, ?"hello world" will find hello world previously output to the screen.

How to Exit Less

To exit the less command, press q.

Less supports quite a few switches. Run less --help or man less for additional information.