【问题标题】:GNU findutils doesn't find fileGNU findutils 找不到文件
【发布时间】:2021-10-28 15:29:53
【问题描述】:

搜索位于 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 目录中搜索只返回一个文件?

【问题讨论】:

标签: linux bash find gnu-findutils


【解决方案1】:

为什么在 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

或....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 2011-03-14
    • 2019-02-23
    相关资源
    最近更新 更多