1

I want to exclude all .zip files in all subdirectories of the parent folder I'm zipping but I doesn't work. I have check the man zip pages, this and this questions without success.

I have tried:

zip -q -r FILE.zip * -x \*.zip
zip -q -r FILE.zip * -x\*.zip
zip -q -r FILE.zip * -x */\.zip
zip -q -r FILE.zip * -x \*.zip
zip -q -r FILE.zip * -x \*.zip\*
zip -q -r FILE.zip * -x*.zip

and I still get all zip files included (except the one in the parent folder generated with this command before).

EDIT: The problem was that I already had a FILE.zip in the folder which included some zip files. The zip command updates instead of creates a new compressed file if the file it attempts to create already exist, even if you don't indicate the -u option.

1
  • 1
    the first command is working for me..
    – AlexGreg
    Aug 27, 2014 at 21:52

1 Answer 1

2

You need to quote the exclude pattern, as you already tried to do; The first one should actually work, even if there is a "nicer" solution.

But let's look at a simplified example to work with; I Created some directories with files with .foo and .bar extensions:

Here I run find to list my test directory - it shows all files and directories:

$ find
.
./1.bar
./1.foo
./sub1
./sub1/sub2
./sub1/sub2/1.bar
./sub1/sub2/1.foo
./sub1/1.foo
./sub1/1.bar

Now, we can just see what works
(we get the output file out of the way, its not relevant to the matching).

Packing all files:

$ zip -r /tmp/out.zip *          
  adding: 1.bar (stored 0%)
  adding: 1.foo (stored 0%)
  adding: sub1/ (stored 0%)
  adding: sub1/sub2/ (stored 0%)
  adding: sub1/sub2/1.bar (stored 0%)
  adding: sub1/sub2/1.foo (stored 0%)
  adding: sub1/1.foo (stored 0%)
  adding: sub1/1.bar (stored 0%)

Packing excluding .foo files:

$ rm /tmp/out.zip
$ zip -r /tmp/out.zip * -x '*.foo'
  adding: 1.bar (stored 0%)
  adding: sub1/ (stored 0%)
  adding: sub1/sub2/ (stored 0%)
  adding: sub1/sub2/1.bar (stored 0%)
  adding: sub1/1.bar (stored 0%)

Works!
Trying again with your first pattern:

$ rm /tmp/out.zip
$ zip -r /tmp/out.zip * -x \*.foo 
  adding: 1.bar (stored 0%)
  adding: sub1/ (stored 0%)
  adding: sub1/sub2/ (stored 0%)
  adding: sub1/sub2/1.bar (stored 0%)
  adding: sub1/1.bar (stored 0%)

Works too!

So... take a close look at what you really did run there.

First you could remove the -q option for "quiet", hiding the standard messages shown in my examples.
As a next step, add the -v option for "verbose", to see more details - maybe too many, but maybe you can spot something interesting.


From the comments I see that removing the -q already helped;

The underlying problem was that the zip command used for testing was modified by the existence of the output file.
Technically, it had modified itself even...

From man zip:

Command format.  The basic command format is

               zip options archive inpath inpath ...

        where archive is a new or existing zip archive and inpath is a directory or
        file path optionally including wildcards.  When given the name of an exist‐
        ing zip archive, zip will replace identically named entries in the zip  ar‐
        chive (matching the relative names as stored in the archive) or add entries
        for new names.
5
  • Thank you Volker for your effort. The commands work for new zip files created with the zip command but not for the zip files I have in my folder, which were not created with zip command. That is weird!!
    – user308164
    Aug 27, 2014 at 23:32
  • Do you see something useful if you remove the -q option ("quiet")? Or even add the -v ("verbose") option? Aug 27, 2014 at 23:34
  • Ok, that helped a lot. Because I was trying many consecutive times, I already had a file named FILE.zip. So, even though I did not add the -u option, the process was indeed updating the files instead of adding them (didn't know that!). That's a weird thing. I could realize that by eliminating the -q option. Now it worked. Thank you Volker!
    – user308164
    Aug 27, 2014 at 23:45
  • From man zip: "When given the name of an existing zip archive, zip will replace identically named entries in the zip archive (matching the relative names as stored in the archive) or add entries for new names." Now I know this.
    – user308164
    Aug 27, 2014 at 23:53
  • Hey, that's good! And yes, the "update"- thing is odd; I added the rm lines because of that. Linux programs try to avoid doing things "by themselves" - but zip is more or less the same as it was in Windows, and DOS ;) (You can accept the answer with the tick mark, if it helped) Aug 27, 2014 at 23:59

You must log in to answer this question.