Archive for September, 2011

Untar Several Files to a directory


Posted by adamlinuxhelp  |  No Comments »

Today we’re going to talk about the UNIX tar command.

Specifically, how to use tar to extract or unpack several archives (tar.gz files).  Only – instead of extracting them to the same directory, we’re going to extract them to a named directory.

This utility came in handy after downloading close to 10 gkrellm themes from . I have used the Gkrellm Monitoring tool over the years and was always disappointed with the (somewhat) `boring` stock themes.  I mean, if you’re going to have something sitting on your desktop, using resources to report on resources, then why not have it look slick, sick, or at the very least—”easy on the eyes”… right?

Why would anyone want to unpack tar.gz files with the command line in Linux when there are GUI tools available?

Right, the linux OS comes packed with tons of GUIs to handle files and archives.  We have programs such as FileRoller.  I have used it but imagine you have downloaded (oh I don’t know, twelve) archives that are themes for gkrellm.  Would you really want to use the manual effort of unpacking all of them first, directing the GUI (with more clicks) where to save the extracted files to?

What if I told you that you can accomplish the same in the Linux Shell with a basic loop over a set of tar.gz files?  You’d probably want to take the quick way I bet.  I know I did.

Ok, get on to it already.  Right. Here we go.  Firstly, I’d be a bad dude if I didn’t at least name some links that I googled along the way to building the handy-dandy loop I’m about to share with ye. very simple layout, 2 commands that do the trick.  And which has the command, and provides advice too.

Using the basic concept of the command, which unpacks a tar.gz to a named directory:

tar -zxvf tarball.tar.gz -C <directory>

Then, using a loop construct, where the find command provides a list of tar.gz files, we iterate over the list, unpacking each one to the specified directory following the -C.

The setup:

  1. Have all of your (downloaded) tar.gz files in the same directory.  You may want to get rid of archives that are unrelated to gkrellm themes.
  2. CD to that directory.  We will extract to the .gkrellm2/themes.
for fyl in $(find . -type f -name "*gz");\
do tar -zxvf $fyl -C /home/awsmeadam/.gkrellm2/themes; done

And that’s it.  Done.  Did you download more tar.gz themes?  Ok, run the command again.

If I run the command again, the previously-unpacked files will get over-written, won’t they?

Yes, they will.  In most cases, this is bad.  But since this is a theme unpack, you really have little to worry about unless you’ve edited or otherwise customized a theme.

Enjoy, and happy Linux-ing.