find . -name *.rb

doesn't work on my ubuntu

I have to do

find . -name "*.rb"

to get it to work.

Why is that?

  • I cannot say why, but it is the same on Solaris UNIX (when the * is used). I have had to do it that way for many years. – Tim Mar 13 '12 at 21:48
  • might consult man find – Ringtail Mar 13 '12 at 22:19

If you have a file in the current directory ending with .rb, it'll be expanded by the shell. So, if you have one file named "foo.rb", the command that gets executed is find . -name foo.rb. ("find a file named foo.rb")

It gets even worse if you've multiple files in the current directory (say, "foo.rb" and "bar.rb"). Then the command becomes find . -name foo.rb bar.rb, which will cause an argument error for find.

To prevent the shell from expaning the glob pattern *.rb, you must either quote it (either single or double quotes will do) or escape the asterisk. The below commands have equivalent behavior:

find . -name "*.rb"
find . -name '*.rb'
find . -name \*.rb

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.