Extract without First Directory

Whenever I download something that is compressed on the Internet in a .zip, .rar or .tar.gz it is always a crapshot whether or not it contains a “container directory”. A “container directory” is a directory that contains all the other files usually with the same name as the compressed file.

For example the Zend Framework when downloaded contains a folder called, ‘ZendFramework-1.7.2‘. All the other files are contained under this folder. This is great but sometimes I want to extract the contents of the folder without the “container folder”.

This is how I used to extract the contents and remove the “container folder”:

tar -xvf ZendFramework-1.7.2.tar.gz

Get rid of the tarball…

rm ZendFramework-1.7.2.tar.gz

cd ZendFramework-1.7.2/

Which would result in:

Zend Framework Directory Structure

Copy everything in the “container” folder and move it up a directory.

cp -rf * ../

Now I have found a better way…

A better way

 
The flag that I have learned is the strip flag. This will strip off the first directory and extract the rest.

tar -xvf ZendFramework-1.7.2.tar.gz --strip 1

The only thing now is… How do I tell if a tar contains a “container folder”?

Easy

tar -tf ZendFramework-1.7.2.tar.gz | head

This will list contents of the file ‘ZendFramework-1.7.2.tar.gz‘ showing only the first few lines.

What do you think? Is there an even better way?


Was this information useful?


5 Responses to "Extract without First Directory"
  1. Srikanth on January 6th, 2009

    Mark, thanks a lot for the tip! This always bothered me.

  2. Jade Robbins on January 6th, 2009

    Very useful. I wonder though what it will do if, say, you have a README file in the root of the tar and THEN a head directory. Will it just merge all of them into the root? Will it just not extract the README? You should find out :D

  3. Mark Sanborn on January 6th, 2009

    It will ignore the readme, which is why you need to use, tar -tf blah.tar.gz | head

  4. Matt on January 14th, 2009

    Finally I’ve found what I was looking for!

    Maybe there’s a way to automatically detect if there’s a container folder since it seems the container folder often shares the same name as the .tar.gz file without the extension?

  5. Jade Robbins on January 25th, 2009

    This is GENIUS! Seriously how do you come up with these ideas?!?!?! LOVE IT