How can I create a zip archive of a whole directory via terminal without hidden files?

by Martin Thoma   Last Updated December 07, 2017 11:02 AM

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.

Tags : command-line zip


Answers 9


Add " 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%)
Rinzwind
Rinzwind
August 27, 2011 19:17 PM

This also excludes hidden files in unhidden directories:

find /full_path -path '*/.*' -prune -o -type f -print | zip ~/file.zip -@
arrange
arrange
August 27, 2011 19:27 PM

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 -@
cosimnot
cosimnot
November 01, 2012 03:19 AM

The correct method would be:

zip -r zipfile.zip directory -x directory/.*
Rômulo Neves
Rômulo Neves
May 08, 2014 16:49 PM

Example for excluding all folders begining with . :

tar cvpzf folder.tgz folder/ --exclude '.*'

Better compress but slower :

tar cvpjf folder.tar.bz2 folder/ --exclude '.*'
Tigrouzen
Tigrouzen
May 21, 2014 16:51 PM

While zipping dirs excluding some file extension:

$ cd /path/to/dir
$ zip -r dir.zip . -x "*.log" -x "*.cache"
K-Gun
K-Gun
December 08, 2014 17:49 PM

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 -@
M A K Ripon
M A K Ripon
August 11, 2015 06:28 AM

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%)
Umanshield
Umanshield
February 15, 2017 08:45 AM

You can do:

sudo apt-get install zip unzip

and then use the zip and unzip programs.

simhumileco
simhumileco
December 07, 2017 11:01 AM

Related Questions



Run command with spaces on startup

Updated October 19, 2017 11:02 AM


How to install ZIP file

Updated May 08, 2017 22:02 PM

Repeat terminal command until specified output

Updated February 23, 2016 03:01 AM