如何从列表中排除文件
How to exclude files from list
我的目录中有大约 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:
find /20210111/ -type f -iname "*.zip" ! -iname "*$(cat Exclude.list)*" -exec ...
出现错误:参数太长。因为exclude.list有很多行。
我该怎么做?
您可以使用grep
过滤find
的输出,然后使用xargs
处理结果列表。
find /20210111/ -type f -iname '*.zip' -print0 \
| grep -zvFf Exclude.list - \
| xargs -0 rm
-
-print0
、-z
和 -0
用于通过空字节分隔文件名,因此文件名可以包含任何有效字符(您不能存储包含无论如何,Exclude.list 中的文字换行符)。
- grep 的
-F
将模式解释为固定字符串而不是正则表达式。
我的目录中有大约 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:
find /20210111/ -type f -iname "*.zip" ! -iname "*$(cat Exclude.list)*" -exec ...
出现错误:参数太长。因为exclude.list有很多行。
我该怎么做?
您可以使用grep
过滤find
的输出,然后使用xargs
处理结果列表。
find /20210111/ -type f -iname '*.zip' -print0 \
| grep -zvFf Exclude.list - \
| xargs -0 rm
-
-print0
、-z
和-0
用于通过空字节分隔文件名,因此文件名可以包含任何有效字符(您不能存储包含无论如何,Exclude.list 中的文字换行符)。 - grep 的
-F
将模式解释为固定字符串而不是正则表达式。