首页 文章

“find:paths必须在表达式之前:”如何指定也在当前目录中查找文件的递归搜索?

提问于
浏览
200

我很难获得 find 来查找当前目录及其子目录中的匹配项 .

当我运行 find *test.c 时,它只给我当前目录中的匹配项 . (不查看子目录)

如果我尝试 find . -name *test.c 我会期望相同的结果,但它只给我一个子目录中的匹配 . 如果有工作目录中应该匹配的文件,它会给我: find: paths must precede expression: mytest.c

这个错误意味着什么,以及如何从当前目录及其子目录获取匹配?

4 回答

  • 340

    试着把它放在引号中:

    find . -name '*test.c'
    
  • 24

    从查找手册:

    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
    
  • 13

    试着把它放在引号中 - 你正在进入shell的通配符扩展,所以你正在寻找的东西看起来像:

    find . -name bobtest.c cattest.c snowtest.c
    

    ...导致语法错误 . 所以试试这个:

    find . -name '*test.c'
    

    请注意文件表达式周围的单引号 - 这将停止shell(bash)扩展通配符 .

  • 8

    发生的事情是shell正在将“* test.c”扩展为文件列表 . 尝试将星号转义为:

    find . -name \*test.c
    

相关问题