Trouble getting regex to work with find
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}.*"
Related videos on Youtube
BryanK
Updated on September 18, 2022Comments
-
BryanK almost 2 years
I'm having a lot of trouble getting
find
to work with regex flags. Im usingegrep
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 regexfind
supports though I likeegrep
because of the-o
flag for returning only the matches, NOT the entire line. But really I just needfind
to find all files in a directory that have a match of pattern above in the file name.