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

Chr*_*ley 225 linux bash find

我很难找到在当前目录及其子目录中查找匹配项.

当我运行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)扩展通配符.

  • 举例来说,如果你做`echo*test.c`你可以看到发生了什么......结果不会是扩展通配符的回声,而是shell本身.简单的一课就是如果你使用通配符,引用filespec :-) (15认同)
  • 出于某种原因,单引号对我不起作用.我不得不使用双引号.¯\\ _(ツ)_ /¯ (2认同)

Jim*_*son 28

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

find . -name \*test.c
Run Code Online (Sandbox Code Playgroud)


rku*_*lla 15

试着把它放在引号中:

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)


cel*_*-in 8

我看到这个问题已经有了答案。我只是想分享对我有用的东西。(我在和之间缺少一个空格-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)