suppose I have two files: ac
and abc
. I want to find a regex to match both files. Normally I would expect the following regex to work, but it never does:
find ./ -name ab?c
I have tried escaping or not the questionmark, this never seems to work. Normally in the regex documentations I have found; ?
means: previous character repeated 0 or 1 times, but find
doesn’t seem to understand this.
I have tried this on two different find versions: GNU find version 4.2.31 and find (GNU findutils) 4.6.0
PS: this works with *
, but I specifically would like to match just one optional character.
find ./ -name a*c
gives
./ac ./abc
Advertisement
Answer
The expression passed to -name
is not a regex, it is a glob expression. A (single) glob expression can’t be used for your use case but you can use regular expressions using -regex
:
find -regex '.*/ab?c'
Btw, the default regular expression language is Emacs Style as explained here : https://www.emacswiki.org/emacs/RegularExpression . You can change the regex language using -regextype
.