What is the difference between \*.xml and *.xml in find command in Linux/mac

The results of:

find . -name \*.xml and find . -name *.xml are different. But why?

Also, is locate '*.xml' better than find? Which one is the most commonly used?


By escaping the asterisk, you're giving find the literal string *.xml to match, which it could parse as a wildcard internally regardless of shell. If you fail to escape or quote the asterisk, it will be expanded by the shell before being touched by the find command at all.

For example, if you only had one file somefile.xml in your directory, the shell would expand the wildcard and pass the literal string somefile.xml to your command. ie:

sh-4.1$ find . -name *.xml

would be exactly equivalent to:

sh-4.1$ find . -name somefile.xml

which would only match that exact filename. If you have two files in your directory which match the *.xml wildcard, you would probably get a syntax error when the wildcard's expanded:

sh-4.1$ find . -name somefile.xml someotherfile.xml
find: paths must precede expression: someotherfile.xml
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

As to whether or not locate is better, that really depends on what you're using it for. Although locate is much faster, it relies on a pre-built index so it's only as good as the most recent index update (usually done daily, but that's distro-dependent). Also, it only returns results based on filename matches, whereas find allows a plethora of options to refine your search.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.