Reading Long Text Files With less: Search, Jump, and Follow
Why less is the pager to learn
Run less filename, search with /, jump with g and G, and press q to leave. That covers most of what you need to read a long file in the terminal.
A pager shows a file one screen at a time. According to Wikipedia's entry on less), less does not need to read the entire file before it starts, so a huge log opens right away. Mark Nudelman wrote it in 1983 to 1985 because he wanted a version of more that could scroll backward, and the name is a joke on "backwards more". The official less home page describes it as a free, open-source pager found on most versions of Linux, Unix, and Mac OS, so it is already on your Mac.
Opening a file and getting out
Open a file by giving less its name:
less server.logTo leave, press q. The less manual page lists several ways out: q, Q, :q, :Q, and ZZ all exit. Remember q and you're set. If you have ever been stuck in another terminal tool, the few keys for a quick config edit in vim or nano covers that problem for editors.
Because less is a viewer, you cannot change the file by accident. Pressing keys won't edit anything.
Moving around and jumping
Space moves forward a screen and b moves back one. Arrow keys scroll a line at a time. That's enough for a short file. A 50,000-line log needs jumps.
The manual says commands can take a number first, which it calls N. Type the number, then the key:
| Keys | What happens | | --- | --- | | g | Go to line N, default the start of the file | | G | Go to line N, default the end of the file | | 50G | Go to line 50 | | 50% | Go to a position 50 percent into the file |
So G takes you to the last line, where a log's newest entries usually sit, and g takes you back to the top.
Marks
When you find a spot you want to return to, mark it. Press m followed by a letter to mark the first displayed line with that letter. Later, press ' (an apostrophe) followed by the same letter to return to it.
ma mark this spot as "a"
'a jump back to "a"Searching
Search is the reason to use a pager. Type / and a pattern to search forward, or ? and a pattern to search backward:
/timeout
?timeoutThe manual describes / as searching forward for the line containing the pattern, and ? as searching backward. After the first match, press n to repeat the search in the same direction. Press N to repeat it in the reverse direction.
Searching is case-sensitive by default. If you would rather ignore case, start less with the -i option:
less -i server.logWikipedia lists -i as the case-insensitive search option. Matches use regular expressions, so a pattern such as error|warn can be useful, but regex flavors differ between tools, so test a pattern before you trust it.
Filtering lines with &
Searching jumps between matches. Filtering hides everything else. Type & followed by a pattern, and the manual says less will "display only lines which match the pattern; lines which do not match the pattern are hidden".
&ERRORNow the screen shows only lines containing ERROR. To bring everything back, press & and then Enter with no pattern. This works well for long output such as a diff or a log; reading a unified diff pairs nicely with it when you only care about changed lines.
Following a growing file
Some files grow while you read them, such as a log from a running service. Press F and less scrolls forward and, as the manual puts it, keeps trying to read when the end of file is reached. It is similar to tail -f.
F follow the file as it grows
Ctrl+C stop waiting and go back to normal browsingWhen you want to stop following, press the interrupt character, usually Ctrl+C. You are back in normal less and can scroll up or search through what arrived. Watching a log, then searching its history without leaving the program, is what makes F handy.
Useful options and multiple files
A few options change how the file looks. Per Wikipedia's feature list:
-Nshows line numbers, which helps when you want to jump with50Gor tell someone which line to check.-Schops long lines instead of wrapping them, so wide log lines stay on one row.-iignores case when searching.
You can combine them: less -N -S -i server.log.
You can also open more than one file at once: less a.log b.log. The manual says :n examines the next file in the list and :p examines the previous one.
Getting help inside less
If you forget a key, press h. The manual says it displays a summary of the commands and adds, "If you forget all the other commands, remember this one." If the keys look unfamiliar in the manual itself, how to read a man page SYNOPSIS explains the notation.
Doing this with Carets
less is the right tool when a file lives on a server or in a terminal session. For notes, code snippets, and config files you keep and edit on your own devices, a text editor fits better. Carets is a native notes and code editor for iPhone, iPad, and Mac. You can paste a trimmed less filter result, such as the ERROR lines you found, into a note with syntax highlighting, and keep it in a project or under a tag next to the commands you used. Carets is not a terminal, so you will still run less elsewhere.
Get Carets on the App Store for iPhone, iPad, and Mac.
Frequently Asked Questions
What is the difference between less and more?
less lets you move both forward and backward through a file. Wikipedia says its commands are based on those used by both more and vi, and that it was written because its author wanted a more that could scroll backward.
How do I search in less?
Type / and a pattern to search forward, or ? and a pattern to search backward. Press n to repeat the search and N to repeat it in the reverse direction.
How do I follow a growing file in less?
Press F. It keeps trying to read when the end of the file is reached, like tail -f. Press Ctrl+C to stop waiting.
How do I quit less?
Press q. Q, :q, :Q, and ZZ also work.