Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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 ubuntu 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.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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)
share|improve this answer
    
Works perfectly. Could you explain what is the command doing? Specifically the -exec part. –  Michael Aug 31 at 13:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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