10.2 less and more: Paging Through Long Files
Right, so you’ve got a file. It’s not a little file. It’s a log file, or a massive CSV, or maybe a config file that seems to go on for miles. cat will blast it across your terminal in a blinding, useless waterfall of text. What you need is a pager—a tool that lets you actually read the thing, one screenful at a time. Enter more and less. And yes, less is more. The naming convention is a classic piece of Unix humor that is both brilliant and infuriating. more came first, and then someone built a better version and, with a straight face, called it less because, as the joke goes, “less is more.” I’ll wait while you groan.
Why more is, well, less
more is the old guard. It’s on every system, and it does one thing: it lets you page forward through a file. That’s it. You can’t go back. It’s like a book with no “previous page” button. You open it, you go forward until you hit the end, and then it spits you back out to the command line. It’s functional, but it’s a bit like a car that only has a drive gear and no reverse. For this reason, you’ll almost always want to use less. But because history is a clingy ghost, more is often the default pager for other commands (like man), which is why we’re stuck with knowing it.
To use `more, you just throw your file at it:
more /var/log/syslog
You’ll see a screen of text with a percentage at the bottom. Hit the spacebar to go forward a page, or Enter to go forward one line. When you hit the end, you’re done. That’s the whole party.
Why less is, objectively, more
less is more’s hyper-intelligent successor. It does everything more does, plus it lets you scroll backwards, search, jump to the start/end, and a whole lot more, all without loading the entire file into memory first. This last part is key—you can less a 100GB file instantly because it only looks at the part it needs to show you. It’s brilliant.
The basic incantation is the same:
less /var/log/syslog
Now you’re in the less viewer. It looks like more, but you are now holding a lightsaber instead of a club.
Navigation: Your Lightsaber’s Controls
Once inside less, you move around with single-key commands. They’re mostly mnemonic, if you squint.
- Spacebar: Forward one page.
f: Also forward one page (think ‘forward’).b: Back one page (think ‘back’).jor↓: Down one line. (Thank you,less, for bowing to modern convention).kor↑: Up one line.G: Go to the end of the file. (Shift + g).- **
1Gorg: Go to the start of the file. (The1means ‘first line’. Justgby itself also usually works). 10j: Go down 10 lines. You can prefix any movement command with a number./pattern: Search forward for “pattern”. Hitnfor next match,Nfor previous match.?pattern: Search backwards. This is incredibly useful for finding the start of an event in a log.q: Quit. This is the most important one. You’re not trapped inless; you can always leave.
The Killer Feature: Following Files in Real-Time
This is the reason I use less a dozen times a day. When you’re watching a log file that’s actively being written to—like when you’re troubleshooting a web server—you can use less to follow it, much like tail -f, but with the ability to scroll around.
You start less and then press F. Note the capital ‘F’. The command prompt will change to “Waiting for data… (interrupt to stop)”. Now, less is effectively tailing the file. New lines written to the file will appear on your screen. It’s live. It’s magical. If you want to stop following and get your cursor back to scroll through the history, you just hit Ctrl+C. You’re still in less, with all the text that was there, and you can now scroll up to see what happened before you started following. It’s infinitely better than a plain tail -f.
Best Practices and Pitfalls
- Don’t forget you’re in
less. I can’t tell you how many times I’ve absentmindedly typed a command into alesssession, only to have it just jangle uselessly. Remember,lessis a viewer. You need toqout of it to get back to your shell. lessis also your manual reader. Themancommand almost always useslessas its pager. So every time you read a man page, you’re practicing yourlessskills. Use/to search for flags you can’t remember.- Line numbers matter. Start
lesswith the-Nflag (less -N filename) to show line numbers. This is crucial for debugging or when you need to go to a specific point in a file. - It understands syntax! Sometimes. If you
lessa source code file and it looks like a boring monochrome mess, your system might not have thelesspipeextension installed or configured. If it is set up,lesscan automatically pipe files through syntax highlighters, making it a surprisingly decent code viewer. It’s worth checking your distro’s docs on how to enable this.
So, in summary: use less. Forget more exists unless you’re on some ancient, hobbled machine. less gives you total control over the file, doesn’t bog down on massive files, and even lets you ride along as data is written. It’s not just a tool; it’s a fundamental part of your shell’s interface. Master it.