0

I try to unzip the content of a specific folder of a zip file. My zip file structure looks like:

  • folder1
    • folder1_1
    • file1_1
  • folder2
  • folder3
  • file1
  • file2

I want to unzip only the content of folder1.

I tried to using:

unzip -d path_to_unzip/ results.zip 'folder1/*'

Unfortunately it creates a structure like:

path_to_unzip/folder1/folder_1_1 etc...

How can I archieve to get rid of the unnecessary folder1?

Many thanks in advance!

1
  • Doesn't look like Info-ZIP has any way of stripping directory prefixes. Oct 25, 2017 at 5:43

1 Answer 1

0

How can I archieve to get rid of the unnecessary folder1?

As Ignacio correctly pointed out, unzip cannot.

I would propose something like this:

TMPDIR=`mktemp -d` || exit 1
unzip -d ${TMPDIR}/ results.zip 'folder1/*'
mv ${TMPDIR}/folder1/* path_to_unzip/
rm -r ${TMPDIR}

Basically, since mv file operations on a single filesystem are atomic, as in, performed "almost" immediately, there is no real performance impact ... provided your /tmp is on the same filesystem as the destination path. If it is not and the ZIP file is of great size, you should extract it to a temporary folder on the target filesystem. Here, we take the parent of the target directory:

TMPDIR=`mktemp -p path_to_unzip/.. -d` || exit 1
4
  • Thanks for that solution! Is it possible to archive this with tar? Oct 25, 2017 at 7:18
  • Nope, not that I know of, neither Solaris tar nor GNU tar can do what you want.
    – thecarpy
    Oct 25, 2017 at 7:55
  • 1
    GNU tar has --strip-components. Oct 25, 2017 at 8:07
  • Learned something new today, you can also use --transform=SED_EXPRESSION nice!
    – thecarpy
    Oct 26, 2017 at 7:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .