【问题标题】:How do I zip files that don't match certain criteria?如何压缩不符合某些条件的文件?
【发布时间】:2018-03-07 07:27:59
【问题描述】:

我需要制作一个 zip 存档,其中包含名称中没有“.processed”的目录中的所有文件。我想出了这条线

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

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

【问题讨论】:

  • 文件名是否以.processed结尾?
  • 是的,没错
  • 你能检查我的答案吗?
  • @Bobby:你实际看到了什么错误?
  • @Allan:OP 尝试使用\( ! -iname "*.processed" \) -print 可以正常工作,无需使用-not

标签: linux bash unix find zip


【解决方案1】:

只需使用以下命令即可:

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

若要否定条件,请将 -not 与 find 一起使用,always always always 在文件名中使用 单引号 和 find 以避免您的 shell 解释特殊字符。

测试:

$ 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

【讨论】:

  • 是的。这有效,但我发现如果我手动执行我的代码也有效。我猜我的脚本有些问题。感谢您的帮助,这是一个很好的解释,我想很多人可能会觉得这很有用
【解决方案2】:

我的手册上说(在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.

【讨论】:

    猜你喜欢
    • 2017-05-01
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多