Tuesday, April 30, 2024
94
rated 0 times [  94] [ 0]  / answers: 1 / hits: 92953  / 3 Years ago, thu, june 17, 2021, 11:45:16

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.


More From » command-line

 Answers
2

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)

[#23413] Saturday, June 19, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
utilitere

Total Points: 394
Total Questions: 110
Total Answers: 114

Location: Solomon Islands
Member since Wed, Mar 29, 2023
1 Year ago
;