If there’s one thing you’re sure to find on a Linux system, it’s text files. A lot of them. Readme files, configuration files, documents, and more.
Most of the time, you probably open text files using a text editor. But there is a faster and, I think, better way of reading text files. That’s using a utility called less. Standard kit with all Linux distributions (at least the ones I’ve used),
less is a command-line textfile viewer with some useful features.
Don’t let the fact that it’s a command-line tool scare you.
less is very easy to use and has a very shallow learning curve.
Let’s take a look at some of the things that you can do with
Crack open a terminal window and navigate to a directory containing one or more text files that you want to view. Then run the command
less filename, where
filename is the name of the file you want to view.
The file takes over your terminal window, and you’ll notice a colon (:) at the bottom of the window. The colon is where you can type any of the internal commands you use with
less. More on these in a moment.
Chances are that the text file you’re perusing is more than a couple of lines long; it’s probably a page or more. With
less, you can move forward in the file in a few ways:
- Move down a page by pressing the spacebar or the PgDn key
- Move down one line at a time by pressing the Down arrow key
less also allows you to move backward in a file. To do that, press the PgUp key (to move up a page at a time) or the Up arrow key (to move up one line at a time).
If you have a large text file or are trying to find a specific piece of text, you can do that easily in
less. To find a word or phrase, press / on your keyboard and type what you want to find.
Note that the search function in
less is case-sensitive. Typing “the silence” isn’t the same as typing “The Silence.”
less also highlights the words or phrases you search for. That’s a nice touch that makes it easier for you to scan the text.
You can press n on your keyboard to find the next instance of the word or phrase. Press p on your keyboard to find the previous instance.
Getting out of there
Once you get to the end of a text file and you’re done viewing it, how do you exit
less? That’s easy. Just press q on your keyboard. (You can also press q at any time to leave the program.)
As I mentioned at the beginning of this post,
less is easy to use. Once you use it, you’ll wonder how you ever did without it.