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*
(Screenshot)
In my humble opinion it should find when asterisk is used too, but it doesn't.
What can be wrong?
locate reads one or more databases prepared by updatedb and writes file names matching at least one of the PATTERNs to standard output, one per line. 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*".
To use locate, open a terminal and type locate followed by the file name you are looking for. In this example, I'm searching for files that contain the word 'sunny' in their name. Locate can also tell you how many times a search keyword is matched in the database.
To check whether the locate utility is installed, open up your terminal, type locate and press Enter . If the package is installed, the system will display locate: no pattern to search for specified . Otherwise, you will see something like locate command not found .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With