4
find . -name *.rb

doesn't work on my ubuntu

I have to do

find . -name "*.rb"

to get it to work.

Why is that?

Alessio
  • 694
npiv
  • 163

1 Answers1

14

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
htoip
  • 260
Lekensteyn
  • 178,446