0

I have the file archive.zip located in multiple directories, i.e.:

dir/001/archive.zip
dir/002/archive.zip
dir/003/archive.zip
...

Is there a way in which I can recursively unzip all the archive.zip files from all the directories with different names, using command line? The content of the ZIP files should be extracted to the same directory where the ZIP file resides.

The dir folder is common, only the parents of archive.zip have different names.

Thank you in advance!

0

1 Answer 1

1

This looks like a job for find. Change to the directory dir, and issue the following command:

find . -type f -name "archive.zip" -execdir unzip {} \;
  • This will search all directories starting with the current (., which is equal to dir in your case) for files (-type f) with the name archive.zip.
  • If found, it will execute the command unzip {} in the directory where the file was found (-execdir), with {} being the reference to the name of the found file (which find will replace with the actual filename - in this case, simply archive.zip).

It will have the same effect as saying cd 001/; unzip archive.zip; cd ..; cd 002/; unzip archive.zip ... (I think you get the point).

Note that the ; terminating the command-to-be-executed must really be escaped with a \, otherwise the shell will interpret it before it reaches find.

Also, if the dir/ directory itself can contain an archive.zip that you don't want extracted, you have to add the -mindepth 1 argument after the starting point . to ensure that find only considers files starting at the first subdirectory-level below dir/.

You must log in to answer this question.

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