【问题标题】:"find: paths must precede expression:" How do I specify a recursive search that also finds files in the current directory?“查找:路径必须在表达式之前:”如何指定在当前目录中也可以找到文件的递归搜索?
【发布时间】:2011-09-23 15:24:23
【问题描述】:

我很难让 find 在当前目录及其子目录中查找匹配项。

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

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

此错误是什么意思,如何从当前目录及其子目录中获取匹配项?

【问题讨论】:

  • 作为记录,msysgitfind 可能会抛出此错误,除非您用引号将模式括起来:find . -name "*test.c"。 (如果您选择喜欢它而不是 Windows 的不同 find.exe 并从 cmd 使用)

标签: linux bash find


【解决方案1】:

试着把它放在引号里——你遇到了 shell 的通配符扩展,所以你实际传递给 find 的内容看起来像:

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

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

find . -name '*test.c'

注意文件表达式周围的单引号——它们会阻止 shell (bash) 扩展你的通配符。

【讨论】:

  • 举例来说,如果你执行echo *test.c,你可以看到发生了什么……结果不会是扩展通配符的echo,而是shell本身。简单的教训是,如果您使用通配符,请引用文件规范:-)
  • 感谢您帮助我处理这个 VARIANT。我尝试了在网上找到的find . -type f -printf ‘%TY-%Tm-%Td %TT %p\n’,并遇到了“路径必须先于表达式”。问题是引号太“聪明”了。我重新输入了命令,导致引号被替换,然后它运行了。
  • 由于某种原因,单引号对我不起作用。我不得不使用双引号。 ¯\_(ツ)_/¯
  • 单引号可用于 Busybox 和 GNU find - 如果使用通配符 *.$variable,则需要双引号。
  • @Planky:我把 : find , -name 'write.lock' 放在 shell 脚本文件中,但它有这个错误消息。但是如果我输入控制台,它就可以工作。有谁知道为什么?
【解决方案2】:

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

find . -name \*test.c

【讨论】:

  • #gitbash 这是我在 Windows 上使用 git bash 的解决方案,即使引用 PATTERN find . -name '*txt'
【解决方案3】:

试着把它放在引号里:

find . -name '*test.c'

【讨论】:

    【解决方案4】:

    从查找手册:

    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
    

    【讨论】:

      【解决方案5】:

      我看到这个问题已经回答了。我只想分享对我有用的东西。我在(-name 之间缺少一个空格。因此,选择排除其中一些文件的正确方法如下所示;

      find . -name 'my-file-*' -type f -not \( -name 'my-file-1.2.0.jar' -or -name 'my-file.jar' \) 
      

      【讨论】:

        【解决方案6】:

        当我试图找到多个文件名时遇到了这个问题,这些文件名我无法按照@Chris J 的回答中的描述组合成正则表达式,这对我有用

        find . -name one.pdf -o -name two.txt -o -name anotherone.jpg
        

        -o-or 是逻辑或。请参阅Finding Files on Gnu.org 了解更多信息。

        我在 CygWin 上运行它。

        【讨论】:

          【解决方案7】:

          在我的情况下,我在路径中缺少尾随 /

          find /var/opt/gitlab/backups/ -name *.tar
          

          【讨论】:

          • 不需要尾随 /
          猜你喜欢
          • 2012-05-29
          • 1970-01-01
          • 1970-01-01
          • 2014-02-13
          • 1970-01-01
          • 2018-05-09
          • 1970-01-01
          • 2021-07-28
          相关资源
          最近更新 更多