Learn › Linux Foundations › Reading Files
less - a hands-on Linux lab on a real virtual machine.
Page through a file too long to dump with less, the interactive pager. Open it with less, add -N for line numbers or +/pattern to land on a word, then drive it with Space, b, /pattern, g, Shift+G, and q to quit. All on the intercept fixture, right in the practice terminal.
You already know cat, the command that prints a whole file and drops you back at the prompt. On a short file that is perfect. But inside your intercept folder is a file called bulk.log, and it is one single line of ten thousand characters. Run cat logs/bulk.log on that and the whole wall of text blasts past in one burst, the top scrolled away before you can read a word, and it is now stuck in your scrollback.
What you actually want is to hold the file still and move through it at your own pace: down a screen, back a screen, jump around, search for a word. That is the job of a pager: a program that shows a file one screenful at a time and waits for you between moves. The standard pager on Linux is less, and this whole lesson is about it.
The black boxes in this lesson are a practice terminal: a safe sandbox that only checks the one command each step teaches, so you cannot break anything. less is interactive: on a real machine it takes over the entire screen and waits for your keypresses, so a sandbox cannot replay what it shows. Here the practice terminal checks the command that *opens* less, and the moves you make once inside are taught as keys. You drive less for real on the machine at the end of the module.
You open a file in the pager by typing less and the file name. cat would dump the text and hand you back the prompt. less does the opposite: it clears the screen, shows you the top of the file, and stays open, holding the file in front of you until you decide to leave.
Open the giant log now. Type this exact command and press Enter:
less logs/bulk.log
prompt: student@linuxcamp:~/intercept$ answer: less logs/bulk.log output: hint: Type less, a space, then the path to the file: less logs/bulk.log
Nothing prints back to the practice terminal here, and that is correct: on a real machine less does not print to your command line at all, it takes over the whole screen. The single most important thing to carry away from this whole lesson: press q to quit less and return to your prompt. If a pager ever swallows your screen and you feel trapped, q is always the way out.
On the real machine, that command replaced your screen with the start of bulk.log and parked there, waiting. It did not scroll away and it did not touch your command history. less is now holding the file open, and every move from here is a single keypress. The rest of this lesson is those keys, one idea at a time, starting with how to move down and back up.
Once less is holding a file open, you move a whole screen at a time with two keys. Space moves you forward one screenful, deeper into the file. b (for *back*) moves you one screenful back toward the top. Tap Space a few times to travel down, tap b to back up, and the file scrolls under a window that you control.
For finer movement, the arrow keys (or Enter) nudge you one line at a time instead of a whole screen. Space and b are the ones you will lean on, because a screen at a time is how you actually skim a long file.
less does not load the whole file into memory before showing it. It reads only as much as it needs to fill your screen, which is why it opens a gigantic file instantly while cat would still be pouring the thing out. That is what makes less safe to point at a log of any size.
So far you have three moves and an escape: Space forward a screen, b back a screen, arrows or Enter for one line, and q to quit. That alone is enough to read any file end to end at your own pace. Next comes the move that makes less more than a slow cat: searching.
Reading screen by screen is fine, but the real power of less is jumping straight to a word. While less is open, press / (the forward slash), type the text you are hunting for, and press Enter. less leaps to the first line that contains it and highlights every match on screen. This is a search you run *inside* the pager, not a separate command.
After that first jump, n takes you to the next match further down the file, and Shift+N goes back to the previous one. So the rhythm is: /word Enter to find it, then n, n, n to walk through every other place it appears.
Searching is why engineers open a log in less instead of dumping it. On a file with thousands of lines, /error drops you on the first error instantly, and n walks you through the rest. You never scroll looking for it by eye.
Four moves now cover almost everything: Space and b to page, /pattern Enter to search, n and Shift+N to step between matches, and q to quit. Two more single keys round out navigation: g jumps to the very top of the file, and Shift+G jumps to the very bottom. On a log, Shift+G lands you on the newest line at the end in one keypress.
less also takes flags on the way in, before it opens. The handiest is -N, which turns on a line number down the left edge of every line. When you are reading a config file or a report and need to say "the problem is on line 47," -N puts that number right in front of you.
Open the report with line numbers switched on. There is a smaller file next to bulk.log called report.txt, a 12-line intercept report:
less -N logs/report.txt
prompt: student@linuxcamp:~/intercept$ answer: less -N logs/report.txt output: hint: Put -N between less and the file name: less -N logs/report.txt
On the real machine that opened report.txt in the pager with a number running down the left of every line, so line 9 is labeled 9, not left for you to count. Everything you already learned still works inside it: Space and b to page, / to search, q to quit. A flag like -N just changes how less looks when it opens; it does not change how you drive it once you are in.
You can also tell less where to start the moment it opens, so you skip the manual scrolling. Put +/pattern in front of the file name and less opens already jumped to the first line that contains that word, exactly as if you had opened it and pressed /pattern Enter yourself.
The report has a section that starts with the word SUMMARY. Open the report and land on it in one command:
less +/SUMMARY logs/report.txt
prompt: student@linuxcamp:~/intercept$ answer: less +/SUMMARY logs/report.txt output: hint: Put +/ and the word right before the file name: less +/SUMMARY logs/report.txt
On the real machine less opened report.txt already sitting on the SECTION 2: SUMMARY line, with the word highlighted, no scrolling required. A cousin of this is +G, which opens the file jumped straight to the bottom. So less +G logs/bulk.log drops you at the end of the log the instant it opens, handy when the newest lines are what you want. These + starters just press a key for you at open time; once you are in, it is the same pager you already know.
Scaffolding off. No command is shown this time. You have every piece you need.
You want to page through the giant logs/bulk.log in the pager, and you want a line number shown down the left edge so you can point at an exact spot. Recall the pager command, and recall the flag that switches line numbers on.
prompt: student@linuxcamp:~/intercept$ answer: less -N logs/bulk.log output: hint: The pager is less, the line-number flag is -N, and the file is logs/bulk.log. Put the flag between them.
That opens bulk.log in less with line numbers on, all from memory: the pager, the -N flag, the file. Inside it you already know the moves. Space and b to travel, / to search, g and Shift+G for the ends, and q to get out. That recall, pick the tool, add the flag, aim it, is exactly the move the real machine will ask of you.
You earned this cheat sheet. The top rows are how you open less; the rest are the keys you press once you are inside it:
The one idea to carry away: less holds a file still so you can move through it, instead of dumping it past you the way cat does. Reach for it whenever a file is too long to read in one screen, and remember that q is always the way out.
You are already a less user without knowing it. When you read a manual page with man <command>, that page opens inside less, so every key here works there too: Space to page, / to search, q to close the manual. The pager is one of the most reused tools on the system.
less sits at the end of a family you meet across this module. cat dumps a whole short file, head and tail show just the ends, and an older, simpler pager called more pages forward only. less was written as the more capable successor to more. That is where the classic line comes from: "less is more." Each of those tools has its own lesson; this one was only about less.
The practice terminal has shown you how to open less and every key you press once you are inside it. The Reading Files module ends with one real Linux machine, and that is where you page through a file for real.
In the Reading Files capstone mission, a VM boots just for you on this same intercept filesystem, bulk.log and all. Its objectives use exactly what you just ran: open a file with less, add -N for line numbers, +/pattern to land on a word. Then you drive it with Space, b, /, g, Shift+G, and q. One difference from here: the mission shows no commands. You read the objective, recall the pager, and type it. That recall is what makes it stick.
Finish the other Reading Files lessons, then go work the pager for real.
Practice less in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.