`
xitong
  • 浏览: 6195122 次
文章分类
社区版块
存档分类
最新评论

find+*的问题

 
阅读更多

不久前做移植的时候想把某个目录下的C文件都找出来,然后拷贝下,结果一直报错,我用的是*.c作为pattern。今天看论坛的时候知道为什么了。

$ ls

test2.c test.c test.txt

目录下有两个.c文件,还有一个.txt文件

$ find . -name *.c

error:find: 路径必须在表达式之前: test.c
用法: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

$ find . -name *.txt

./test.txt

$ find . -name "*.c"

./test2.c
./test.c

原因:

若是不加引号 $ find . -name*.txt成功,而不加引号的 $ find . -name*.c出错了,得看看shell是怎么执行这句话的,首先shell读到了 *.txt 和 *.c,因为 * 是shell认为的meta(理解为特殊字符),所以就先解释*txt 先执行成 test.txt,传递给find,find就去执行自己的操作,根据pattern发现符合要求。而 *.c 被shell翻译成了test.c和test2.c,这时候命令就变成了 find . -name test.c test2.c 这就出错了!因为find -name 选项后面只能支持一个文件的搜索。所以对于test2.c是前面没有选项,就报错了!

对于有引号的 find . -name "*.c",shell读到"*.c"的时候,会当作参数处理,传递给find,find之后会自己处理 *.c,可以查看 man find ,里面有

The metacharacters (`*', `?', and `[]')match a `.'

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics