文件在查找命令中被忽略

文件在查找命令中被忽略

我的目录包含许多文件,我只想显示那些未启动 atm 的文件。

D:/elite/custom>find d:/elite/custom/test -print
d:/elite/custom/Test
d:/elite/custom/Test/001234.txt
d:/elite/custom/Test/abc123.txt
d:/elite/custom/Test/ATM001.txt
d:/elite/custom/Test/ATM002.txt
d:/elite/custom/Test/DEF111.rtf
d:/elite/custom/Test/wwww.txt
d:/elite/custom/Test/abc


D:/elite/custom>find d:/elite/custom/test -name "[!ATM]*" -print
d:/elite/custom/Test/001234.txt
d:/elite/custom/Test/DEF111.rtf
d:/elite/custom/Test/wwww.txt

为什么没有接起d:/elite/custom/Test/abc123.txt

答案1

的参数-name是所谓的图案,它看起来像 shell 的“通配符”——通配符等。语法的[!ATM]*含义与您想象的非常不同——它意味着以任何一个非 A、T 或 M 字符开头的名称。

该模式atm*(注意没有括号)匹配任何以“atm”开头的名称。有无图案否定此并匹配名称的语法不是以“atm”开头。但是,幸运的是,find 确实有一个语法来否定“!”的匹配。在“-name”参数之前(不在模式内!)否定匹配:find . ! -name 'atm*'

答案2

因为[!ATM]并不意味着你(显然)认为它意味着什么。它的意思是“任何不是A、 aT或 a 的字符M”。

它似乎find支持相当多不同的正则表达式类型,我不知道它们全部,但是对于适合您使用的匹配,我不会乐观。

您需要(在 PCRE(perl 兼容正则表达式)术语中)所谓的(零宽度)负向预测。

相关内容