Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Click here to return to the 'Combine locate with grep' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Combine locate with grep
Authored by: thinkyhead on Dec 04, '01 11:14:30PM
The real magic of locate happens when you pipe the output to grep. You may have noticed that locate matches anything in the full path of the item you're searching for, so you often get too many results. grep makes it easier to whittle down those large sets to find what you really meant to find.

For example, say you want to find a file named "thing" but you don't want all the items that match "thing" in their path. This will limit the results to items that don't have any more slashes after "thing" (case-insensitive, of course):

locate thing | grep -iE thing[^/]*$

I use a script named "ffind" to do this for anything I want to locate. Here it is:

locate $1 | grep -iE /$1[^/]*\$

There are other variations on the same theme. For example if you want an exact match of the filename (still case-insensitive) use this:

locate thing | grep -iE /thing$

[ Reply to This | # ]