Linux查找命令:奇怪的行为

时间:2016-08-08 20:29:30

标签: linux find

为什么这三个find命令的结果会有所不同,具体取决于我所在的目录?

daniel@machine:~$ find /home/daniel/workspace/service/tests/smoke -name *.test
(result: lists all files named *.test as expected)

daniel@machine:~/workspace$ find /home/daniel/workspace/service/tests/smoke -name *.test
find: paths must precede expression: service_real_all_tests_possible.test
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

daniel@dtrezub:~/workspace/service$ find /home/daniel/workspace/service/tests/smoke -name *.test
/home/daniel/workspace/service/tests/smoke/doc.test

2 个答案:

答案 0 :(得分:2)

你需要引用这样的名字

find /home/daniel/workspace/service/tests/smoke -name "*.test"

否则,shell正在进行全局扩展。

答案 1 :(得分:0)

不同的行为取决于当前目录(不是子目录)中的文件数与*.test匹配。

0 matches => find command is executed like you intended
1 match   => find command will only look for files with the same name as the *.test in your current dir.
>1 match  => Your find command is incorrect 
# ignoring special cases like a filename `-name something.test`

修正:将*.test放在引号中或写下\*.test