Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you recursively unzip archives in a directory and its subdirectories from the Unix command-line?

The unzip command doesn't have an option for recursively unzipping archives.

If I have the following directory structure and archives:

 /Mother/Loving.zip /Scurvy/Sea Dogs.zip /Scurvy/Cures/Limes.zip 

And I want to unzip all of the archives into directories with the same name as each archive:

 /Mother/Loving/1.txt /Mother/Loving.zip /Scurvy/Sea Dogs/2.txt /Scurvy/Sea Dogs.zip /Scurvy/Cures/Limes/3.txt /Scurvy/Cures/Limes.zip 

What command or commands would I issue?

It's important that this doesn't choke on filenames that have spaces in them.

like image 798
chuckrector Avatar asked Sep 20 '08 12:09

chuckrector


People also ask

How do I unzip a directory in Unix?

You can use the unzip or tar command to extract (unzip) the file on Linux or Unix-like operating system. Unzip is a program to unpack, list, test, and compressed (extract) files and it may not be installed by default.

Is unzip recursive?

The unzip command doesn't have an option for recursively unzipping archives. What command or commands would I issue? It's important that this doesn't choke on filenames that have spaces in them.


1 Answers

If you want to extract the files to the respective folder you can try this

find . -name "*.zip" | while read filename; do unzip -o -d "`dirname "$filename"`" "$filename"; done; 

A multi-processed version for systems that can handle high I/O:

find . -name "*.zip" | xargs -P 5 -I fileName sh -c 'unzip -o -d "$(dirname "fileName")/$(basename -s .zip "fileName")" "fileName"' 
like image 50
Vivek Thomas Avatar answered Sep 20 '22 09:09

Vivek Thomas