Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have been trying to create a find command string that will find all files that end with a number 1-99 but exclude all others.

e.g. I want to find myfile1 myfile99 but not myfile456 and not myfilebackup

The regex I'm thinking of is myfile[1-9]{1,2} but I can't get this to work with find.

find . -regex '.*myfile[0-9]{1,2}' OR find . -iname 'myfile[0-9]{1,2}'

From what I can see it's the {1,2} part that is not working.

(by the way can you use -name -regex interchangably?)

Any help appreciated.

share|improve this question
    
-iname (and -name) use globs, which do not have this syntax. –  l0b0 Mar 7 '13 at 13:51
add comment

2 Answers

up vote 2 down vote accepted

You could try

find . -regex '.*myfile[0-9][0-9]?'

or

find . -\( -name "myfile[0-9][0-9]" -o -name "myfile[0-9]" \)
share|improve this answer
    
Thanks - the first line is working for me. I had tried this with -iname but I don't think it supports the syntax or else I'm doing something wrong. –  Lukas88 Mar 7 '13 at 14:16
add comment

If you have GNU find, you can use another regular expression type:

find . -regextype sed -regex '.*myfile[0-9]\{1,2\}'

According to GNU find uses a neutered Emacs regular expression syntax by default - Emacs supports \{from,to\} syntax, but at least GNU find doesn't support it.

Strangely, the reference manual doesn't include a section on the sed regular expression syntax, so who knows which parts of it are supported.

share|improve this answer
    
Cheers for this tip. Very useful to know. –  Lukas88 Mar 7 '13 at 14:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.