Link to home
Start Free TrialLog in
Avatar of hpasystems
hpasystems

asked on

using regular expression with slocate

Hello peeps,

I want to locate files matching a regular expression and can't get it working.

Example.....

locate files matching....

AAaabb11.txt
AAaacc11.txt
AAbbaa44.txt

by using: locate -r AA*.txt - or - locate --regexp=AA*.txt

I also tried locate --regexp=* AA*.txt

I have tried quoting and bracing but I'm not sure of what is required. man page is sparse.

Is locate able to match on specific expressions as per my example?

please help, I am not familiar with regular expressions other than what is already in place (learning by examples)

Cheers,
Avatar of ddrudik
ddrudik
Flag of United States of America image

If you are looking for a regex pattern:
AA.*\.txt
And, more specifically, if your app supports it:
^AA.*\.txt$
Try

AA??????.txt
Avatar of ghostdog74
ghostdog74

if you only want to find files starting with AA....

find /path -type f -name "AA*txt" -print
Avatar of hpasystems

ASKER

Hi, yes, I can use find, but want to use locate - or at least be familiar with using locate with a regular expression.

ddrudik,
So the locate command should look like....
locate --regexp=* AAA.*\.txt ??? This did not work.

Omarfarid,
Using AA??????.txt is no good, the variable can be different lengths.

Can anyone supply the exact syntax of using a regular expression for locate???

Thanks all for assist, please continue!  :)
So, what about AA*.txt?

Do you have any particular criteria for the file name? What you have shown is of a fixed length.

yeah sorry, just realised my examples are misleading.

it needs to be like you say....
AA*.txt

...and how the command should actually look. I cannot get it to work. returns nothing.

locate -r AA*.txt  <- obviously, I am missing something.

ASKER CERTIFIED SOLUTION
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
From the man page it would seem you should skip regex altogether:
locate AA*.txt
Hi guys,

thanks for assist. My man pages are not very helpful, very baron. Also, the test machine isn't working but server I want it running on is and works with/without -r as suggested above. Only dirfference in versions...

did not work for...
Secure Locate 2.6 - Released May 21, 2001
on RHEL 2.1

did work for...
Secure Locate 2.7 - Released January 24, 2003
on RHEL4AS

Thanks guys, much appreciated.
Great response time.
Very helpful.