Read long files with less
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.logMoving around
| Key | Does |
|---|---|
| Space or f | next screen |
| b | back a screen |
| ↓ ↑ (or j k) | one line |
| g and G | the start and the end |
| /word then Enter | search forward; n is the next match, N the one before |
| q | quit, 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 | lessManual 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.
Your task
Read logs/app.log in less with line numbers. Then page through only the lines that contain ERROR, by piping grep into less.
- Open logs/app.log in less with line numbers (not done yet)
- Pipe the ERROR lines into less (not done yet)
The checklist ticks itself off as you work in the terminal: any way that gets the result counts.
Hints come one at a time, then one way to do it. Try each before the next.
What this lesson uses, in one place.
less- View a file one screen at a time More on less
less -N- Number the lines More on less
man- Show the manual page for a command More on man
Every everyday command on one page: the Linux commands cheat sheet.