-2

Why the matched value with the parameter -name in find must be marked with double quotation?

# find / -type f -name *.out |wc -l 1>mv.out                                  
0403-027 The parameter list is too long.
# cat mv.out                                                   
       0
# find / -type f -name "*.out" |wc -l 1>mv.out                                   
# cat mv.out                                                                                           
  146302
ilkkachu
  • 138,973

2 Answers2

6

Without double quotes, the *.out gets expanded by the shell, meaning it gets replaced by all filenames in the current directory that match *.out (mainly any file ending with .out and not beginning with a dot). In your case there seem to be at least two files matching that are passed to find as parameters. find doesn't know what to do with them and complains, that the parameter list is too long.

By quoting the pattern, the shell leaves it as it is, just removing the quotes and find sees one untouched parameter *.out, which is what you probably intended.

Philippos
  • 13,453
-5

I'm not sure if this is exactly correct. But I believe putting it in quotations makes the parser read it as a regular expression. Without quotation marks, I believe the asterisk represents all the files in the current directory.

Check the difference in output between:

$ echo $(find ./ -name *)

and

$ echo $(find ./ -name "*")

So, with the first command you will likely get an error. If you have the files foo & bar in the current directory, the asterisk represents the filenames. So the command parser would read the command like this:

$ echo $(find ./ -name foo bar)

And the error output would be find: bar: unknown primary or operator.

The second command reads the asterisk as a regular expression, denoting search for any filename.

Edit: Though now, after testing the commands more, it seems on my system it works whether or not the argument value is in quotation marks, as long as some text is combined with the asterisk:

$ echo $(find ./ -name *foo)

is the same as

$ echo $(find ./ -name "*foo")

Perhaps I misunderstand the question?

  • 2
    When you refer to "the parser", think closely which program you mean (the shell, find, something else?) Also, where did you get the idea that it would be a regular expression? – ilkkachu Nov 28 '17 at 11:37
  • Re. the edit, see http://mywiki.wooledge.org/BashGuide/CommandsAndArguments and http://mywiki.wooledge.org/glob – ilkkachu Nov 28 '17 at 11:43
  • Semantics. I'm not terminology savvy when it comes to things technology. And I am not very experienced with regular expressions. – AntumDeluge Nov 28 '17 at 11:43
  • what is semantics? The difference between the shell's parse and that of the find utility? I don't think it is, they implement two different languages. Or the difference between regular expressions and shell patterns? They, too, are two different languages. Of course they share some features (like character groups/ranges/classes, like [ace]), and are used for pattern matching, but they are not the same. – ilkkachu Nov 28 '17 at 11:46
  • 3
    Just for future reference, "semantics" means "meaning". Meaning is rather important in communication. – AlexP Nov 28 '17 at 11:46
  • 2
    @AlexP, In this context, I presume the meaning of that word is to say that I'm nitpicking. Which would be fine, except that in this case there are relevant differences between things that seem similar. (and in my experience that is often the case in computing!) – ilkkachu Nov 28 '17 at 11:50