Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 dir1 -x dir1/ignoreDir/**/* 

That doesn't seem to work though.

I also tried

zip -r 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?

share|improve this question

migrated from Jul 19 '11 at 11:58

This question came from our site for professional and enthusiast programmers.

5 Answers 5

up vote 30 down vote accepted

I was so close!

The actual command I need is:

zip -r dir1 -x dir1/ignoreDir1/**\* dir1/ignoreDir2/**\*
share|improve this answer
Cool, and thanks for following up! You can accept your own answer, by the way. –  Daniel H Jul 20 '11 at 3:43
Just what I was looking for, thanks dude! –  Latheesan Dec 4 '13 at 10:20
This did not work for me on osx. @pathfilder answer did though. –  rynop Dec 12 '14 at 20:44

For my particular system in order to exclude a directory I had to put quotes around my excluded directories and it worked like a charm:

zip -r dir1 -x "dir1/ignoreDir1/*" "dir1/ignoreDir2/*"


-- this excluded both the directory to exclude and all files inside it.

-- You must use the full path to the directories you want to exclude!

share|improve this answer
This is the one that works on Mac OS. :-) –  physicalattraction Jun 1 at 12:55

The following will do

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

What did you need the ** for, @sulman?

It works like a charm for me as follows:

[root@ip-00-000-000-000 dir1]# ls -lrt dir1/ 
total 16
drwxr-xr-x 2 root root 4096 Oct 31 07:38 ignoredir1
drwxr-xr-x 2 root root 4096 Oct 31 07:38 ignoredir2
drwxr-xr-x 2 root root 4096 Oct 31 07:39 dir3
-rw-r--r-- 1 root root    8 Oct 31 07:39 test.txt

[root@ip-00-000-000-000 temp]# zip -r dir1 -x dir1/ignoredir1\* dir1/ignoredir2\*
  adding: dir1/ (stored 0%)
  adding: dir1/dir3/ (stored 0%)
  adding: dir1/dir3/test3.txt (deflated 13%)
  adding: dir1/test.txt (stored 0%)
share|improve this answer
I don't know why the ** is needed. Maybe someone else can shed some light on this? –  sulman Sep 17 '12 at 8:55
Sorry, @sulman, I typed wrongly. What I meant is as per my latest edit. Works a like a charm for me :) –  eric Oct 31 '12 at 7:52

@sulman using:

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

will still include dir1/ignoreDir1/ empty folder in the zip archive, using:

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

will do the trick, you can also use a leading ** to search in subfolders instead of only dir1

share|improve this answer
Ah ok! Thanks for shedding the light on this! –  sulman Oct 30 '13 at 14:51

I found this to work from David R Heffelfinger:

zip -r dir1 -x dir1/ignoreDir1\* dir1\ignorDir2\*

It excluded the directory and its contents.

share|improve this answer
Surely the backslash between dir1 and ignorDir2 is meant to be a forward slash. Do you mean for the other backslashes to be backslashes? If you do, then you have just copied fuzzybee's answer from two years ago. –  G-Man Oct 3 '14 at 19:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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