I have a project with lots of hidden folders / files in it. I want to create a zip-archive of it, but in the archive shouldn't be any hidden folders / files. If files in a hidden folder are not hidden, they should also not be included.
I know that I can create a zip archive of a directory like this:
zip -r zipfile.zip directory I also know that I can exclude files with the -x option, so I thought this might work:
zip -r zipfile.zip directory -x .* It didn't work. All hidden directories were still in the zip-file.
8 Answers
First of all if you don't have installed zip install it first as follows:
sudo apt-get install zip Then for simply creating a zip file:
zip -r compressed_filename.zip foldername If you want to exclude hidden files:
find /folder_path -path '*/.*' -prune -o -type f -print | zip ~/compressed_filename.zip -@ Excluding Files from a Zip Archive (taken from )
2The basics of file exclusion when creating a zip archive are centered around the
-xflag, 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_Storefiles 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
.jpgfiles: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
.gitand it’s contents:zip -r zipdir.zip directorytozip -x "*.git*"Zip a folder, without including the
.svndirectory: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
.svnor an individual file like.bash_profileor.htaccess.zip -r archivename.zip directorytozip -x "*.*"Or to exclude all invisible files from all subdirectories:
zip -r archive.zip directory -x "*/\.*"
This also excludes hidden files in unhidden directories:
find /full_path -path '*/.*' -prune -o -type f -print | zip ~/file.zip -@ 1Add " to the .* (otherwise, your shell expands .* to the dot files in the current directory), and also exclude hidden files in subdirectories:
zip -r zipfile.zip . -x ".*" -x "*/.*" This will result in files starting with a . not to be added into your zip file.
rinzwind@discworld:~/tmp$ ls -la drwxr-xr-x 2 rinzwind rinzwind 4096 2011-08-28 00:15 tmp drwxr-xr-x 2 rinzwind rinzwind 4096 2011-08-28 00:15 .tmp rinzwind@discworld:~/tmp$ zip -r zipfile.zip . -x .* adding: .tmp/ (stored 0%) adding: tmp/ (stored 0%) rinzwind@discworld:~/tmp$ zip -r zipfile.zip . -x ".*" -x "*/.*" updating: tmp/ (stored 0%) 5Example for excluding all folders begining with . :
tar cvpzf folder.tgz folder/ --exclude '.*' Better compress but slower :
tar cvpjf folder.tar.bz2 folder/ --exclude '.*' This one includes all "." directories, subdirectories, and "." files or directories within directories... Essentially the first answer but includes top level "." files.
find /full_path -path '*.*/.*' -prune -o -type f -print | zip ~/file.zip -@ The correct method would be:
zip -r zipfile.zip directory -x directory/.* 2While zipping dirs excluding some file extension:
$ cd /path/to/dir $ zip -r dir.zip . -x "*.log" -x "*.cache" Without hidden folders and files in directory:
zip -r zipfile.zip directory/* directory:
|── .git │ ├── src │ └── Work.file ├── .test │ └── .file └── test.file $ zip -r zipfile.zip directory/* adding: directory/src/ (stored 0%) adding: directory/src/Work.file (stored 0%) adding: directory/test.file (stored 0%)