linux - 发现之间的区别。 –name *.txt 并找到 . –名称 "*.txt"

标签 linux shell unix find

我想知道这两个命令有什么区别..

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

关于linux - 发现之间的区别。 –name *.txt 并找到 . –名称 "*.txt",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19604349/

相关文章:

linux - 如果文件的列匹配,则 AWK 过滤文件

linux - 将文件从源复制到目标的 Bash Shell 脚本

bash - Vim:读取输入错误

shell - 用awk返回单引号之间的字符串

git - 用于 git 提交的 Shell 脚本助手

linux - 为什么 linux 内核模块中的 printk 不像 printf 那样缺乏浮点支持

linux -/etc/passwd的语法意义是什么

c++ - 定时函数调用

linux - Bash脚本为所有文件添加一个随机数

linux - 我如何将 postinst 脚本与 fakeroot deb 包生成器一起使用