In Part I, I covered some basics that help with working on the CLI and enabling you to keep things going after you close the shell. Now let’s move on to some other useful tidbits that i’ve found immensely helpful when troubleshooting and manipulating text files (like logs) to find relevant information for whatever I was trying to solve.
Less is More
less and more are two commands to allow you to view the contents of text files. Now, I know that many people think less vs more is a personal choice. Truth is, I was a “more” guy at first, and I quickly ran into its limitations. For one, you can’t “go back up” as you’re scrolling down using more. Secondly (at least on FreeBSD when I was using more) you couldn’t search. Sure, now you can, but it won’t highlight it, and you still can’t scroll back up. I can go on. You have all your vi commands to poke around with. This alone should convince you. You can do so much more with less (pun very much intended), so just use it. Your desire to be efficient thanks you.
Cat
All I’ll say about this one, is what my mentor always said: “If you’re not using cat in combination with other commands in one line on the command line, you’re probably doing something wrong.” Seriously though, it’s kind of true. cat is not very valuable by itself, but it’s power comes from the ability to essentially allow other commands (like say grep, cut, tr, sort, wc or uniq – which I’ve covered in Part III )
Head or tail?
What if you just wanted to get the last few tidbits of lines from a log file? Or perhaps you’re only interested in the first line of a tab/comma/space delimited file because you want the first line that tells you what each column is? head shows you the top lines, tail the last. You can use -n to specify how many, usually the default is 10 lines. I find tail to be the most useful. Often, I find myself running tail -f <filename> more than anything else as I’m troubleshooting issues and trying to recreate them, seeing what shows up in logs. What the -f switch does (or -F if it’s a file that can be renamed or rotated) is output the last lines of text in a file, but then instead of closing, it keeps sitting there, and as new lines are added to the bottom of the file, they are printed on screen.
Manipulating Compressed Text Files
Here’s one that’s fairly useful. I only found out about it recently-ish because I’d always just uncompress the file and pipe it to the text manipulating command I wanted to use. If the file is gzip’d you have the choice of zless, zdiff, zcat, zgrep, zmore. If it’s a bzip file, then it’s bzless, bzcat, etc… a rather useful shortcut.
Ok, I know these subheadings are full on cheesy, but I’m actually hoping the light humour helps this stick a little. At work, I find myself often sharing tidbits of my knowledge, because I like doing it, but also because too many people simply never learned the power of the shell. On unix-alikes anyway, there’s nothing plain and boring about the shell.
1 ping
Using the Linux/Unix shell more Effectively (Part III) | obsecured.net
January 22, 2011 at 12:26 (UTC -4) Link to this comment
[...] mentioned in Part II that I was going to dive into a few more useful CLI commands. Here now are some more useful [...]