hpekristiansen
hpekristiansen

Reputation: 1070

bash find command refuses to find more than one file with wildcard

My find is not working the way I expected. When there is more than one file it halts with error.

hpek@melda:~/temp/test$ ll
total 16
-rw-r--r--  1 hpek  staff    70B Mar  2 15:16 f1.tex
-rw-r--r--  1 hpek  staff    70B Mar  2 15:17 f2.tex
hpek@melda:~/temp/test$ find . -name *.tex 
find: f2.tex: unknown option
hpek@melda:

and if I remove one of the files, then it works:

hpek@melda:~/temp/test$ rm f1.tex 
hpek@melda:~/temp/test$ find . -name *.tex 
./f2.tex
hpek@melda:~/temp/test$ 

It does not matter what file I remove. As soon as the wildcard gives more than one file, then find halts.

Upvotes: 2

Views: 3329

Answers (4)

Eduardo Ivanec
Eduardo Ivanec

Reputation: 11862

You have to quote the wildcards so bash doesn't expand them:

find . -name '*.tex'

Right now * is being interpreted by bash. As a result, this is the actual command being executed:

find . -name f1.tex f2.text

Upvotes: 4

Dominik Honnef
Dominik Honnef

Reputation: 18430

You want find . -name "*.tex" instead – Note the quotation marks around the glob. What is happening here is that in your case, your shell is expanding the glob and then passing the result of that to find, which results in find . -name f1.tex f2.tex – Which is not a valid way of using find.

By putting the argument in quotation marks, it gets passed to find as is.

Upvotes: 2

rahmu
rahmu

Reputation: 5878

Your wildcard * is being expanded by the shell before reaching the find command. In other words, here's the command executed by find:

find . -name f1.tex f2.tex

Note that if you execute the command from a different directory, you will get different results since the wildcard will be expanded differently.

In order to get the desired result, try escaping it like this:

find . -name \*.tex 

Upvotes: 3

aioobe
aioobe

Reputation: 421220

*.tex is expanded by bash before sent as argument to the command.

find . -name *.tex

is in your case equivalent to

find . -name f1.tex f2.tex

Solution: Put "..." around arguments with wildcards to avoid the shell expansion:

find . -name "*.tex"

This will work as expected:

$ find . -name "*.tex"
./f1.tex
./f2.tex

Upvotes: 6

Related Questions