Look inside a file

INPUT · Slides

Read it a page at a time

01 / 08

When neither cat nor head is enough

cat pours the lot past you. head only shows you the start.

So what about "I want to read a long file slowly, top to bottom"?

The tool for that is less. It puts out one screenful and then waits for you.

02 / 08

less — open it, and wait

Hand less a file name and one screenful comes out, and it stops there.

One important thing here. Unlike other commands, less is not finished once you have typed it. It stays open, waiting for your next key.

The file name along the bottom is the sign that says "you are inside less right now".

~ $ less long.txtline01line02(as much as fits on the screen)long.txt

03 / 08

The way out is q

Learn this one first. The way out is q.

First letter of quit. No Enter needed. One press of q and you are back at the prompt.

With other tools, the ones that are only for reading usually let you out with q as well. man does, and so does top. Reading means q is a good rule to carry around.

04 / 08

Turn the page, go back

Inside less, a handful of keys move you about.

  • space — on to the next screenful
  • b — back a screenful (back)
  • j / k — one line down / up
  • G (capital) — all the way to the end
  • g (lower case) — all the way to the start

j and k for down and up is the same as in vi, which turns up later. The keys line up between the thing you read with and the thing you write with.

05 / 08

Search with /

Press / inside less and a little input line appears at the bottom. Type what you are looking for, press Enter, and it jumps to the line that has it.

Hunting for "error" in a long log is the commonest use there is.

Once you have found one, n takes you to the next match.

/line28line28line29(and everything below it)

06 / 08

It can read a stream too

less is not limited to files — it can read whatever flows into it.

Hand it something with | (a pipe) and you can page through what another command put out. It is the standard catch for a long listing that would otherwise pour past.

Something like ls -l /bin | less lets you take your time over a big pile of file names.

~ $ seq 1 100 | less

07 / 08

It has an older brother called more

There is a command called more that is very like less. It came first, and less is the improved version.

more cannot go back (only forwards). less can. Which is where the joke in the name comes from: less can do more than more.

Both are here, so you can just use less.

08 / 08

Now have a go

From here you actually open less.

These questions have you press keys one at a time after typing. There are moments where you send a key without pressing the run button, so read the question carefully.

When in doubt, q. That always puts you back at the prompt.