Codementor Events

Less cat tail, more less! (Why I stopped using the UNIX cat)

Published Feb 02, 2018Last updated Aug 01, 2018

I've been using cat for ages to display contents of files. I used it very rarely to actually concatenate files together.

And I've been using tail -f  to follow logs, since I started doing web development. I always had the problem that I wanted to scroll back but, as new items were added, it would automatically scroll me back to the end of the file.

At one point I stumbled upon a good alternative, namely using  less +F. I loved that you can switch between examine ( ^C ) and follow mode (⇧ F ).

I was still using cat  until the final blow   came and revealed that cat interprets escaped sequences. If you've been doing any kind of programming, you know how that can turn bad.

head, tail & more  also interpret escaped sequences. Good news is that you can ditch head and use less in combination with it's LINES option.

Here's a cool use case for less:  let's say you have a directory of text files and you quickly want to skim through them.

env LINES=10 less file.txt
ls | xargs less --prompt=%x --squeeze-blank-lines

You can navigate with :n to the next file and use --prompt  to see the name of the next file which less will open. Prompts are cool, you can use them to display line number, percentage of how much you read from the file, percentage of how many of the files you went through.

man  also uses less  so learning it's tricks is a great way to speed up navigating the UNIX documentation.

When using less  to read documentation to can jump to a certain chapter by using the --pattern option.

less --help | less --pattern=MOVING

The above will display the less  docs starting with the section on "MOVING."

And finally, as I found out from Gary Bernhardt, using less is also great for doing pretty pagination.

echo longfile.txt | less -FXRS

Let's break this apart: -F tells less not to page if it doesn't have more than one page.  -X doesn't clear the screen after exiting. -R is for passing color codes through. And finally -S which chops long lines.

I'm really curios of other uses of less so please share them with me in the comments below!

Discover and read more posts from Mircea Mare
get started
post commentsBe the first to share your opinion
McClure McClure
5 months ago

Concerns about the impact of air fresheners on feline health have led pet owners to question the safety of certain products, including Febreze plug-ins. For those seeking information on this issue, a valuable resource is https://kittenwiki.com/are-febreze-plug-ins-safe-for-cats/. Exploring the potential risks associated with these popular air fresheners is essential for creating a safe and comfortable environment for our feline companions. Understanding the nuances of pet-friendly choices contributes to responsible pet ownership and promotes the well-being of our cherished cats.

Show more replies