GNU findutils 找不到文件

GNU findutils doesn't find file

在 2 个不同的主目录中搜索 .txt 文件时,根据当前的工作目录,只会显示一个。这是为什么?

/home/bob/1.txt 
/home/alice/dir1/2.txt

pwd /tmp
[root@host tmp]#find /home -name *.txt 
/home/bob/1.txt 
/home/alice/dir1/2.txt

pwd /home
[root@host bob]#find /home -name *.txt 
/home/bob/1.txt

为什么在 bob 目录中只搜索 return 一个文件?

Why does searching from within the bob directory only return the one file?

因为当工作目录是 /home/bob 时,find 命令中的 *.txt 被 shell 扩展(到 1.txt)并且是传递给 find 的内容。即find /home -name 1.txt。这将在 /home/bob 中找到该文件,但不会在 /home/alice 中找到名称不同的文件。如果存在这样的文件,它找到/home/alice/1.txt

另一方面,当模式不匹配任何文件(相对于工作目录)时,它将作为文字传递。至少在默认情况下——你应该小心这一点,因为如果 nullglob shell 选项有效并且 find 命令是从模式与任何文件都不匹配的位置。

如果您想确保 shell 路径名扩展不应用于模式,请引用它:

find /home -name '*.txt'

find /home -name \*.txt

或....