36

I have many zip files a.zip, b.zip, c.zip, ... and I want to extract each of them into new folders a, b, c, ... respectively, via terminal.

Actually, what I want is a solution that I can use later with a find because I actually have many folders 2014, 2013, 2012, ... each of them containing many zip files a.zip, b.zip, etc. If I do find . -name "*.zip" -exec {} unzip \; it will unzip all the files and put them into their respective parent folder.

70

You should be able to use unzip's -d option to set an alternate directory for the archive contents.

unzip -d a a.zip
unzip -d b b.zip

and so on. Within a find expression, you should be able to derive the name for the directory from the name of the zipfile using shell parameter expansion e.g.

find -name '*.zip' -exec sh -c 'unzip -d "${1%.*}" "$1"' _ {} \;

Test it first by adding an echo i.e.

find -name '*.zip' -exec sh -c 'echo unzip -d "${1%.*}" "$1"' _ {} \;

or something like

while read -rd $'\0' f; do 
  unzip -d "${f%.*}" "$f"
done < <(find -name '*.zip' -print0)
  • 1
    +1 for the test it first… part – mrzmyr Dec 4 '18 at 15:27
  • 2
    find needs a directory, so find . -name ... – slf Dec 31 '18 at 16:41
  • @slf POSIXly, yes - but not in the case of GNU find ("If no starting-point is specified, . is assumed.") – steeldriver Dec 31 '18 at 17:00
10

I came looking for this myself, only to realize I'd already done it with other commands and it could be applied to just about anything else, the way I was already doing it.

The find method is crazy over-complicated for no reason.

for i in *.zip; do unzip "$i" -d "${i%%.zip}"; done
  • This is hands down the very best answer to the question at hand. The previous one is ridiculous and the one after this doesn't sort into the files respective root folders. – TheMegolith Mar 6 at 19:56
  • How would one go about creating the directory without the .zip suffix? Asking for a friend. – dashard Apr 15 at 14:37
  • The answer does not create a directory with a .zip suffix, that is the very point of the portion within the braces. – kencinder Apr 15 at 16:13
6

Simply Use

unzip '*.zip' -d /home/user/folder/
  • 9
    Unless I'm missing something, this will unzip everything into /home/user/folder, not into /home/user/folder/a and /home/user/folder/b – M. Justin Mar 27 '17 at 19:59
0

I also needed to do this using unrar. This can be achieved by a minor modification to kencinder's code.

for i in *.rar; do mkdir "${i%%.rar}"; unrar x -r "$i" "${i%%.rar}"; done 

PS: I wanted to add this as a comment but I don't have enough reputation points!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.