Thursday, October 25, 2007

I love cats (not the animal)

Personally, I think one of the most useful tools in linux is cat (it is short for conCATenate). I use it for all sorts of things like sending text files through a pipe (stdin) to other tools such as sed. In fact, that is how I usually upgrade my system:

cat /etc/apt/sources.list | sed -e s/feisty/gutsy/g | sudo tee /etc/apt/sources.list ; sudo apt-get update ; sudo apt-get dist-upgrade

That sends the text file sources.list to sed (stream editor), replaces all instances of feisty with gutsy, and sends the end result to tee which writes the end text to the new sources.list. It then updates apt and upgrades my system accordingly. The reason I like this is because I can ssh into my box remotely and upgrade my system without the need of a GUI (I know there is ssh -X, but it can be dead slow over a bad connection).

Other good uses of cat are joining multiple files together (in conjunction with tar and split, you can do some pretty powerful stuff with backing up your data). Let's say you have a couple different PDF's that you would like to join into one, you would do something like this:

cat pdf1.pdf pdf2.pdf pdf3,pdf > new_pdf.pdf

Now you have all your PDF's in one handy one. This also works if you have a movie that is broken up into parts such as Stephen King's The Stand:

cat The\ Plague.avi The\ Dreams.avi The\ Betrayal.avi The\ Stand.avi > Stephen\ King\'s\ The\ Stand.avi

The result is a nice 2.7 gig file that has all the parts in it.

Ok, so putting files together is really useful, but what about taking them apart? split can help us. Let's say you backup your system to a tarball every week and the resulting tarball is several gigs and all you have is a CD burner. How do you get those backups back to your computer quickly using CD's?

split -b 650m backups.tar.gz

That breaks up your backups tarball into x amount of 650MB files (they are named by default aa, ab, ac, ad, etc...) that can be put together with cat after being transferred to your host machine.

No comments:

Post a Comment