在两个不同的主目录中搜索.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
。
nullglob
shell选项生效并且find
命令从模式不匹配任何文件的位置执行,则模式将被扩展为无。
如果您想确保shell路径名扩展不应用于模式,那么请引用它:
find /home -name '*.txt'
或
find /home -name *.txt
还是……