# find and -regex



## Business_Woman (Sep 19, 2011)

Hi,

This question is purely academic.

A simple 
	
	



```
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 
	
	



```
find -E /bin -regex '^..$'
```

Why?


----------



## respite (Sep 19, 2011)

Not entirely sure as I never use find regex's, though ls will output only the filename while find will produce the full path.


----------



## kpa (Sep 19, 2011)

You're matching the whole string with -regex so you have to account for the /bin/ at the beginning.


```
find -E /bin -regex '.*/..$'
```


----------



## Business_Woman (Sep 19, 2011)

Thank you.


----------



## wblock@ (Sep 19, 2011)

find(1):

```
-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:

```
% 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.


----------



## Business_Woman (Sep 19, 2011)

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'.


```
find -E /bin -regex '\( .*/.. -and -regex '.*\^p \)'
```

Hm?


----------



## wblock@ (Sep 20, 2011)

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..$'`


----------

