GNU findutils not find file



在两个不同的主目录中搜索.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目录中搜索只返回一个文件?

为什么从bob目录中搜索只返回一个文件?

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

另一方面,当模式不匹配任何文件(相对于工作目录)时,它将作为文字传递。至少在默认情况下——您应该小心这一点,因为如果nullglobshell选项生效并且find命令从模式不匹配任何文件的位置执行,则模式将被扩展为无。

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

find /home -name '*.txt'

find /home -name *.txt

还是……

最新更新