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/{}' \;
Share:
7,060

Related videos on Youtube

Aweston
Author by

Aweston

Software developer interested in web/mobile apps

Updated on September 18, 2022

Comments

  • Aweston
    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' '{}' \;
    

    Source

    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
      Admin about 7 years
      You 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
      Admin about 7 years
      I have a LOT of files, so I'd rather do this in as few operations as possible. Otherwise I would just do that.
  • Aweston
    Aweston about 7 years
    When 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
    Aweston about 7 years
    So 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?