【发布时间】:2026-01-11 00:50:02
【问题描述】:
我想知道这两个命令有什么区别..
find . –name *.txt
find . –name "*.txt"
我在系统中运行它并没有发现任何区别,
" " 的标志是做什么的?
【问题讨论】:
-
旁白:您似乎使用了文字处理器来准备这个问题。切勿使用文字处理器来编辑代码,除非您喜欢寻找不可见的字符和看起来像 ASCII 但不是的字符。
我想知道这两个命令有什么区别..
find . –name *.txt
find . –name "*.txt"
我在系统中运行它并没有发现任何区别,
" " 的标志是做什么的?
【问题讨论】:
当您不在 glob 模式周围使用引号时,即当您说:
find . -name *.txt
然后shell 会将*.txt 扩展为当前目录 中的匹配文件,然后将它们作为参数传递给find。如果没有找到与该模式匹配的文件,则行为类似于引用的变体。
当你使用引号时,即当你说:
find . -name "*.txt"
shell 将*.txt 作为参数传递给find。
在指定 glob 时始终使用引号(尤其是用作 find 的参数时)。
一个例子可能会有所帮助:
$ 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
【讨论】: