It is so strange that sometimes the following pattern of -name in find could unexpectedly search all the corresponding filenames successfully. By rights, it could only search one corresponding filename in the current directory via the shell parse (not find utility that without quotation).

find /tmp -type f -name *.txt


find /tmp -type f -name *.txt|wc -l
  • 1
    You're in an empty directory/one with no .txt files. Is that the question? Dec 2, 2017 at 9:00
  • Yes, there is just by chance no any .txt files under the current directory(/)!
    – lylklb
    Dec 2, 2017 at 10:22

1 Answer 1


Without any quoting *.txt is subject to pathname expansion (aka filename expansion). This happens in the current working directory (CWD). If the CWD contains any files with extension 'txt' then before calling find the shell replaces the *.txt parameter with the matching filenames and your command becomes something like this:

find /tmp -type f -name file1.txt file2.txt file3.txt ...

There are at least three results you can then get depending on whether the expansion returns...

  • ...multiple files: you will get an error
  • ...one file and its name matches a filename in the path(s) you are running find against: the -name test will match just that one file
  • ...one file and it matches no files in the searched path(s): -name will match nothing

On the other hand if the CWD does not contain any txt files then *.txt is passed to find and it behaves exactly the same as if you quoted it.

Moral: when using find always put quotes around patterns that contain globs unless you actually want the pathname expansion to occur (rarely would you see this done intentionally for the -name option).

  • Yes, you are right! I have already tested & verified the result! But why the find utility still always starts to search files from the current directory(/) regardless of specific searching path(/tmp)
    – lylklb
    Dec 2, 2017 at 10:16
  • Pathname expansion doesn't have anything to do with find. It's a function of the shell and it happens before find runs. So the expansion occurs against your CWD. But find will still be operating against the path you pass to it. You should not be seeing something different.
    – B Layer
    Dec 2, 2017 at 10:21
  • In fact, I mean that the find utility should be able to avoid & filtrate this unnecessary disturb from the current directory, or why not!?
    – lylklb
    Dec 2, 2017 at 10:36
  • Because, like I said, this all happens before find starts...it has no way of knowing that you passed it *.txt. I made a small update to my answer to point out that shell replaces the param.
    – B Layer
    Dec 2, 2017 at 10:37
  • Yes, thanks to your wonderful explanations!!
    – lylklb
    Dec 2, 2017 at 10:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .