3

I want to know what is the difference between this two commands..

find . –name *.txt

find . –name "*.txt"

I run it in the system and cant find any difference, what does the sign " " do?

devnull
  • 118,548
  • 33
  • 236
  • 227
user2922456
  • 391
  • 4
  • 10
  • 24
  • Aside: it looks like you have used a word processor to prepare this question. Never use a word processor to edit code, unless you like hunting for invisible characters and characters that look like ASCII but are not. – n. m. could be an AI Oct 26 '13 at 08:28

1 Answers1

8

When you don't use quotes around the glob pattern, i.e. when you say:

find . -name *.txt

then the shell would expand *.txt to the matching files in the current directory before passing those as an argument to find. If no file matching the pattern is found, then the behaviour is similar to the quoted variant.

When you use quotes, i.e. when you say:

find . -name "*.txt"

the shell passes *.txt as an argument to find.

Always use quotes when specifying a glob (esp. when used as an argument to find).


An example might help:

$ touch {1..5}.txt                # Create a few .txt files
$ ls
1.txt  2.txt  3.txt  4.txt  5.txt
$ find . -name *.txt              # find *.txt files
find: paths must precede expression: 2.txt
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
$ find . -name "*.txt"            # find "*.txt" files
./2.txt
./4.txt
./3.txt
./5.txt
./1.txt
devnull
  • 118,548
  • 33
  • 236
  • 227