The less command in Linux: scroll, search, jump to the end

By

Published · Updated

Tagged: CLI

Learn how to use the less command in Linux: scroll a file, jump to the end with G, search with / and n, filter lines with &, show line numbers, and press F to follow new lines like tail -f.

The less command is one I use a lot. It shows you the content stored inside a file, in a nice and interactive UI.

Usage: less <filename>.

Terminal window showing the less command displaying a markdown file with frontmatter and content about bash shell scripting

Once you are inside a less session, you can quit by pressing q.

less doesn’t load the whole file before showing it, so it opens huge log files right away, where an editor would struggle.

Moving around

You can navigate the file contents using the up and down keys, or using the space bar and b to navigate page by page. You can also jump to the end of the file pressing G and jump back to the start pressing g.

A few more keys:

KeysWhat it does
j / kOne line down / up
d / uHalf a page down / up
42gGo to line 42
50pGo to 50% of the file
=Show the file name, the line number and how far into the file you are
hHelp with all the commands

Typing a number before a command repeats it or gives it a target, like 42g.

Searching

You can search contents inside the file by pressing / and typing a word to search. This searches forward. You can search backwards using the ? symbol and typing a word.

After a search, press n to go to the next match and N to go to the previous one. All the matches are highlighted. Press ESC and then u to turn the highlighting off.

The search pattern is a regular expression, so /^ERROR finds lines that start with ERROR.

Searches are case sensitive. Type -i while you’re inside less to make them ignore case, unless the pattern has an uppercase letter. Type -i again to turn it off.

Show only the lines that match

This one is great for logs. Type & followed by a pattern, and less hides every line that doesn’t match:

&ERROR

An & at the start of the prompt reminds you that some lines are hidden. Type & and press enter with no pattern to see all lines again.

Follow a file as it grows

Pressing the F key enters follow mode, or watch mode. When the file is changed by someone else, like from another program, you get to see the changes live. By default this is not happening, and you only see the file version at the time you opened it. You need to press ctrl-C to quit this mode. In this case the behaviour is similar to running the tail -f <filename> command.

The difference with tail -f is that after ctrl-C you’re back in less, and you can scroll up and search what you just saw. Press F again to go back to following.

To open a file directly in follow mode, start less with +F:

less +F /var/log/system.log

+ runs a command when the file opens. less +G file starts at the end, and less +/error file starts at the first match for error.

Useful options

less -N file.txt   # show line numbers
less -S file.txt   # don't wrap long lines, scroll right with the arrow keys
less -R file.txt   # show colors instead of escape codes like ESC[31m
less -F file.txt   # quit right away if the file fits on one screen

-S helps a lot with CSV files and wide log lines.

You can toggle any of these while less is open by typing them, like -N to turn line numbers on and off.

To use some options every time, put them in the LESS environment variable in your ~/.zshrc or ~/.bashrc:

export LESS="-R -i"

Git already uses less as its pager. When LESS isn’t set, Git sets it to FRX for you. If you set your own LESS, Git leaves it alone, so add F, R and X to yours if you want to keep that behavior in git log and git diff.

Use less with other commands

With no file name, less reads from a pipe. Use it to scroll the output of any command:

ps aux | less

If the command prints colors, add -R so you see the colors instead of strange characters.

Edit the file

This command just visualises the file’s content. You can directly open an editor by pressing v. It uses the editor set in the VISUAL environment variable, or EDITOR if that’s not set, and falls back to vi.

Multiple files

You can open multiple files, and navigate through them using :n (to go to the next file) and :p (to go to the previous).

less access.log error.log

This command works on Linux, macOS, WSL, and anywhere you have a UNIX environment

Want me to talk about your product? You can sponsor this site.

Related posts about cli: