Search code examples
bashglob

Does * match hyphens in a glob in bash?


Today I was looking for a file named cfg-local.properties in a directory and its subdirectories.

I used:

find . -iname *.properties

Output:

./gradle.properties

Then, I tried:

find . -iname *-*.properties

Output:

./cfg/src/test/resources/cfg-local.properties
./cfg/src/main/resources/cfg-local.properties
./cfg/build/classes/test/cfg-local.properties
./cfg/build/resources/test/cfg-local.properties
./cfg/build/resources/main/cfg-local.properties

Shouldn't the asterisk match any character, including hyphens, in a glob expression?


Solution

  • Yes it will match, however you are actually running the command:

    find . -iname gradle.properties
    

    Insted, run:

    find . -iname '*.properties'
    

    When you type and run find . -iname *.properties , the *.properties part will be expanded by the shell to match files. (a process called globbing).

    In your second example, find . -iname *-*.properties ,the *-*.properties doesn't match any file, so no filename expansion is performed and *-*.properties is passed verbatim to the find command.

    Enclosing a word inside single quotes, like '*.properties' will prevent the shell from performing globbing.