I try to find a file with locate
command.
It behaves itself some strange with patterns, at least not like ls
or find
commands.
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*
In my humble opinion it should find when asterisk is used too, but it doesn't.
What can be wrong?
From 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*
locate
will 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).
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
:
-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 basename matching (in regex): ^rhyth.*
Hope this helps.
Edit. To answer your comment: if you want to locate
all jpg files in folder /home/sh/music/
, this should do:
locate '/home/sh/music/*.jpg'
(no -b
here, it wouldn't work). Notice that this will show all jpg files that are in folder /home/sh/music
and also in its subfolders. You could be tempted to use the -i
flag (ignore case) so that you'll also find those that have the uppercase JPG extension:
locate -i '/home/sh/music/*.jpg'
Edit 2. Better to say it somewhere: the locate
command works with a database — that's why it can be much faster than find
. If you have recent files, they won't be locate
d and if you delete some files, they might still be locate
d. If you're in this case (which might be the purpose of your other comment), you must update locate
's database: as root, issue:
updatedb
Warning. The command updatedb
might take a few minutes to complete, don't worry.