Bash find command with pattern

I am trying to find a file with the locate command.

It behaves strangely with patterns, at least not like the ls or find ls .

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 an asterisk is used, but it is not.

What could be wrong?

+4
source share
1 answer

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*. 

Therefore when you release

 locate rhyth* 

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

In your case, you can try:

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

or

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

But this is 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. 

Hooray! line:

 locate -b "rhyth*" 

should work the way you want: find a file with a match of the base name (in a regular expression): ^rhyth.*

Hope this helps.

Change . To answer your comment: if you want to locate all jpg files in the /home/sh/music/ folder, this should do:

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

(no -b here, this did not work). Please note that this will show all jpg files that are in the /home/sh/music folder, as well as in its subfolders. You may be tempted to use the -i flag (ignore case), so you will also find those that have an uppercase extension JPG:

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

Change 2 .. Better to say it somewhere: the locate command works with the database - why it can be much faster than find . If you have the latest files, they will not locate d, and if you delete some files, they may be locate d. If you are in this case (this may be the goal of your other comment), you should update the locate : database as root, issue:

 updatedb 

A warning. The updatedb command may take a few minutes, don't worry.

+8
source

Source: https://habr.com/ru/post/1487742/


All Articles