locate 命令 - 仅查找整个单词匹配的位置

locate command - find only where whole word matches

在终端 window 中,我如何使用命令 locate 仅 return 出现整个单词的结果。

例如,我正在查找与 GO lang 相关的所有文件。但是,输入 locate go return 是从实际 GO 文件到 algorithm.php 之类的所有内容。

有没有办法让这个命令 return 只得到整个单词匹配的结果?

如果您正在查找 .go 文件,您可以使用 locate "*.go" 来查找具有该扩展名的文件,而不是在完整文件名中搜索单词 'go'。

作为 mlocate(广泛使用的实现)一部分的 locate 命令接受正则表达式,因此您可以

locate -r '\<go\>'

它的文档还说它有一个 -w 选项,就像 word-matching 的 GNU grep 选项一样,但功能不同。但是你可以 使用 -w 选项(单词)将 locate 的结果通过 grep 管道化,例如

locate go |grep -w go

grep 也接受正则表达式。 -w 选项似乎不是标准选项(参见 POSIX), however it also is widely implemented, e.g., on Solaris.

locate 没有 POSIX 文档;有些实现的功能明显不如其他实现。