FreeBSD 9: How to locate an exact filename?

10

Trying to use locate command to find an exact match for the given pattern. However it results showing all matching files..

For example: I want to find a binary named: node

But it gives me all matches containing this word:

server2# locate node
/usr/share/man/man9/getnewvnode.9.gz
/usr/share/man/man9/ieee80211_amrr_node_init.9.gz
/usr/share/man/man9/ieee80211_dump_node.9.gz
/usr/share/man/man9/ieee80211_dump_nodes.9.gz
/usr/share/man/man9/ieee80211_find_rxnode.9.gz
/usr/share/man/man9/ieee80211_find_rxnode_withkey.9.gz
/usr/share/man/man9/ieee80211_free_node.9.gz

Alex G

Posted 2012-09-27T13:24:18.650

Reputation: 909

Answers

10

If you look at locate --help, you may find:

  -r, --regexp REGEXP    search for basic regexp REGEXP instead of patterns
      --regex            patterns are extended regexps

You can use -r to provide a regexp pattern to locate:

locate -r /node$

The / ensures node is at the start of the file name. The $ ensures node is at the end of the file name. This will give you only the files matching the exact file name.

If you want to do a case-insensitive search (matches Node, NODE, nOdE, etc), add -i:

locate -i -r /node$

If locate does not support regexp, you can use grep (as mentioned by Iracicot):

locate node | grep /node$
locate -i node | grep -i /node$

ADTC

Posted 2012-09-27T13:24:18.650

Reputation: 2 649

Alternatively, you can use the -b switch to only match against the basename: locate -br node$ – Sarke – 2017-09-29T22:22:10.160

6

You may use grep with locate

server2# locate node | grep node$

The $ sign will tell grep to look at the end of the string.

lracicot

Posted 2012-09-27T13:24:18.650

Reputation: 173

1Result is the same. It brings up different matches ending with node... /usr/ports/www/p5-WebService-Linode. I guess it should be /node$ ? – Alex G – 2012-09-27T13:40:33.347

Yes you can try this too (But I'm not sure if the / character must be escaped or not). Have you tried locate -b ? – lracicot – 2012-09-27T13:43:05.093

0

Disable locate's implicit glob by adding your own glob that matches all directories:

locate */node

From the man page:

If any PATTERN contains no globbing characters, locate behaves as if the pattern were *PATTERN*

This syntax will match a complete file or directory name anywhere, including in the root.

Roger Dahl

Posted 2012-09-27T13:24:18.650

Reputation: 926