I'm having trouble using the regex of the find
command. Probably something I don't understand about escaping on the command line.
Why are these not the same?
find -regex '.*[1234567890]'
find -regex '.*[[:digit:]]'
Bash, Ubuntu
To use regular expressions, open either the Find pane or the Replace pane and select the Use check box. When you next click Find Next, the search string is evaluated as a regular expression. When a regular expression contains characters, it usually means that the text being searched must match those characters.
Regexps are acronyms for regular expressions. Regular expressions are special characters or sets of characters that help us to search for data and match the complex pattern. Regexps are most commonly used with the Linux commands:- grep, sed, tr, vi.
You should have a look on the -regextype
argument of find
, see manpage:
-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.
I guess the emacs
type doesn't support the [[:digit:]]
construct. I tried it with posix-extended
and it worked as expected:
find -regextype posix-extended -regex '.*[1234567890]'
find -regextype posix-extended -regex '.*[[:digit:]]'
Regular expressions with character classes (e.g. [[:digit:]]
) are not supported in the default regular expression syntax used by find
. You need to specify a different regex type such as posix-extended
in order to use them.
Take a look at GNU Find's Regular Expression documentation which shows you all the regex types and what they support.
Note that -regex
depends on whole path.
-regex pattern
File name matches regular expression pattern.
This is a match on the whole path, not a search.
You don't actually have to use -regex
for what you are doing.
find . -iname "*[0-9]"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With