find and -regex

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?
 
Not entirely sure as I never use find regex's, though ls will output only the filename while find will produce the full path.
 
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.
 
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?
 
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..$'
 
Back
Top