locate behavior with bash wildcards

2019-06-11 08:22发布

I have

$ ls -l re*trict
ls: cannot access re*trict: No such file or directory

Why does this give 0 matches?

$ locate -c 're*trict'
0
$ locate -c re*trict
0
$ locate -c re?trict
0

This works though and gives 8351 matches:

$ locate -c restrict
8351

标签: linux bash shell
2条回答
地球回转人心会变
2楼-- · 2019-06-11 09:01

locate matches against the full path name. To find re*trict as a substring, you have to add * before and after it:

locate '*re*trict*'
查看更多
唯我独甜
3楼-- · 2019-06-11 09:20

I think the answer is available on the man page:

If any PATTERN contains no glob‐bing characters, locate behaves as if the pattern were *PATTERN*.

Since my PATTERN contains a globbing character it is not expanded to *PATTERN* and so a literal match is tried to be found. Obviously there is no file with name (substituting s) 'restrict' since even under root the name of such file would be /re?trict and the match would fail because of a leading /

查看更多
登录 后发表回答