Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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