Skip to content

Read long files with less

IntermediateLesson 17 of 665 min

Open long files and logs with less, move with Space, b and /search, number lines with -N and page through grep’s output. Practise in your browser.

cat is fine for a short file. Point it at a log with thousands of lines and the start scrolls away before you can read it. less is a pager: it opens a file one screen at a time, lets you move around and search, and changes nothing.

less logs/app.log

Moving around

KeyDoes
Space or fnext screen
bback a screen
↓ ↑ (or j k)one line
g and Gthe start and the end
/word then Entersearch forward; n is the next match, N the one before
qquit, back to the prompt

less -N numbers the lines, which helps when someone says "look at line 80".

At the end of a pipe

Any command that prints too much can end in | less, so you read its output at your own pace:

grep -n WARN logs/app.log | less

Manual pages open in less too. That's why q gets you out of man.

Tip:This practice terminal prints the whole file instead of one screen at a time, so scroll up to read it. On a real machine, the keys above work as described.

Your turn

Read logs/app.log in less with line numbers. Then page through only the lines that contain ERROR, by piping grep into less.

Next