Trouble getting regex to work with find

5,408

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}.*"
Share:
5,408

Related videos on Youtube

BryanK
Author by

BryanK

Updated on September 18, 2022

Comments

  • BryanK
    BryanK almost 2 years

    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.