The Computer Oracle

FreeBSD 9: How to locate an exact filename?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Drifting Through My Dreams

--

Chapters
00:00 Freebsd 9: How To Locate An Exact Filename?
00:28 Answer 1 Score 6
00:40 Accepted Answer Score 10
01:23 Answer 3 Score 0
01:43 Thank you

--

Full question
https://superuser.com/questions/480309/f...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#unix #freebsd #locate

#avk47



ACCEPTED ANSWER

Score 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$



ANSWER 2

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




ANSWER 3

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