【问题标题】:Zip command for excluding directories and files用于排除目录和文件的 Zip 命令
【发布时间】:2012-02-12 11:50:06
【问题描述】:

我正在尝试在 centos 5 中使用这个 ssh 命令来压缩一个完整的文件夹和文件的目录,但不包括下面的示例。它不工作。

zip -r file.zip * -x dir1 -x dir2 -x file1 -x file2

或者这个

zip -r file.zip * -x "dir1" -x "dir2" -x "file1" -x "file2"

它仍然只是压缩整个目录及其中的所有内容。我不想要 dir1 dir2 file1 file2。

我需要知道正确的 -x 语法才能使排除函数起作用。

【问题讨论】:

标签: ssh directory centos zip


【解决方案1】:

对于我的特定系统,为了排除一个目录,我必须在我排除的目录周围加上引号,它就像一个魅力:

zip -r myarchive.zip target -x "target/exclude1/*" "target/exclude2/*"

注意事项:

-- 这排除了要排除的目录和其中的所有文件。

-- 必须使用要包含和排除的目录的完整路径!

-- 只要排除项位于行尾,您就不需要 @ 符号

【讨论】:

  • 这是唯一对我有用的答案!
【解决方案2】:

-x 选项有点奇怪;您列出文件,以-x 开头,以@ 符号结尾:

   zip file.zip -r * -x dir1 dir2 file1 file2 @

我也严重怀疑您是否希望在 Unix 系统上的文件名中包含 \……确保您的路径正确。 / 是常用的路径分隔符。

例子:

mkdir tmp5
cd tmp5
touch a b c d e f g
zip foo.zip -r * -x e f g @
  adding: a (stored 0%)
  adding: b (stored 0%)
  adding: c (stored 0%)
  adding: d (stored 0%)

对于子目录,您可以使用通配符轻松省略其内容,但似乎* 会导致目录本身被包含(空):

mkdir x y z
touch {a,b,c} {x,y,z}/{a,b,c}
zip foo.zip -r * -x c y y/* z z/* @
  adding: a (stored 0%)
  adding: b (stored 0%)
  adding: x/ (stored 0%)
  adding: x/b (stored 0%)
  adding: x/a (stored 0%)
  adding: x/c (stored 0%)
  adding: y/ (stored 0%)
  adding: z/ (stored 0%)

您最好省略* 并明确列出您想要包括的那些事情......

zip core-latest-$version.zip -r cp images include mobile stats \
    -x cp/includes/configure.php @

(末尾的\ 只是继续到下一行;您可以将其全部放在一行上,而无需尾随\

【讨论】:

  • 好的,很奇怪。我从 about.com 得到了反斜杠,他们做了某种 -x \*.0 来排除所有以 .0 结尾的文件。不知道。无论如何,我尝试了您的命令,但仍然无法正常工作。这是我的实际命令。 zip core-latest.zip -r * -x 模板 -x stats -x cp/includes/configure.php @
  • 应该只需要一个-x在头部和一个@在最后......而且,你真的在​​你正在压缩的目录中运行zip吗?跨度>
  • 这里是我的脚本:echo -n "enter new version (v1-1)" read version cd /home/core/www/ rm -rf core-latest-*.zip zip core-latest- $version.zip -r * -x 模板统计 cp/includes/configure.php @chown -R core.core core-latest-$version.zip
  • 上一级更常见,这样您的 ZIP 包含一个顶级文件夹。不是“错误”,只是不寻常。看起来不错,似乎对我有用。
  • 你的意思是压缩 www 目录?这是一个 php 程序,我们想安装在其他服务器上以供下载,他们会将其放入 www 或 public_html 目录并解压缩并完成。
【解决方案3】:

如果您使用的是 putty,请按照以下步骤操作:

  1. 输入您的目录:$ cd directory_name 并回车
  2. 写命令:

    $ zip -r file_name.zip . -x \*excludingfolderName\*
    

zip 之后使用. 表示当前目录,* 是通配符。

【讨论】:

    【解决方案4】:

    根据我对zip 命令的有限经验,-x 选项似乎想要模式匹配文件的整个路径名。因此,如果您想排除 dir1 dir2 目录中的所有内容以及任何名为 file1 file2 的文件,请尝试以下操作:

    zip -r file.zip * -x 'dir1/*' 'dir2/*' '*/file1' '*/file2'
    

    您拥有它的方式只会排除在根级别与这些名称完全匹配的文件和目录。

    【讨论】:

      猜你喜欢
      • 2016-02-26
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多