I have a script that extracts a tar.gz-file to a speificed subdirectory mysubfolder:
mkdir mysubfolder; tar --extract --file=sourcefile.tar.gz --strip-components=1 --directory=mysubfolder;
Is there any equivalent way of doing this with a zip-file?
migrated from stackoverflow.com Dec 12 '12 at 14:05
This question came from our site for professional and enthusiast programmers.
As Mathias said,
Problem is: the best approach depends on your archive layout. A solution that assumes a single top-level dir will fail miserably if the content is directly in the archive root (think about
And the same fail happens with
So, to strip the root dir, assuming there is one (and only one):
Just make sure second-level files/dirs do not have the same name of the top-level parent (for example,
If you're using Bash, you can test if top level is a single dir or not using:
Speaking of Bash, you should turn on
Now put that in your
(notice it will automatically create
I couldn’t find such an option in the manual pages for
However, (depending on the situation) you could work around it. For example, if you’re sure the only top-level directory in the zip file is named