“查找:路径必须位于表达式之前:”如何指定同时查找当前目录中文件的递归搜索?

发布于 2024-11-17 09:30:19 字数 314 浏览 4 评论 0原文

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

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

如果我尝试 find . -name *test.c 我希望得到相同的结果,但它只给我子目录中的匹配项。当工作目录中存在应该匹配的文件时,它会给出: find: paths Must before expression: mytest.c

此错误意味着什么,以及如何从当前的两个文件中获取匹配项目录及其子目录?

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

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

If I try find . -name *test.c I 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

旧夏天 2024-11-24 09:30:19

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

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

...导致语法错误。因此,请尝试以下操作:

find . -name '*test.c'

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

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:

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.

花伊自在美 2024-11-24 09:30:19

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

find . -name \*test.c

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

find . -name \*test.c
遮云壑 2024-11-24 09:30:19

从查找手册:

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

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
断爱 2024-11-24 09:30:19

尝试将其放在引号中:

find . -name '*test.c'

Try putting it in quotes:

find . -name '*test.c'
情绪操控生活 2024-11-24 09:30:19

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

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

I see this question is already answered. I just want to share what worked for me. I was missing a space between ( and -name. So, the correct way of chosen files with excluding some of them would be like below;

find . -name 'my-file-*' -type f -not \( -name 'my-file-1.2.0.jar' -or -name 'my-file.jar' \) 
愚人国度 2024-11-24 09:30:19

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

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

-o- or 是逻辑或。请参阅在 Gnu.org 上查找文件< /a> 了解更多信息。

我在 CygWin 上运行这个。

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

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

-o or -or is logical OR. See Finding Files on Gnu.org for more information.

I was running this on CygWin.

丘比特射中我 2024-11-24 09:30:19

你可以试试这个:

cat $(file $( find . -readable) | grep ASCII | tr ":" " " | awk '{print $1}')

这样,你可以找到所有带有ascii的可读文件,

如果你想指定他的权重和不可执行的文件,可以用cat读取它们:

cat $(file $( find . -readable ! -executable -size 1033c) | grep ASCII | tr ":" " " | awk '{print $1}')

You can try this:

cat $(file $( find . -readable) | grep ASCII | tr ":" " " | awk '{print $1}')

with that, you can find all readable files with ascii and read them with cat

if you want to specify his weight and no-executable:

cat $(file $( find . -readable ! -executable -size 1033c) | grep ASCII | tr ":" " " | awk '{print $1}')
吾家有女初长成 2024-11-24 09:30:19

当我忘记 (-name 之间的空格时,出现此错误。

find . -not \(-name 'scripts' \)

应该是

find . -not \( -name 'scripts' \)

I got this error when I forgot the space between the ( and -name.

find . -not \(-name 'scripts' \)

Should have been

find . -not \( -name 'scripts' \)
留蓝 2024-11-24 09:30:19

问题在于脚本中如何处理命令替换和管道。您可以简化命令,而无需使用 find 的命令替换。以下是更正后的脚本:

find ./test/ -maxdepth 1 -type f -print0 | xargs -0 mv -t ./test/new

如果您需要使用变量 fbc,则应使用 eval 正确执行命令:

fbc="find ./test/ -maxdepth 1 -type f -print0 | xargs -0 mv -t ./test/new"
eval ${fbc}

这应该可以修复您遇到的错误。

The issue is with how the command substitution and piping are being handled in the script. You can simplify the command without using command substitution for find. Here is the corrected script:

find ./test/ -maxdepth 1 -type f -print0 | xargs -0 mv -t ./test/new

If you need to use the variable fbc, you should use eval to properly execute the command:

fbc="find ./test/ -maxdepth 1 -type f -print0 | xargs -0 mv -t ./test/new"
eval ${fbc}

This should fix the error you're encountering.

情话难免假 2024-11-24 09:30:19

就我而言,我在路径中缺少尾随 /

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

In my case i was missing trailing / in path.

find /var/opt/gitlab/backups/ -name *.tar
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文