`locate` wildcard strange behavior - why?

locate "*.png" returns all files ending with .png, as expected.

locate "test.*" doesn't return anything, but there are files named test in my system.

locate "*test" returns all files ending with test, as expected.

locate "test*" doesn't return anything, but there are files starting with test in my system.

Why do wildcards seem to work only for "ending with"?


Solution 1:

locate "test.*" doesn't return anything, but there are files named test in my system.

. is treated as dot, not as in regex's as an arbitrary character, so test.* does not match test, but test.foo.

locate "test*" doesn't return anything, but there are files starting with test in my system.

locate stores the full path to the file, so to find files starting with test, you should use locate "*/test*".

The last point might be confusing, as locate foo finds anything including foo, so the pattern gets interpreted as *foo*. It seems that the pattern is not enclosed in stars, if there is already one wildcard in the pattern.

Disclaimer: I did some test and these are my conclusions, I cannot prove them by citing the man page, which seems very rudimentary.