1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

find and -regex

Discussion in 'General' started by Business_Woman, Sep 19, 2011.

  1. Business_Woman

    Business_Woman New Member

    Messages:
    148
    Likes Received:
    0
    Hi,

    This question is purely academic.

    A simple
    Code:
    ls /bin | egrep '^..$'
    gives a nice list of all the binaries that are two characters long, mv cp ls etc..

    But for some reason this doesn't work
    Code:
     find -E /bin -regex '^..$'
    Why?
     
  2. respite

    respite New Member

    Messages:
    33
    Likes Received:
    0
    Not entirely sure as I never use find regex's, though ls will output only the filename while find will produce the full path.
     
  3. kpa

    kpa Member

    Messages:
    4,005
    Likes Received:
    9
    You're matching the whole string with -regex so you have to account for the /bin/ at the beginning.

    Code:
     find -E /bin -regex '.*/..$'
    
     
  4. Business_Woman

    Business_Woman New Member

    Messages:
    148
    Likes Received:
    0
    Thank you.
     
  5. wblock@

    wblock@ Administrator Staff Member Administrator Moderator Developer

    Messages:
    11,205
    Likes Received:
    15
    find(1):
    Code:
        -regex pattern
                 True if the [color="Red"]whole path[/color] of the file matches pattern using regular
                 expression.  To match a file named “./foo/xyzzy”, you can use the
                 regular expression “.*/[xyz]*” or “.*/foo/.*”, but not “xyzzy” or
                 “/foo/”.
    
    Let's see what the input to that regex really is:
    Code:
    % find -E /bin -print
    /bin
    /bin/cat
    /bin/chflags
    /bin/chmod
    /bin/cp
    /bin/chio
    ...
    
    Aha. find returns the whole path. My use of find(1) is mostly by rote; if there's a way to get it to return basenames only, then it would work.
     
  6. Business_Woman

    Business_Woman New Member

    Messages:
    148
    Likes Received:
    0
    yes, sorry. I should have read the man page more careful >_>

    If i want to match all binaries in /bin that are three characters long and starts with 'p'.

    Code:
    find -E /bin -regex '\( .*/.. -and -regex '.*\^p \)'
    Hm?
     
  7. wblock@

    wblock@ Administrator Staff Member Administrator Moderator Developer

    Messages:
    11,205
    Likes Received:
    15
    Easier to do in a single regex:
    % find /bin -regex '.*/p..$'

    But for this use, ls and grep are more appropriate, shorter and simpler.
    % ls /bin | grep '^p..$'