在 find 命令中使用“单引号”或不使用“单引号”有什么区别

Yun*_*ong 7 shell find quoting

find ~/ -name *test.txt
find ~/ -name '*test.txt'
Run Code Online (Sandbox Code Playgroud)

我需要构建一个示例,其中第一种形式失败但第二种形式仍然有效。

Eta*_*ner 21

引号保护内容不受 shell 通配符扩展的影响。运行该命令(或者更简单的只是echo *test.txt在一个包含footest.txt文件的目录中,然后在没有任何文件结尾的目录中运行test.txt,您将看到不同之处。

$ ls
a  b  c  d  e
$ echo *test.txt
*test.txt
$ touch footest.txt
$ echo *test.txt
footest.txt
Run Code Online (Sandbox Code Playgroud)

find 也会发生同样的事情。

$ set -x
$ find . -name *test.txt
+ find . -name footest.txt
./footest.txt
$ find . -name '*test.txt'
+ find . -name '*test.txt'
./footest.txt
$ touch bartest.txt
+ touch bartest.txt
$ find . -name *test.txt
+ find . -name bartest.txt footest.txt
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]
$ find . -name '*test.txt'
+ find . -name '*test.txt'
./bartest.txt
./footest.txt
Run Code Online (Sandbox Code Playgroud)