linux - find -regex not recognizing \d digit

07
2014-07
  • Madeleine P. Vincent

    I have a directory structure that looks like the following:

    processor0/10
    processor0/10.1
    processor0/10.2
    processor1/10
    processor1/10.1
    processor1/10.2
     ...etc...
    processor7/10
    processor7/10.1
    processor7/10.2
    

    I'd like to "find" all the directories that are 10.1 or 10.2.

    The following works:

    $ find . -type d -regextype posix-egrep -regex '\./processor[0-9]*/10\.(1|2)'
    

    but this doesn't:

    $ find . -type d -regextype posix-egrep -regex '\./processor\d*/10\.(1|2)'
    

    I'm not sure why, since egrep should understand that \d is a digit. Can anyone explain this?

    This is command-line find on 64-bit Ubuntu - specifically (GNU findutils) 4.4.2

    Regards, Madeleine

  • Answers
  • devnull

    This is because \d denotes a decimal digit character in Perl Compatible Regular Expressions which is not supported by find.

    You could make use of the character classe [:digit:]:

    find . -type d -regextype posix-egrep -regex '\./processor[[:digit:]]*/10\.(1|2)'
    

    You may also want to refer to regular expressions.


  • Related Question

    regex - using find to locate filenames before a certain date
  • Questioner

    I have a ton of files that are timestamped directories. These all look like

    2011-06-24_13.53.36  // a directory name for june 24th, 1:53:36 pm
    

    I have thousands of these directories. I want to do operations on some of the older ones. Let's say I give it a string for date time that matches that exact format, like i'll give it

    2011-06-25_00.00.00 // june 25th, 12am
    

    I want to find all the directories BEFORE my time. So if i give the string for 12am on june 25th, i want to find all the directories before then.

    Can this be done using find?

    If not i can find EVERY directory i have like this and then filter after wards. The created/modified dates are not tied to the actual timestamp im looking for (that would make this easier)


  • Related Answers
  • Qtax

    This format is string sortable, so you could just do a string compare on the strings of the same format.

    I don't know if/how find can do this, but here is a possible Perl example (if all folders are in the same location and have the same format):

    perl -E "-d && $_ lt '2011-06-25_00.00.00' && say while <*>"
    

    Output:

    2011-05-28_00.00.00
    

    (Which is correct for my little test.)

  • ring bearer

    One quirky way :

    • Assuming you only have these directories under working directory
    • You are in bash or similar

    Following command should give you list of files "alphabetically older" than Jun 25.

    ls -l | head -`ls -l | grep -n 2011-06-24 | cut -d: -f1`
    

    Now note that the name pattern criteria is 2011_06_24 (to avoid 2011_06_25 in the result)