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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You could try
find . -regex '.*myfile[0-9][0-9]?'
or
find . ( -name "myfile[0-9][0-9]" -o -name "myfile[0-9]" )
Method 2
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0