简体   繁体   English

Shell脚本查找命令,用于将文件名与一个或另一个单词匹配

[英]Shell script find command for matching file names with one or the other word

I am looking for a command for bash shell script to find the list of files in a directory whose names start with either WordA or WordB and end with digits. 我正在寻找bash shell脚本的命令,以在名称以WordA或WordB开头并以数字结尾的目录中查找文件列表。

I have this command with WordA and duplicating the same code with WordB . 我有这个命令WordA和重复使用相同的代码WordB

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

I tried putting Or condition in various formats such as (WordA|WordB) , [WordA|WordB] , [(WordA)|(WordB)] , [(WordA)||(WordB)] to match WordA or WordB followed by digits. 我尝试以各种格式放置Or条件,例如(WordA|WordB)[WordA|WordB][(WordA)|(WordB)][(WordA)||(WordB)]以匹配WordAWordB后跟数字。

None of them worked. 他们都没有工作。

You need to use the -regextype supported in find command, in the case used the posix-extended type and do 在使用posix-extended类型的情况下,您需要使用find命令支持的-regextype并执行

-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.

So your command should be 所以你的命令应该是

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

In the find man page, under header Expressions, you'll find (no pun intended) the following 在查找手册页的标题表达式下,您将找到(无双关)

Operators 运营商

Operators join together the other items within the expression. 运算符将表达式中的其他项组合在一起。 They include for example -o (meaning logical OR) and -a (meaning logical AND). 例如,它们包括-o(表示逻辑OR)和-a(表示逻辑AND)。 Where an operator is missing, -a is assumed. 如果缺少运算符,则假定-a。

Thus the answer to your problem appears to be 因此,您的问题的答案似乎是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM