2017-07-27 126 views
2

我正在寻找命令bash shell脚本来查找名称以WordA或WordB开头并以数字结尾的目录中的文件列表。Shell脚本查找与其中一个或另一个词匹配文件名的命令

我有这个命令WordA并复制相同的代码WordB

find /log/ -name 'WordA*[[:digit:]]' 

我试图把还是在条件等各种格式(WordA|WordB)[WordA|WordB][(WordA)|(WordB)][(WordA)||(WordB)]匹配WordAWordB其次是数字。

他们都没有工作。

+0

只需运行两个find命令...'找到/日志/ -name 'WordA * [[:数字:]]''然后运行'find/log/-name'WordB * [[:digit:]]''。 –

+0

为简单起见,在问题中提到了2个。实际上,我必须匹配4个字左右。考虑到维护角度,不想粘贴4次。 – bjskishore123

+0

@ bjskishore123:看看我的回答是否有帮助 – Inian

回答

2

您需要使用支持-regextypefind命令,在情况下使用的posix-extended类型和做

-regextype type 
     Changes the regular expression syntax understood by -regex and -iregex 
     tests which occur later on the command line. Currently-implemented 
     types are emacs (this is the default), posix-awk, posix-basic, 
     posix-egrep and posix-extended. 

所以,你的命令应该是

find /log/ -regextype posix-extended -regex './(WordA|WordB)([[:digit:]]+)$' 
2

在查找手册页,下header表达式,你会发现(无双关语意思)以下

运算符

运算符将表达式中的其他项目连接在一起。它们包括例如-o(意思是逻辑OR)和-a(意思是 逻辑AND)。如果操作员失踪,则假设-a。

这样的答案,您的问题似乎是

find /log/ -name 'WordA*[[:digit:]]' -o 'WordB*[[:digit:]]' 
相关问题