1

This question already has an answer here:

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?

marked as duplicate by Gilles, cas, cuonglm shell Nov 1 '15 at 4:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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

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.

  • Thank you for clearing this up for me and formatting the code! I appreciate it. – rriera96 Nov 2 '15 at 15:59
0

You can try double quoting the file name.

find . -name "*.txt" -print

That works on my system (Debian 8)

Not the answer you're looking for? Browse other questions tagged or ask your own question.