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

121

37

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?

sulman

Posted 2011-07-19T10:19:13.253

Reputation: 2 651

@AlexanderMills - See this question. – jww – 2018-01-01T01:02:51.080

Answers

134

I was so close!

The actual command I need is:

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

sulman

Posted 2011-07-19T10:19:13.253

Reputation: 2 651

2This did not work for me on osx. @pathfilder answer did though. – rynop – 2014-12-12T20:44:41.443

12Instead of this: -x dir1/ignoreDir1/*\*, you can do this: -x dir1/ignoreDir1/\ – Richard Gomes – 2016-01-22T14:37:21.647

Cool, and thanks for following up! You can accept your own answer, by the way. – Daniel H – 2011-07-20T03:43:30.100

73

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 myarchive.zip dir1 -x "dir1/ignoreDir1/*" "dir1/ignoreDir2/*"

Notes:

-- 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!

pathfinder

Posted 2011-07-19T10:19:13.253

Reputation: 831

7This is the one that works on Mac OS. :-) – physicalattraction – 2015-06-01T12:55:58.083

9As nobody has explained the reason for requiring either quoting the paths or escaping the asterisk, the reason is this: If the path is not quoted or the wildcard escaped, the shell will perform wildcard expansion before passing them to zip, which will receive a list of paths to entries in the specified directories, but not paths to files within subdirectories of those, thus causing zip to not to ignore everything under the given directories. – zagrimsan – 2015-12-01T13:57:43.910

I think originally one of the reasons this worked was because on Mac OS there were spaces in my directories and this avoided needing a \ – pathfinder – 2017-10-14T20:08:37.073

Thank you for clearing things up @zagrimsan. That both fixed the problem and explained the reason for it so that we understand it (which will also come in handy in the future for other things). – Synetech – 2019-06-25T13:38:42.797

1Relative paths works for me – Cazuma Nii Cavalcanti – 2020-01-06T16:53:50.643

9

@sulman using:

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

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

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

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

Gianluca P.

Posted 2011-07-19T10:19:13.253

Reputation: 191

Ah ok! Thanks for shedding the light on this! – sulman – 2013-10-30T14:51:13.950

7

The following will do

zip -r myarchive.zip 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.zip 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%)

ericn

Posted 2011-07-19T10:19:13.253

Reputation: 211

I don't know why the ** is needed. Maybe someone else can shed some light on this? – sulman – 2012-09-17T08:55:50.570

Sorry, @sulman, I typed wrongly. What I meant is as per my latest edit. Works a like a charm for me :) – ericn – 2012-10-31T07:52:20.913

1

Just like other answers, but excluding directories entirely, instead of excluding all contents of directories:

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

Richard Gomes

Posted 2011-07-19T10:19:13.253

Reputation: 129

0

I found this to work from David R Heffelfinger:

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

It excluded the directory and its contents.

Rick Ehrahrt

Posted 2011-07-19T10:19:13.253

Reputation: 1

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 Says 'Reinstate Monica' – 2014-10-03T19:48:43.740

-1

For me worked: zip -9 -r ~/folded.zip online -x folder/folder2/folder3/foldern/\*.

Seems to be that the asterisk must be escaped.

Francisco M

Posted 2011-07-19T10:19:13.253

Reputation: 1

2How does this differ from the solution by Rick Ehrahrt a year ago, or Eric's (a few more years ago)? Please do read the provided solutions before posting a new one... – zagrimsan – 2015-12-01T13:50:59.017

Because I added a backslash before the asterisk (that is to escape the asterisk). In the linux version at my work doesn't work if i don't do that. – Francisco M – 2015-12-15T09:30:38.380

Also Rick Ehrahrt (2014) and eric (2012) posted the same solution (to escape the asterisk). There's nothing wrong with the solution, but posting the same one multiple times doesn't make sense unless one can significantly add depth to the solution by explaining why the solution works. In a simple case like this, there's nothing to be explained more. Please don't take this personally, my only intention here is to help in keeping SU clean and concise so users can find the best solution with the least effort. – zagrimsan – 2015-12-15T10:24:50.903