Linux “查找:路径必须在表达式之前:”我如何指定一个递归搜索也可以在当前目录中查找文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6495501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:46:46  来源:igfitidea点击:

"find: paths must precede expression:" How do I specify a recursive search that also finds files in the current directory?

linuxbashfind

提问by Chris Finley

I am having a hard time getting findto look for matches in the current directory as well as its subdirectories.

我很难找到在当前目录及其子目录中查找匹配项的方法。

When I run find *test.cit only gives me the matches in the current directory. (does not look in subdirectories)

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

If I try find . -name *test.cI would expect the same results, but instead it gives me only matches that are in a subdirectory. When there are files that should match in the working directory, it gives me: find: paths must precede expression: mytest.c

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

What does this error mean, and how can I get the matches from both the current directory and its subdirectories?

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

采纳答案by Chris J

Try putting it in quotes -- you're running into the shell's wildcard expansion, so what you're acually passing to find will look like:

尝试将它放在引号中——您遇到了 shell 的通配符扩展,因此您实际传递给 find 的内容将如下所示:

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

...causing the syntax error. So try this instead:

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

find . -name '*test.c'

Note the single quotes around your file expression -- these will stop the shell (bash) expanding your wildcards.

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

回答by rkulla

Try putting it in quotes:

尝试将其放在引号中:

find . -name '*test.c'

回答by Jim Garrison

What's happening is that the shell is expanding "*test.c" into a list of files. Try escaping the asterisk as:

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

find . -name \*test.c

回答by Nick Constantine

From find manual:

从查找手册:

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

回答by Vikash Singh

In my case i was missing trailing /in path.

在我的情况下,我错过/了路径尾随。

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

回答by HappyTown

I came across this question when I was trying to find multiple filenames that I could not combine into a regular expression as described in @Chris J's answer, here is what worked for me

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

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

-oor -oris logical OR. See Finding Files on Gnu.orgfor more information.

-o-or是逻辑或。有关更多信息,请参阅在 Gnu.org 上查找文件

I was running this on CygWin.

我在 CygWin 上运行它。