Labels

tools (7) programming (6) rails (5) cluj (4) productivity (4) misc (3) startups (3) emberjs (2) internet (2) software (2) hack (1) meetup (1) os x (1) science (1)

Tuesday, November 10, 2015

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

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.
env LINES=10 less file.txt
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.
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 long_file.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!

No comments:

Post a Comment