How to Exclude Files from a Zip Archive
The easiest way to exclude many specific files or a group of matched files from a zip archive is by skipping the easy zipping utility built into Mac OS X’s friendly UI and turn over to the command line, where the powerful zip command resides.
This is useful for a million and one reasons, but the primary motivation for this post pertains to the .DS_Store files which get bundled along with zip archives created on a Mac, only to clutter up another machine that unzips the file, whether it’s on another Mac, Windows PC, or linux. That happens with both the friendly zip tool and the command line zip utility by default, and it’s because the zipping tools default behavior is to including hidden files whether they are shown or not. That’s not necessarily a bad thing and in many cases it would be considered useful, but if you don’t want them, or any other file for that matter, showing up in your archives, then read on.
How to Exclude Files from a Zip Archive
The basics of file exclusion when creating a zip archive are centered around the -x flag, which is used to exclude files from the archive that match a specific name or pattern. At it’s most basic, it will look like this:
zip archive.zip files -x "ExcludeMe"
Meaning you could exclude a single file, say it’s named “Nothanks.jpg”
zip archive.zip images/ -x "Nothanks.jpg"
Let’s cover a few specific examples where this is useful.
Exclude .DS_Store Files from Zip Archives
This will prevent the typically invisible Mac metadata .DS_Store files from being included in a zip archive, which are bundled in by default:
zip -r archivename.zip archivedirectory -x "*.DS_Store"
If the directory includes subdirectories however, you’ll want to use another variation of that command to exclude the the ds_store files from subdirectories as well:
zip -r archive.zip directory -x "*/\.DS_Store"
Note: not all shells require the quotations for this command to work properly, but in the bash shell (the default for Mac OS X) you will need to use the quotes for excluding with wildcards and patterns.
Exclude Specific File Types from a Zip Archive
With wildcards, you can also exclude all files of a certain type by focusing on the extension. For example, this command will zip an entire directory, minus any .jpg files:
zip -r archive.zip directory -x "*.jpg"
That could be modified for any specific file extension or pattern matched in a file name.
Exclude the .git or .svn Directory from a Zip Archive
Zip a directory, minus .git and it’s contents:
zip -r zipdir.zip directorytozip -x "*.git*"
Zip a folder, without including the .svn directory:
zip -r zipped.zip directory -x "*.svn*"
Exclude All Hidden Files from a Zip Archive
Since patterns and wildcards can be used, you could also exclude any or all invisible files and folders that are made so by being prefixed with a period, whether it’s a directory like .svn or an individual file like .bash_profile or .htaccess.
zip -r archivename.zip directorytozip -x "*.*"
Or to exclude all invisible files from all subdirectories:
zip -r archive.zip directory -x "*/\.*"
Cheers to a commenter on the Macworld Forums for the precise syntax on excluding those files from subdirectories as well.
Ultimately, this is just another reason for power users to jump to the Terminal for creating archives. With powerful features like wildcard support, exclusion, and optional password protection of zips, it’s just more full-featured, and since it’s all included on the Mac anyway you won’t need to download another app to support the advanced features.
And yes, technically if you were determined to stay in the UI you could use Finder and Spotlight search operators to narrow down a folders contents in Mac OS X before creating an archive, or just Select All and manually Command+Click each file to not include, but that is really not efficient for large archiving operations. Thus, the terminal wins out for ease, and despite being centered around the command line, it’s really not complicated once you learn the basics.
It would be extra helpful to indicate how to exclude a given filename at an arbitrary folder depth e.g. exclude .DS_Store, */.DS_Store, */*/.DS_Store, to any arbitrary depth. And likewise to exclude a given folder at arbitrary depth (I’m looking at you node_modules). Thanks for the helpful article.
Kool, I’ve got it now, thanks.
Hi
I want to exclude all files and folders inside a subdirectory except one file.
For example cache folder has an ‘index.html’ file and several subdirectories. I want the archive to include the cache folder with ‘index.html’ file and none of the other files or subdirectories.
Is this possible?
Thanks
Hey, excellent article, just one thing missing…
How would one exclude several files in various directories/sub-directories
Would this be the correct syntax?
zip -r archive.zip directory -x “*/\.DS_Store” “*/\.Thumbs.db” “*/\.localized” “*/\error_log”
Thanks!
From the zip man page:
-x files
–exclude files
Explicitly exclude the specified files, as in:
zip -r foo foo -x \*.o
which will include the contents of foo in foo.zip while exclud-
ing all the files that end in .o. The backslash avoids the
shell filename substitution, so that the name matching is per-
formed by zip at all directory levels.
Also possible:
zip -r foo foo -x@exclude.lst
which will include the contents of foo in foo.zip while exclud-
ing all the files that match the patterns in the file
exclude.lst.
The long option forms of the above are
zip -r foo foo –exclude \*.o
and
zip -r foo foo –exclude @exclude.lst
Multiple patterns can be specified, as in:
zip -r foo foo -x \*.o \*.c
If there is no space between -x and the pattern, just one value
is assumed (no list):
zip -r foo foo -x\*.o
That is A-WE-SOME.
…