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?

  • 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 – NanoPish Mar 15 '17 at 18:04
  • 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 Mar 16 '17 at 15:24

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/{}' \;
| improve this answer | |
  • 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 Mar 16 '17 at 14:51
  • 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? – Aweston Mar 16 '17 at 14:56

If you're not married to zip files, you could just create a squashfs image of the whole directory tree in question that's mountable too (no opening & looking in specific zip files to see what's there). It may have better compression too, since it looks at the entire tree together, more like a "solid" archive, instead of lots of individual zip files.

Basically you just need the squashfs-tools package (that's what it's called in Debian/Ubuntu, maybe a different name in different linux families), then:

mksquashfs /some/directory  archive.squashfs

and to mount/view/browse it:

mount archive.squashfs /mnt/mountpoint -t squashfs

the -t flag may even be optional, mount is really good at automatically figuring out types. Shouldn't need the -o loop flag either, but try it just in case it's needed.

See http://tldp.org/HOWTO/SquashFS-HOWTO/creatingandusing.html or just search for how to use squashfs.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.