我的find
没有达到预期的效果。如果有多个文件,则会因错误而暂停。
hpek@melda:~/temp/test$ ll
total 16
-rw-r--r-- 1 hpek staff 70B Mar 2 15:16 f1.tex
-rw-r--r-- 1 hpek staff 70B Mar 2 15:17 f2.tex
hpek@melda:~/temp/test$ find . -name *.tex
find: f2.tex: unknown option
hpek@melda:
如果我删除其中一个文件,那么它可以工作:
hpek@melda:~/temp/test$ rm f1.tex
hpek@melda:~/temp/test$ find . -name *.tex
./f2.tex
hpek@melda:~/temp/test$
删除哪个文件无关紧要。只要通配符提供了多个文件,find
就会停止。
答案 0 :(得分:6)
*.tex
被bash扩展。
find . -name *.tex
在您的情况下等同于
find . -name f1.tex f2.tex
解决方案:将"..."
放在带通配符的参数周围以避免shell扩展:
find . -name "*.tex"
这将按预期工作:
$ find . -name "*.tex"
./f1.tex
./f2.tex
答案 1 :(得分:4)
你必须引用通配符,所以bash不会扩展它们:
find . -name '*.tex'
现在正在由bash解释*
。结果,这是正在执行的实际命令:
find . -name f1.tex f2.text
答案 2 :(得分:3)
在到达*
命令之前,shell正在扩展您的通配符find
。换句话说,这是find
执行的命令:
find . -name f1.tex f2.tex
请注意,如果从其他目录执行命令,则会得到不同的结果,因为通配符的扩展方式会有所不同。
为了获得理想的结果,请尝试将其转义为:
find . -name \*.tex
答案 3 :(得分:2)
你需要find . -name "*.tex"
- 注意glob周围的引号。这里发生的是,在你的情况下,你的shell正在扩展glob,然后将结果传递给find,这导致find . -name f1.tex f2.tex
- 这不是使用find的有效方式。
通过将参数放在引号中,它将被传递给查找原样。