【问题标题】:How to zip files with exclude list in linux/unix如何在 linux/unix 中使用排除列表压缩文件
【发布时间】:2012-06-15 20:45:01
【问题描述】:

我有一个要压缩的文件列表,但我也有一个排除文件的列表,不希望它们包含在 zip 存档中。

所以我创建了一个 exclude.lst 文件,其中包含绝对路径和文件名。

样本排除文件

/home/logs/apache/access.log
/home/logs/tomcat/catalina.out

但是使用下面的命令后,zip 命令并没有排除文件,而是将它们归档。

zip archives.2012.zip /home/logs/ -x@exclude.lst

我该如何克服呢?还有没有其他方法可以通过排除上述文件来归档文件。

【问题讨论】:

    标签: file zip


    【解决方案1】:

    我没有创建 exclude.lst 文件,而是将所有排除文件分配给一个变量,并将它们传递给 zip 中的 -x 选项。

    例如

    do_not_archive=/home/logs/apache/access.log /home/logs/tomcat/catalina.out
    

    然后如下图使用zip

    zip archives.2012.zip /home/logs/ -x $do_not_archive
    

    【讨论】:

    • 这不是OP的问题,他想知道如何使用列表排除
    【解决方案2】:

    老问题,从那时起 zip 可能已经改变,但来自手册页:

    $man zip | grep -a2 exclude
    

    也可以:
    zip -r foo foo -x@exclude.lst
    这将在 foo.zip 中包含 foo 的内容,同时排除与文件 exclude.lst 中的模式匹配的所有文件。

    我已经确认这在 Ubuntu 18 中有效,每个模式/文件名都在单独的行上(没有尝试用空格分隔)

    【讨论】:

    • 如何排除包含特定字符串或模式字符串的文件?例如,我想排除所有包含_ai1ec_parsed_css.css 或以_ai1ec_parsed_css.css 结尾的文件。
    • @Pathros 您使用正则表达式来表示要排除的模式。将模式写入文件(每行一个模式)并使用“-x@{filename}”选项
    【解决方案3】:

    对于您有一堆文件要排除的情况(在我的情况下文件大于 10MB),CBR 的答案只是一个简短的补充:

    do_not_archive=$(find relative/path/to/directory -type f -size +10000000c)
    zip -r backup_without_files_bigger_than_10mb.zip relative/path/to/directory -x $do_not_archive
    

    【讨论】:

      【解决方案4】:

      可以分两步完成:

      zip archive.zip -r -@ < include.lst
      zip archive.zip -d -@ < exclude.lst
      

      【讨论】:

      • 如何排除包含特定字符串或模式字符串的文件?例如,我想排除所有包含_ai1ec_parsed_css.css 或以_ai1ec_parsed_css.css 结尾的文件。
      猜你喜欢
      • 2023-01-10
      • 2012-03-20
      • 1970-01-01
      • 2020-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-18
      相关资源
      最近更新 更多