21

Using find with grep, one can locate files that match a pattern:

# find | grep error
./solr-modifiedSolr4/SolrPhpClient/phpdocs/errors.html
./error_log
./includes/classes/error_log

However, using find alone the first file is not found:

# find . -name error*
./error_log
./includes/classes/error_log

Why doesn't find locate the errors.html file when not used with grep? How is find used to show this file as well?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
dotancohen
  • 15,494
  • 26
  • 80
  • 116

1 Answers1

42

You need to quote your argument error* because the shell expands it. So what you're actually running now is find -name error_log, because that's what the shell can expand it to (there's a file named error_log in your current directory).

find . -name 'error*'

Is the correct invocation for your use case.

phunehehe
  • 20,030
  • 27
  • 99
  • 151
Dennis Kaarsemaker
  • 8,420
  • 3
  • 29
  • 31
  • 12
    an altenative writing is `find -name error\*` - one key less to press ;) this has the same effect, the `*` gets passed as an literate asterisk to the find command and is not expanded by your shell – zhenech Nov 11 '12 at 11:14
  • 3
    When having trouble with the shell (how it interprets your command-line and passes all arguments and parameters to the actual command), re-run the command prepending it with the `echo` command. So, if you'd run `echo find . -name error*` it would have outputted `find . -name error_log` – Carlos Campderrós Nov 12 '12 at 10:01