5

I've got a set of files in a directory, all of the format test[0-9][0-9].txt. If I run

find . -regex ".*/(test)[0-9][0-9]\.txt"

then all the files are shown, but if I run

find . -regex ".*/(test)[0-9]{2}\.txt"

then none are shown. What am I doing wrong?!

I've had a search through previous similar questions to this, but can't find a particularly relevant answer

2
  • 1
    The first example uses normal regexp syntax, where the second uses extended syntax. Your version of find obviously expects the former by default. You don't say what system you're on - your find might have a switch to turn on extended regexp parsing. On FreeBSD, for example, it's -E Commented Nov 23, 2015 at 12:27
  • @D_Bye ah, that's a good point. CentOS6. I've tried -regextype posix-extended and that doesn't work either Commented Nov 23, 2015 at 12:33

1 Answer 1

7

If you are using GNU find, this should work for you

 find . -regextype sed -regex "./test[0-9]\{2\}.txt"

Explanation

  • -regextype sed - use basic posix regular expression (just because thats what Im familiar with)
  • ./ - necessary because find considers all file paths on a relative search to begin with this pattern
  • [0-9]\{2\} - 2 instances of the [0-9] digit character class with the quantifier \{...\} brackets escaped - per basic posix regular expressions

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.