Using less

Command line If there’s one thing that you’re going to find on a Linux system, it’s text files. Lots 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. And that’s using a utility called less – which I discussed briefly in a previous post.

Standard kit with all Linux distributions (at least, the ones I’ve used), less is a command line text file viewer that has 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 that you want to view.

Starting less

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 that 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 long. With less, you can move forward in the file in a few ways:

  • Move down a page by pressing the space bar or the PgDn key
  • Move down one line at a time by pressing the down arrow key

less also allows you to move backwards 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 then type what you want to find.

Starting less

Remember 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 that you’re searching for. That’s a nice touch, making it easier for you to scan the text.

Highlighted 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, 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 get used to it, you’ll wonder how you ever did without it.

Photo credit: By ZxxZxxZ (Own work) [GPL], via Wikimedia Commons

This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

flattr this!