2

I'm having a lot of trouble getting find to work with regex flags. Im using egrep on its own to find a date pattern in a file, like this:

egrep '[0-9]{4}-[0-9]{2}-[0-9]{2}' 

which will return dates formatted as CCYY-MM-DD just fine. I'm trying to use this same pattern with find. I have tried:

find . -regextype egrep -regex "[0-9]{4}-[0-9]{2}-[0-9]{2}"
find . -type f -regex "[0-9]{4}-[0-9]{2}-[0-9]{2}"

Tried to backslash the curly brackets...

find . -type f -regextype egrep -regex "[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}"

All of these don't seem to return anything. I get no error messages, no output... I have even tried to redirect the ouput to a file, but nothing...

It's hard to find good resources on using find with regex. If any one could help, I would greatly appreciate it. I'm not opposed to any of the types of regex find supports though I like egrep because of the -o flag for returning only the matches, NOT the entire line. But really I just need find to find all files in a directory that have a match of pattern above in the file name.

5

Your example mixes single-quote and double quote. And find wants a pattern which matches the whole filename, so you want to begin/end the pattern with '*'.

This will find all files that have the pattern you describe:

find yourpath -name "*[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]*"

But if you want to use regextype, you need "posix-egrep", and note carefully that regex needs to match entire path,

find yourpath -regextype posix-egrep  -regex ".*[0-9]{4}-[0-9]{2}-[0-9]{2}.*"
0

First example should be:

find yourpath -name "*[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]*"
                               ^

Fourth bracket was wrong

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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