2017-07-27 128 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:]]' 
相關問題