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 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 "*/\.*"