Less Is Awesome

Gosh, that naming gets confusing really fast! I’m talking about the Unix CLI tool less.

Less is more, but better.

more being another CLI tool, which was there before less.

As it happens, less is one of those tools I use a lot and couldn’t imagine being without. I use it a lot, and it’s so present that I sometimes don’t really notice just how much I use it for.

In this article, I’d like to share a few tricks I learned over the years. All of them together have helped me feel more capable when working in the terminal. I hope they will help you as well!

Start Boring

You can use less to just view a file on disk. It looks like this:

$ less file_with_words.txt

It’s a convenient way to open and search files, especially if you don’t want to edit them (or to risk editing them). I try to use less instead of vim when I just want to browse around and a cat command wouldn’t be convenient anymore.

The Basics First

You can exit less by typing q. No, ctrl+c won’t work. You’re welcome.

You can use the arrow keys to scroll around, or f and b as replacement for page up and down (or, you know, use those instead).

Or space and shift-space if you don’t have page up and down keys. Cries in mac.

To get to the beginning of the file press g. To go to the end, press G. If you’re familiar with vim, you’ll feel right at home.

Searching

You can grep for lines in a file to get a view of what you care about, but less has the benefit of being interactive!

When in the tool, press / to start searching. You can type a string you’re interested in. Now, press Enter to search through the output.

All hits are highlighted, and you can use the n or b keys to jump between matches now.

To search backwards, do the same with ? instead of /.

But less is not limited to plain-old boring files.

Viewing Changing Files

You might know about tail -f to see lines which are appended to a file in real time. But do you know that you can do the same with less?

Open the file you want to watch as usually, and when in less, press F.

Congrats, now you’re seeing new lines being added. However! You can stop being overwhelmed with a flood of new lines at any time, by pressing ctrl+c.

Now you can browse around the file, and search it! Or continue tailing it with F. You don’t get this flexibility with tail -f.

Pipe Goes In

Ever had a command, which outputs way too much information? You can use less to navigate the output and search in it. Just pipe your long command into less and do your thing!

# docker inspect is VERY verbose
$ docker inspect my_container | less

Oh No! Long Lines!

One last thing. Do you have that one command, which outputs way more columns than fit on your screen? I love working at my 12 inch display, but sometimes tools like the Docker CLI or kubectl have very wide output.

You can pipe them into less and disable line wrapping to scroll around without figuring out which entries belong to which original line. Just add a -S flag!

$ kubectl get pods -A | less -S

The man page (which is excellent by the way, and very long. If you’re curious, check out man less) describes this as:

Causes lines longer than the screen width to be chopped (truncated) rather than wrapped.

You can use the left and right arrow keys to scroll around horizontally, but still do all the other cool less things.

That’s It For Now

Less is really useful! I hope this article has helped you to discover it for your own workflows.

There are a few more CLI tools and tricks I’d like to share (watch is so cool), but that’s a topic for another writeup.

Subscribe to my newsletter!
You'll get notified via e-mail when new articles are published. I mostly write about Docker, Kubernetes, automation and building stuff on the web. Sometimes other topics sneak in as well.

Your e-mail address will be used to send out summary emails about new articles, at most weekly. You can unsubscribe from the newsletter at any time.

Für den Versand unserer Newsletter nutzen wir rapidmail. Mit Ihrer Anmeldung stimmen Sie zu, dass die eingegebenen Daten an rapidmail übermittelt werden. Beachten Sie bitte auch die AGB und Datenschutzbestimmungen .

vsupalov.com

© 2024 vsupalov.com. All rights reserved.