【问题标题】:How to exclude files from list如何从列表中排除文件
【发布时间】:2021-01-11 12:34:58
【问题描述】:

我在目录中有大约 100k 个文件。我需要删除其中一些文件,不包括(15k 不同模式)模式列表:

Directory: /20210111/
Example files: 
/20210111/xxx_yyy_zzz.zip
/20210111/aaa_bbb_ccc.zip
/20210111/ddd_eee_fff.zip
...

Exclude.list 
ddd
aaa
...

我尝试了查找:

find /20210111/ -type f -iname "*.zip" ! -iname "*$(cat Exclude.list)*" -exec ...

出现错误:参数太长。因为 exclude.list 有很多行。

我该怎么做?

【问题讨论】:

标签: bash find


【解决方案1】:

您可以使用grep 过滤find 的输出,然后使用xargs 处理结果列表。

find /20210111/ -type f -iname '*.zip' -print0 \
| grep -zvFf Exclude.list - \
| xargs -0 rm
  • -print0-z-0 用于通过空字节分隔文件名,因此文件名可以包含任何有效字符(您不能在 Exclude.list 中存储包含文字换行符的模式,无论如何)。
  • grep 的-F 将模式解释为固定字符串而不是正则表达式。

【讨论】:

    猜你喜欢
    • 2014-04-28
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多