Using less to view text files at the Linux command line

Using less to view text files at the Linux command line

The less utility makes quick work of viewing text files in the terminal.

Concept
Image credits : 

PublicDomainPictures, via Pixabay. CC0.

Get the newsletter

Join the 85,000 open source advocates who receive our giveaway alerts and article roundups.

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 less.

Getting started

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.

Moving around

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).

Finding text

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.

About the author

That idiot Scott Nesbitt ...
Scott Nesbitt - I'm a long-time user of free/open source software, and write various things for both fun and profit. I don't take myself too seriously and I do all of my own stunts. You can find me at these fine establishments on the web: Twitter, Mastodon, GitHub, and... more about Scott Nesbitt