1

I'm still learning the command line and I am having some trouble fully understanding the use of the wildcard within the find command.

I'm working in the directory user/temp and use the find command as followed to find all .txt files:

find . -name *.txt -print

This gives me the error:

find: unknown primary or operator.

I know you need to use single quotes around the *.txt in order to escape the expansion and get your desired result; however, I do not understand why this needs to be done.

I read that you have to escape the * so that find does the expansion internally. I am confused because I thought that using single quotes around * will give make the command line not interpret it as a special character, so how does the expansion take place?

  • I formatted the code parts for you, and added a * into the first find command, if you didn't mean that to be there obviously remove it – Eric Renouf Oct 30 '15 at 17:30

2 Answers2

2

There are 2 parts here that could try to expand the *: the shell when it's invoking find or find as it's processing/using its arguments.

If you do not single quote it, the shell will expand it, so if you have a.txt and b.txt in the current directory your command line would expand from

find . -name *.txt -print

to

find . -name a.txt b.txt -print

which isn't a valid find command

If you single quote it, you will tell the shell to leave it alone, so find will get the string *.txt as the argument to -name and it will use it as when matching potential names.

Eric Renouf
  • 18,431
0

You can try double quoting the file name.

find . -name "*.txt" -print

That works on my system (Debian 8)

rcjohnson
  • 899