Bobby
Bobby

Reputation: 584

How do I zip files that don't match certain criteria?

I need to make a zip archive with all files in a directory that don't have ".processed" in their names. I came up with this line

cd source_directory && find . -type f \( ! -iname "*.processed" \) -print | zip target_direcory/result_test.zip -@

But it doesn't work for some reason. What am I doing wrong here?

Upvotes: 0

Views: 779

Answers (2)

Jean-Baptiste Yunès
Jean-Baptiste Yunès

Reputation: 36401

My manual says (and it is almost the same under Linux):

   -x files
   --exclude files
          Explicitly exclude the specified files, as in:

                 zip -r foo foo -x \*.o

          which will include the contents of foo in foo.zip while  exclud-
          ing  all  the  files  that  end in .o.  The backslash avoids the
          shell filename substitution, so that the name matching  is  per-
          formed by zip at all directory levels.

          Also possible:

                 zip -r foo foo [email protected]

          which  will include the contents of foo in foo.zip while exclud-
          ing  all  the  files  that  match  the  patterns  in  the   file
          exclude.lst.

          The long option forms of the above are

                 zip -r foo foo --exclude \*.o

          and

                 zip -r foo foo --exclude @exclude.lst

          Multiple patterns can be specified, as in:

                 zip -r foo foo -x \*.o \*.c

          If  there is no space between -x and the pattern, just one value
          is assumed (no list):

                 zip -r foo foo -x\*.o


          See -i for more on include and exclude.

Upvotes: 1

Allan
Allan

Reputation: 12438

Just use the following command and it will work:

cd source_directory && find . -type f -not -iname '*.processed' | zip target_direcory/result_test.zip -@

to negate a condition use -not with find and always always always use simple quotes in the file name with find to avoid your shell to interpret special characters.

TEST:

$ tree .
.
├── a
├── a.processed
├── b
└── c

0 directories, 4 files

$ find . -type f -not -iname '*.processed' | zip output.zip -@
  adding: c (stored 0%)
  adding: a (stored 0%)
  adding: b (stored 0%)

$ tree
.
├── a
├── a.processed
├── b
├── c
└── output.zip

$ less output.zip
Archive:  output.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
       0  Stored        0   0% 2018-03-07 16:44 00000000  c
       3  Stored        3   0% 2018-03-07 16:45 ed6f7a7a  a
       4  Stored        4   0% 2018-03-07 16:45 3aa2d60f  b
--------          -------  ---                            -------
       7                7   0%                            3 files

Upvotes: 1

Related Questions