Recursively zip all files in a directory into individual zip files... but in a separate directory
7,060
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/{}' \;
Related videos on Youtube
Comments
-
Aweston over 1 year
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?
-
Admin about 7 yearsYou can cp -r the directory containing all the directories and file somewhere. Then you delete all the non-zip files using find again, for example : find . -type f ! -name '*.zip' -delete that I found here unix.stackexchange.com/a/78378
-
Admin about 7 yearsI have a LOT of files, so I'd rather do this in as few operations as possible. Otherwise I would just do that.
-
-
Aweston about 7 yearsWhen I try doing this, it gives me errors: zip I/O error: No such file or directory zip error: Could not create output file (../test1_zipped/./file.zip)
-
Aweston about 7 yearsSo I tried creating the top-level directory for the output (test1_zipped/), and it zips each file in the top-level directory that I'm zipping from, but not any files in the sub-directories. Do I need to re-create the entire directory structure in the output directory with this solution? Is there an automated way to do that?