Bash – regex matching with “locate”

bashlocateregular expression

I'm trying to use the locate command to find files in my home folder, however whenever I try and run this I get no results:

locate -i -l 4 --regexp '^\/home\/jack\/[A-Za-z0-9\/\ ]*(My.)*$'

I've also tried ^\/home\/jack\/^(?!\.)[A-Za-z0-9\/\ ]*(My.)*$ but that returns no results either.

The file I'm looking for just to test it is /home/jack/Music/Foals/My Number.flac

I would like to exclude the results of hidden files from my search.

Best Answer

This seems to do the job.

locate -ir '^/home/jack/\([^.][^/]\+/\)\+My[^/]*$'

Quotes from manual:

-i, --ignore-case Ignore case distinctions when matching patterns.

-r, --regexp REGEXP Search for a basic regexp REGEXP. No PATTERNs are allowed if this option is used, but this option can be specified multiple times.

Related Question