Wednesday, February 18, 2009

Simple Unix And Linux Shell Tricks To Save You A Few Gray Hairs

What it is?

Tomorrow, we're going to complete the the experiment we started in Monday's post on absorption of knowledge in the computer age, so, for today, we're just going to focus on a few little tricks that can save you grief, heartache, strife, worry and all those bad feelings people have to take prescription medication to deal with nowadays ;) Not to belittle chronic anxiety/depression (both symptoms treated with the same drugs) but, as fun as they may be, pharmaceuticals rarely actually solve a "mood disorder." And we use that term lightly. When we were kids, we were either happy, sad, pissed off or excited; any number of emotions that required no medication to correct. None of the kids in the neighborhood had ED, ADD, ADHD, LD's, or OCD's - They were all fussy, uninterested, spastic, stupid and had OCD's ;) Nowadays, too many kids have disorders and the meaning of the word has been devalued. We, here, all have CD's. We listen to them when we want to hear music. We will, of course, be asking our respective physicians about ways in which chemically altering ourselves can help us lose our CD's, and (hopefully) not feel compelled to buy new ones.. ;)

Sorry - no offense. We realize it's too late, but, if you have ADD, ADHD or any disorder of that nature, you'll get over this soon enough. Now, what the Hell was this post about, again? ;)

Oh yeah. A few little shell tricks to make your life easier so you can quit popping pills and jump-start the U.S. economy by drinking more :)

1. How to save yourself from having to retype a huge line that you thought you wanted to type, but, about at the end, you realized you couldn't enter until you typed a line preceding it, and you couldn't even tag that line to the beginning of the line you were already typing so you end up hitting ctl-C and typing the whole thing over again :

host # for x in a b c d e f g h i j k l m n o p q r s t u v w x y z ;do ps -ef|grep "[h]orse"|awk '{print $2}'|xargs ptree;don ^C

Rather than stand for that, just do the following thing whenever you log into your shell. Always make sure that you have line editing enabled. In bash, ksh, etc, if you want to enable vi line editing, all you need to do is type:

host # set -o vi

on the command line or, better yet, at it to your .bash_profile, .profile or .bashrc, so line editing will get set every time you log in and you won't have to always remember to do it. If you like emacs, just replace vi in the example above. This way, once you get to the end of that long line, you can type (literally)


That's the escape key (to get into vi command mode), the number 0 (to whisk you back to the beginning of the line), the letter i (to get you out of vi command mode and into insert mode) and the pound (#) symbol (to make the whole line a comment) and then the enter key. This will cause your line to become a comment, just like in a shell script and the shell won't execute it. Then you can type your preceding line and (assuming vi again) type:


Which is the escape key again (to get you into vi command mode) and the "k" key twice to move you up two lines in your history (which goes from newest to oldest from bottom to top), the x key (to delete the # (pound) character)) and then the enter key to have the shell execute your command line :) Yay! One down; one to go. Unfortunately, this won't work for shells that don't support line editing (like the bourne shell, as opposed to most Linux sh's which are usually just bash or dash)

2. How to clean up a huge mess when you untar a file that doesn't contain its own subdirectory. For instance, if you have this in your directory:

host # ls
file1.tar a b c d

and you untar file1.tar (which meets the above conditions), you might end up with this:

host # tar xpf file1.tar
file1.tar ab a bb b cb c db d x y z

and some of those new files might be directories with files in them, etc. This situation shouldn't be too bad, since you have very little in your directory. But sometimes, if you do it in /usr/local/bin (and, to add some more stress, can't rely on the file dates) this can create a very confusing situation. What do you get rid of without destroying everything?

There are a number of ways to get around this issue, but this one seems the fastest, with the least amount of hassle (feel free to combine the steps when you do this yourself; we're just keeping them apart for illustrative purposes):

To see what you've untarred (probably not necessary, but worth it if you happen to eyeball an important file you accidentally overwrote - another issue entirely ;)

host # tar tpf file.tar

Now, you know what was in your original tar file and, therefore, what you should be deleting :) It's very simple to do, but we'd recommend you run this command once, like this:

host # echo $(ls -1d `tar tpf bob.tar`)

just to be sure, and (if that looks good) remove the tar file contents and then do whatever you want with the tar file (like extract it to another directory):

host # rm $(ls -1d `tar tpf bob.tar`)

Also, if your shell doesn't support the $() expansion operators, you can always backtick your internal backticks like so: (or do something even more clever - there are probably more than a few ways to skin this cat ;)

host # rm `ls -1d \`tar tpf bob.tar\``

We'll see you tomorrow for the reading experiment. SPOILER ALERT: It's not what you think it is, unless you think it's what it is ;)


, Mike

Discover the Free Ebook that shows you how to make 100% commissions on ClickBank!

Please note that this blog accepts comments via email only. See our Mission And Policy Statement for further details.