Linux – Recursively zip all files in a directory into individual zip files… but in a separate directory

command linelinuxunixzip

Similar questions have been asked, but I couldn't find exactly what I'm looking for. Basically, I have a large directory with subdirectories and many files, and I want to go into each directory and subdirectory recursively, and zip each file individually. I found this solution, but it's not quite what I want:

find . -type f -execdir zip '{}.zip' '{}' \;


This outputs the zip files into the directory they came from. However, I want the zip files in a completely separate directory, but with the same directory structure, with only the resulting zip files. Can anyone help with this?

Best Answer

You can pre-pend a relative path to the location of the zip file as:

find . -type f -exec zip -D '../zipdir/{}.zip' '{}' \; 

If needed, to create the directory structure you can do:

find . -type d -exec mkdir -p '../zipdir/{}' \;