我很难找到在当前目录及其子目录中查找匹配项.
当我运行find *test.c它时,只给我当前目录中的匹配项.(不查看子目录)
如果我尝试find . -name *test.c我会期望相同的结果,但它只给我在子目录中的匹配.当有工作目录中应该匹配的文件时,它会给我:find: paths must precede expression: mytest.c
这个错误是什么意思,我如何从当前目录及其子目录获取匹配?
Chr*_*s J 374
试着把它放在引号中 - 你正在进入shell的通配符扩展,所以你正在寻找的东西看起来像:
find . -name bobtest.c cattest.c snowtest.c
Run Code Online (Sandbox Code Playgroud)
...导致语法错误.所以试试这个:
find . -name '*test.c'
Run Code Online (Sandbox Code Playgroud)
注意文件表达式周围的单引号 - 这将停止shell(bash)扩展通配符.
Jim*_*son 28
发生的事情是shell正在将"*test.c"扩展为文件列表.尝试将星号转义为:
find . -name \*test.c
Run Code Online (Sandbox Code Playgroud)
Nic*_*ine 10
从查找手册:
NON-BUGS
Operator precedence surprises
The command find . -name afile -o -name bfile -print will never print
afile because this is actually equivalent to find . -name afile -o \(
-name bfile -a -print \). Remember that the precedence of -a is
higher than that of -o and when there is no operator specified
between tests, -a is assumed.
“paths must precede expression” error message
$ find . -name *.c -print
find: paths must precede expression
Usage: find [-H] [-L] [-P] [-Olevel] [-D ... [path...] [expression]
This happens because *.c has been expanded by the shell resulting in
find actually receiving a command line like this:
find . -name frcode.c locate.c word_io.c -print
That command is of course not going to work. Instead of doing things
this way, you should enclose the pattern in quotes or escape the
wildcard:
$ find . -name '*.c' -print
$ find . -name \*.c -print
Run Code Online (Sandbox Code Playgroud)
我看到这个问题已经有了答案。我只是想分享对我有用的东西。(我在和之间缺少一个空格-name。因此,选择文件并排除其中一些文件的正确方法如下所示;
find . -name 'my-file-*' -type f -not \( -name 'my-file-1.2.0.jar' -or -name 'my-file.jar' \)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
194030 次 |
| 最近记录: |