Zipping all files/dirs in a directory except hidden ones?

This works for me:

zip -r mydir.zip mydir -x "*/.*"

@Joe Internet, @Dariusz: normal shell patterns won't work properly because zip matches against the full path+filename internally (as the zip manual explains... ;) )


If you prefer more complex filtering capabilities, find is a good tool:

find mydir/ -! -name ".hello" -print | zip mydir -@

Have fun with 'find'.


The following approach works for this type of directory tree:

$ tree .
.
├── adir1
│   ├── afile1
│   ├── afile2
│   ├── afile3
│   └── afile4.txt
├── adir2
│   ├── afile1
│   ├── afile2
│   ├── afile3
│   └── afile4.txt
├── adir3
│   ├── afile1
│   ├── afile2
│   ├── afile3
│   └── afile4.txt
├── afile1
├── afile2
├── afile3
├── foo.test

This was the solution that worked for this scenario (which I believe is the more general case).

 $ find . -type f -not -path '*/\.*' -exec zip -r test.zip {} +

Example

$ find . -type f -not -path '*/\.*' -exec zip -r test.zip {} +
updating: adir1/afile1 (stored 0%)
updating: adir1/afile1.zip (stored 0%)
updating: adir1/afile2 (stored 0%)
updating: adir1/afile3 (stored 0%)
updating: adir1/afile4.txt (stored 0%)
updating: adir2/afile1 (stored 0%)
updating: adir2/afile2 (stored 0%)
updating: adir2/afile3 (stored 0%)
updating: adir2/afile4.txt (stored 0%)
updating: adir3/afile1 (stored 0%)
updating: adir3/afile2 (stored 0%)
updating: adir3/afile3 (stored 0%)
updating: adir3/afile4.txt (stored 0%)
updating: afile1 (stored 0%)
updating: afile2 (stored 0%)
updating: afile3 (stored 0%)
updating: foo.test (deflated 87%)

Tags:

Unix

Bash

Script