bash 使用模式定位命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17262827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:45:29  来源:igfitidea点击:

bash locate command with pattern

bashlocate

提问by Tebe

I try to find a file with locatecommand.

我尝试使用locate命令查找文件。

It behaves itself some strange with patterns, at least not like lsor findcommands.

它对模式表现得有些奇怪,至少不是喜欢lsfind命令。

I do the following:

我执行以下操作:

sh@sh:~$ locate rhythmdb
/home/sh/.local/share/rhythmbox/rhythmdb.xml
sh@sh:~$ locate "rhyth*"
sh@sh:~$ locate 'rhyth*'
sh@sh:~$ locate rhyth*

(Screenshot)

截图

In my humble opinion it should find when asterisk is used too, but it doesn't.

在我的拙见中,它也应该找到何时使用星号,但事实并非如此。

What can be wrong?

有什么问题?

回答by gniourf_gniourf

From man locate:

来自man locate

If  --regex is not specified, PATTERNs can contain globbing characters. If any
PATTERN contains no globbing characters, locate  behaves  as  if the pattern
were \*PATTERN*.

Hence, when you issue

因此,当您发出

locate rhyth*

locatewill not find it, because there are no files that match this pattern: since there's a glob character, locate will really try to match (in regex): ^rhyth.*and there are obviously no such matches (on full paths).

locate不会找到它,因为没有与此模式匹配的文件:因为有一个 glob 字符, locate 将真正尝试匹配(在正则表达式中):^rhyth.*并且显然没有这样的匹配(在完整路径上)。

In your case, you could try:

在你的情况下,你可以尝试:

locate "/home/sh/.local/share/rhythmbox/rhyth*"

or

或者

locate '/rhyth' # equivalent to locate '*/rhyth*'

But that's not very good, is it?

但这不是很好,是吗?

Now, look at the first option in man locate:

现在,看看第一个选项man locate

   -b, --basename
          Match  only  the base name against the specified patterns.  This
          is the opposite of --wholename.

Hurray! the line:

欢呼!该行:

locate -b "rhyth*"

should work as you want it to: locate a file with basenamematching (in regex): ^rhyth.*

应该按照您的意愿工作:找到具有基本名称匹配的文件(在正则表达式中):^rhyth.*

Hope this helps.

希望这可以帮助。

Edit.To answer your comment: if you want to locateall jpgfiles in folder /home/sh/music/, this should do:

编辑。回答您的评论:如果您想要文件夹中的locate所有jpg文件/home/sh/music/,则应该这样做:

locate '/home/sh/music/*.jpg'

(no -bhere, it wouldn't work). Notice that this will show all jpgfiles that are in folder /home/sh/musicand alsoin its subfolders. You could be tempted to use the -iflag (ignore case) so that you'll alsofind those that have the uppercase JPGextension:

(不在-b这里,它不起作用)。请注意,这会显示所有JPG是在文件夹中的文件/home/sh/music,并在其子文件夹。您可能会想使用-i标志(忽略大小写),以便您还可以找到那些具有大写JPG扩展名的标志:

locate -i '/home/sh/music/*.jpg'

Edit 2.Better to say it somewhere: the locatecommand works with a database — that's why it can be much faster than find. If you have recent files, they won't be located and if you delete some files, they might still be located. If you're in this case (which might be the purpose of your other comment), you must update locate's database: as root, issue:

编辑 2.最好在某处说出来:该locate命令适用于数据库 - 这就是为什么它可以比find. 如果你有最近的文件,它们不会是located,如果你删除了一些文件,它们可能仍然是located。如果您在这种情况下(这可能是您的其他评论的目的),您必须更新locate的数据库:作为 root,发出:

updatedb

Warning.The command updatedbmight take a few minutes to complete, don't worry.

警告。该命令updatedb可能需要几分钟才能完成,别担心。