5

In a terminal window how can I use the command locate to only return results where the whole word is present.

For example, I was looking for all files related to GO lang. However, typing locate go returns everything from actual GO files to something like algorithm.php for example.

Is there a way to get this command to return only results where the whole word matches?

jzg.dev
  • 123
  • 1
  • 13

2 Answers2

4

The locate command which is part of mlocate (a widely used implementation) accepts regular expressions, so you could do

locate -r '\<go\>'

Its documentation also says it has a -w option like the GNU grep option for word-matching, but that does not function the same. You can however pipe the result from locate through grep with the -w option (words), e.g.,

locate go |grep -w go

grep also accepts regular expressions. The -w option does not appear to be standard (see POSIX), however it also is widely implemented, e.g., on Solaris.

There is no POSIX document for locate; some implementations are notably less functional than others.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
0

If you're looking for .go files, you can use locate "*.go" to find files that have that extension, instead of searching the full filename for the word 'go'.

  • Thanks for the info, I was more just curious about how to `locate` a whole word in general, not specifically finding .go files – jzg.dev Jan 28 '16 at 00:06
  • Do you mean locating a file that is named `x` but has a wildcard extension? Or do you literally mean locating full words within a filename that are not separated by anything? – jeraldjuice Jan 28 '16 at 00:17