General Computing
unix zip exclude
Updated Mon, 18 Jul 2022 21:14:09 GMT

Unix zip directory but excluded specific subdirectories (and everything within them)


I'm trying to zip a directory (on Unix via SSH) but I need to exclude a couple of subdirectories (and all files and directories within them).

So far I have this:

zip -r myarchive.zip dir1 -x dir1/ignoreDir/**/* 

That doesn't seem to work though.

I also tried

zip -r myarchive.zip dir1 -x dir1/ignoreDir1/* dir1/ignoreDir2/*

However that will still include subdirectories within ignoreDir1 and ignoreDir2.

The subdirectory structure in the directories that I want to exclude is quite substantial so I can't simply add each directory to the -x argument.

Does anyone know how to do this?




Solution

I was so close!

The actual command I need is:

zip -r myarchive.zip dir1 -x dir1/ignoreDir1/**\* dir1/ignoreDir2/**\*




Comments (5)

  • +3 – This did not work for me on osx. @pathfilder answer did though. — Dec 12, 2014 at 20:44  
  • +0 – Instead of this: -x dir1/ignoreDir1/**\*, you can do this: -x dir1/ignoreDir1/\* — Jan 22, 2016 at 14:37  
  • +2 – This worked for me zip -r theme.zip ./theme -x ./theme/node_modules/\* — Apr 09, 2021 at 18:14  
  • +1 – @AhmadKarim exactly what I needed for node_modules :) — Apr 14, 2021 at 07:13  
  • +0 – On MacOS I was successful with: zip -r theme.zip theme -x theme/node_modules/\* or zip -r theme.zip theme -x "theme/node_modules/*" — Sep 15, 2021 at 04:53