Saturday, April 7, 2007

Forgotten Basic Bash Tools (and some obscure tips)

Wow, i did not realize it had been so long since my last post. For all of you out there that check my blog: sorry. In my own behalf I was busy busy getting engaged and a couple of life altering ordeals. And since this is not related to Linux (Auuu Auuu! (....just watched 300, if you watched im sure you have laughed there!)) I will not extend myself there.

Ok, so I know that most people that want to become a Linux/*NIX/Darwin "expert" immediately dive into the bare bones essentials like how to change directory, rename files... and then crash course into some scripting program like php, ruby, or my weapon of choice perl, and that is that. But they miss a lot of the subtle and beautiful programs included with most distros. Some of them have been around since there very first days of UNIX like, cat or tail and some made it later. But they are powerful and elegant, just try doing some in front of your geek friends, you will earn their awe and probably some resentment along the way.

Lets get started:

tac: the evil twin brother of cat (which stands for concatenate, not the sneaky feline), and it does exactly what cat does (concatenate and print files to standard output) but with a twist. It does it in reverse. So by now like in a M. Night Shyamalan movie, things are all falling into place in your head. Just like cat, tac is the perfect UNIX philosophy example: It does ONE thing, and it does it well.

Ex:

[alex@galapago ~]$cat hello.txt
Hello
my
friends!

[alex@galapago ~]$tac hello.txt
friends!
my
Hello

Simple, elegant and functional, it has a couple of useful switches there. Mix it up with other bash commands, throw in some pipes and voilá you've got yourself a masterpiece.


split : Based on actual events-- Not so long ago i was asked to split a file into several files every million lines, and I thought to myself, "This is an easy job for perl" and indeed it was. Later on i found that i had missed this beautiful command named split whose function in to do exactly that (in less time)

For example

alex@alex-desktop:~/numbers$ cat numbers.txt
one
two
three
four
five
six

And we wish to split in files with three lines:

alex@alex-desktop:~/numbers$ split -l 3 numbers.txt div

div here repre

alex@alex-desktop:~/numbers$ ls
divaa divab numbers.txt
alex@alex-desktop:~/numbers$ cat divaa
one
two
three