Monday, January 19, 2009

Extracting Different File Types On Linux And Unix - Guest Post

Hey there, everyone, hope you had a pleasant weekend :)

Today's Unix/Linux post is courtesy of TuxHelper and deals with the extraction of various types of compressed files in the bash shell. More specifically, it deals with setting up a very clever function that you can include in your .bashrc.

This little function is brilliant, in that it's so simple, obvious, and convenient that I can't believe I never thought to do it myself (I must be a glutton for punishment ;)

Hope you enjoy it and can "extract" some value from it (I really had to "push" that one ;)

Cheers, (and if anyone else out there would like to submit content and help me stave off that impending nervous collapse, please feel free to send me a comment :)





Thanks to "plb" on the debian forum for providing the tip/trick!

Open your .bashrc file located in your /home/$USER/ directory. It is a hidden file as some like to call it in the Gnu/Linux world. Assuming you have opened your file find a spot to paste in the following text:


function extract()
{
if [ -f "$1" ] ; then
case "$1" in
*.tar.bz2) tar xjf "$1" ;;
*.tar.gz) tar xzf "$1" ;;
*.tar.Z) tar xzf "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.rar) unrar x "$1" ;;
*.gz) gunzip "$1" ;;
*.jar) unzip "$1" ;;
*.tar) tar xf "$1" ;;
*.tbz2) tar xjf "$1" ;;
*.tgz) tar xzf "$1" ;;
*.zip) unzip "$1" ;;
*.Z) uncompress "$1" ;;
*) echo "'$1' cannot be extracted." ;;
esac
else
echo "'$1' is not a file."
fi
}


Now go to your editor/notepad menu and save. The next time you have an archived file that need to be opened open a terminal/console and type: (example): extract myfile.zip!





, Mike



Yannis Tsopokis had this to add - Turns out this is all a lot easier than I ever thought. I'm officially a dinosaur ;)


For Linux there is unp which chooses which utility will extract the file and calls it.

Yannis

A huge supporter of the Rox Desktop had this to add - perhaps now, the correct source will get proper attribution!


This is actually from http://roscidus.com/desktop/Archive

tgz = Extract('tgz', "gunzip -c - | tar xf -")
tbz = Extract('tar.bz2', "bunzip2 -c - | tar xf -")
tarz = Extract('tar.Z', "uncompress -c - | tar xf -")
rar = Extract('rar', "unrar x '%s'")
ace = Extract('ace', "unace x '%s'")
tar = Extract('tar', "tar xf -")
rpm = Extract('rpm', "rpm2cpio - | cpio -id --quiet")
cpio = Extract('cpio', "cpio -id --quiet")
deb = Extract('deb', "ar x '%s'")
zip = Extract('zip', "unzip -q '%s'")
jar = Extract('jar', "unzip -q '%s'")

Justin Li noted this very important point! Thanks for contributing, Justin!


Hi Mike,

I have been a follower of the Menagerie for a while; your posts have taught me quite useful things. For the extract script though, wouldn't it be better if the script used file to find the filetype instead of matching against the file name? Especially since in Linux the file extension is only a convention for humans, the output of file would be much more accurate.

Keep writing about Linux!

Justin


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.