find and -regex

General questions about the FreeBSD operating system. Ask here if your question does not fit elsewhere.

find and -regex

Postby Business_Woman » 19 Sep 2011, 19:07

Hi,

This question is purely academic.

A simple
Code: Select all
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: Select all
 find -E /bin -regex '^..$'


Why?
The American lifestyle is not negotiable.
User avatar
Business_Woman
Member
 
Posts: 142
Joined: 19 Nov 2008, 19:25

Postby respite » 19 Sep 2011, 20:15

Not entirely sure as I never use find regex's, though ls will output only the filename while find will produce the full path.
User avatar
respite
Junior Member
 
Posts: 33
Joined: 21 Nov 2009, 21:12
Location: Los Angeles

Postby kpa » 19 Sep 2011, 20:16

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

Code: Select all
 find -E /bin -regex '.*/..$'
kpa
MFC'd
 
Posts: 3399
Joined: 05 Jul 2010, 13:19
Location: People's Technocratic Republic of Finland

Postby Business_Woman » 19 Sep 2011, 20:20

Thank you.
The American lifestyle is not negotiable.
User avatar
Business_Woman
Member
 
Posts: 142
Joined: 19 Nov 2008, 19:25

Postby wblock@ » 19 Sep 2011, 20:24

[man=1]find[/man]:
Code: Select all
    -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: Select all
% find -E /bin -print
/bin
/bin/cat
/bin/chflags
/bin/chmod
/bin/cp
/bin/chio
...


Aha. find returns the whole path. My use of [man=1]find[/man] is mostly by rote; if there's a way to get it to return basenames only, then it would work.
User avatar
wblock@
Old Fart
 
Posts: 10218
Joined: 07 Sep 2009, 23:23
Location: Milky Way galaxy

Postby Business_Woman » 19 Sep 2011, 20:40

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: Select all
find -E /bin -regex '\( .*/.. -and -regex '.*\^p \)'


Hm?
The American lifestyle is not negotiable.
User avatar
Business_Woman
Member
 
Posts: 142
Joined: 19 Nov 2008, 19:25

Postby wblock@ » 20 Sep 2011, 05:03

Easier to do in a single regex:
[cmd="%"]find /bin -regex '.*/p..$'[/cmd]

But for this use, ls and grep are more appropriate, shorter and simpler.
[cmd="%"]ls /bin | grep '^p..$'[/cmd]
User avatar
wblock@
Old Fart
 
Posts: 10218
Joined: 07 Sep 2009, 23:23
Location: Milky Way galaxy


Return to General

Who is online

Users browsing this forum: doc1623, TeoEnMing, Yahoo [Bot], yayix and 0 guests