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

6

0

How can I make a zip of all the files and subdirectories in the directory mydir, except all those files/dirs that begin with a .*?

The command:

zip -r mydir.zip mydir/

...will include everything. For example, if I have:

mydir/foo
mydir/bar
mydir/.hello

I'd like foo and bar to be included in mydir, but not .hello.

How can I do this?

user46976

Posted 2010-08-21T16:42:23.643

Reputation: 1 078

Answers

9

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... ;) )

JanC

Posted 2010-08-21T16:42:23.643

Reputation: 1 125

1Claeys - My example workerd for me, but to be fair, I used Ch Shell (includes zip) under Win7. I CDed into my test directory, and used 'zip -r test.zip ./ -x ..' (minus the single quotes). If I use 'zip -r mydir.zip mydir -x "/."' (minus the single quotes), it doesn't exclude the hidden file in my test directory. I guess it's down to differences in the shells used to test with. – Joe Internet – 2010-08-22T19:01:15.453

1

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%)

slm

Posted 2010-08-21T16:42:23.643

Reputation: 7 449

1

You can use shell patterns to exclude matches, all is written in zip manual (with examples)

canni

Posted 2010-08-21T16:42:23.643

Reputation: 205

1

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

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

Have fun with 'find'.

levif

Posted 2010-08-21T16:42:23.643

Reputation: 141

0

Try this...

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

Joe Internet

Posted 2010-08-21T16:42:23.643

Reputation: 5 145

0

Shorter, and takes advantage of the features of globbing:

zip -r mydir.zip mydir/*

(. files are not included in the * wildcard)

Note that the directory 'mydir/' may not be included in the paths of the files in the resultant zip file, so this will change the output slightly. You may have to change your extraction process as a result.

Slartibartfast

Posted 2010-08-21T16:42:23.643

Reputation: 6 899