bash locate command with pattern
Asked Answered
D

1

6

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?

Disembody answered 23/6, 2013 at 16:27 Comment(0)
C
12

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 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:

updatedb

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

Contraoctave answered 23/6, 2013 at 16:47 Comment(4)
I hate to set one more silly question, but is it possible to specify certain location? e.g. I tried to do: locate -b "/home/sh/music/*.jpg", but it doesn't seem to be workingDisembody
@gekannt You don't need the -b flag in this case.Contraoctave
ok, I tried it doesn't work, if I do find . *.jpg, I get many files, if I do locate - nothing. dl.dropboxusercontent.com/u/18776391/storage/… dl.dropboxusercontent.com/u/18776391/storage/… dl.dropboxusercontent.com/u/18776391/storage/…Disembody
If these are recent files, you might need to update locate's database: issue as root the command updatedb (it might take a few minutes to perform, don't worry).Contraoctave

© 2022 - 2024 — McMap. All rights reserved.