1

我想根据 find 命令的结果创建一个 ZIP。但是当 zip 命令没有任何要压缩的内容时会引发错误。

find ${DIRECTORY_TO_SEARCH} -type f -mtime -7 | xargs zip "${ZIP_FILE}"

zip 命令抛出的错误代码:zip error: Missing or empty zip file

如何确保仅在文件数大于 0 时才创建 ZIP?

谢谢!

4

1 回答 1

1

如果你有 GNU xargs,你可以使用-r开关:

--no-run-if-empty
-r    If the standard input does not contain any nonblanks, do not run the command.
      Normally, the command is run once even if there is no input.
      This option is a GNU extension.

例子:

$ echo "" | xargs echo "foo"
foo
$

$ echo "" | xargs -r echo "foo"
$
于 2013-04-20T09:33:29.327 回答