如何压缩不符合特定条件的文件?

时间:2018-03-07 07:28:00

标签: linux bash unix find zip

我需要制作一个zip存档,其中包含目录中没有" .processed"的所有文件。在他们的名字。我想出了这一行

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

但由于某种原因它不起作用。我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

只需使用以下命令即可:

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

否定条件使用-not使用find,始终始终在文件名中使用简单引号并使用find来避免shell解释特殊字符。

<强> 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

答案 1 :(得分:1)

我的手册说(在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 -x@exclude.lst

          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.