Less Is Awesome
Gosh, that naming gets confusing really fast! I’m talking about the Unix
Less is more, but better.
morebeing 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!
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
ctrl+c won’t work. You’re welcome.
You can use the arrow keys to scroll around, or
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.
grep for lines in a file to get a view of what you care about,
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
b keys to jump between matches now.
To search backwards, do the same with
? instead of
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
Open the file you want to watch as usually, and when in
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
Now you can browse around the file, and search it! Or continue tailing it with
F. You don’t get this flexibility with
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
$ 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
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.