Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux find files with optional character in their name

Tags:

regex

linux

find

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
like image 264
Chris Maes Avatar asked Jul 02 '18 12:07

Chris Maes


1 Answers

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.

like image 71
hek2mgl Avatar answered Sep 30 '22 20:09

hek2mgl