`locate` wildcard strange behavior - why?

7,515

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.

Share:
7,515

Related videos on Youtube

Vittorio Romeo
Author by

Vittorio Romeo

Updated on September 18, 2022

Comments

  • Vittorio Romeo
    Vittorio Romeo over 1 year

    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"?

  • gniourf_gniourf
    gniourf_gniourf almost 11 years
    In my man locate I have: 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*.