bash osx find exec rm find: exec: unknown primary or operator
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27915657/
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
osx find exec rm find: exec: unknown primary or operator
提问by freedrull
I've got a bunch of files that end in "-e" that I want to remove.
我有一堆以“-e”结尾的文件要删除。
$ find . -name "*-e" exec rm {} \;
find: exec: unknown primary or operator
Is the regex expanding in some way that messes everything up?
正则表达式是否以某种方式扩展,把一切都搞砸了?
回答by anubhava
It should be:
它应该是:
find . -name "*-e" -exec rm '{}' \;
Or better:
或更好:
find . -name "*-e" -exec rm '{}' +
As per man find
:
根据man find
:
-exec utility [argument ...] {} +
Same as -exec, except that ``{}'' is replaced with as many pathnames as possible for
each invocation of utility. This behaviour is similar to that of xargs(1).
回答by JL Peyret
+1 to @anubhava's answer. However, I did have to be very careful before it worked:
+1 对@anubhava 的回答。但是,在它起作用之前,我确实必须非常小心:
TLDR: the format is -exec <your-command> {} \;
TLDR:格式是 -exec <your-command> {} \;
Hint: use something harmless, like echo
to get it working first.
提示:使用无害的东西,比如echo
先让它工作。
find . -name "*.sh" -exec echo {} \;
find . -name "*.sh" -exec echo {} \;
./081.documentation/000.sticky.list_url_info_markdown/worklocal.sh
./081.documentation/001.fixed.p2.collab_diagrams/common.sh
./081.documentation/001.fixed.p2.collab_diagrams/worklocal.sh
Once it works you can get all fancy with extra find options as in
一旦它起作用,您就可以通过额外的查找选项获得所有幻想,如
find . -name "*.sh" -maxdepth 3 -exec echo {} \;
find . -name "*.sh" -maxdepth 3 -exec echo {} \;
Be sure to use -exec
, not --exec
or exec
请务必使用-exec
, 不--exec
或exec
remember that find's options all take 1 dash. -exec
is no different.
请记住, find 的选项都需要 1 个破折号。 -exec
没有什么不同。
find . -name "*.sh" --exec echo {} \;
find . -name "*.sh" --exec echo {} \;
? find: --exec: unknown primary or operator
? find: --exec: unknown primary or operator
find . -name "*.sh" exec echo {} \;
find . -name "*.sh" exec echo {} \;
? find: exec: unknown primary or operator
? find: exec: unknown primary or operator
Notice how what comes after find:reflects what you put in? Here find has no idea what's going on so you get a generic I don't knowmessage.
请注意find:之后的内容如何反映您输入的内容?这里 find 不知道发生了什么,所以你得到一个通用的我不知道的消息。
Once you are using -exec
terminate with \;
- that space is critical
一旦您使用-exec
终止\;
- 该空间至关重要
find . -name "*.sh" -exec echo {}\;
find . -name "*.sh" -exec echo {}\;
? find: -exec: no terminating ";" or "+"
................. you want to see -exec
in your error message. that part is good ? find knows what it is looking at so it has -exec
related messages.
? find: -exec: no terminating ";" or "+"
....... 你想-exec
在你的错误信息中看到。那部分好吗?find 知道它在看什么,所以它有-exec
相关的消息。
Finally, replace echo
with your actual payload:
最后,替换echo
为您的实际有效负载:
find . -name "*.sh" -exec reformat.py --myfancy-option-to-reformat {} \;
find . -name "*.sh" -exec reformat.py --myfancy-option-to-reformat {} \;
'{}' vs {} made no difference whatsoever.
“{}”与 {} 没有任何区别。
It might, under some conditions, maybe if your paths have spaces in them so if you're unsure put the quotes. But I couldn't trigger any errors even with spaces and with different commands (cat, ls) than echo.
在某些情况下,如果您的路径中有空格,则可能会在不确定时加上引号。但是,即使使用空格和与 echo 不同的命令(cat、ls),我也无法触发任何错误。
find . -name "*.sh" -exec echo {} \;
find . -name "*.sh" -exec echo {} \;
./bar zoom.sh
./foo.sh
./worklocal.sh
find . -name "*.sh" -exec echo '{}' \;
find . -name "*.sh" -exec echo '{}' \;
./bar zoom.sh
./foo.sh
./worklocal.sh
I'm not saying the quotes are useless, I am saying they're not causing unknown primary or operator.
我不是说引号没用,我是说它们不会导致未知的 primary 或 operator。
ending with +
strips newlines between invocations:
以+
调用之间的换行符结尾:
Don't forget that space before the +.
不要忘记 + 之前的空格。
find . -name "*.sh" -exec echo {} +
find . -name "*.sh" -exec echo {} +
./bar zoom.sh ./foo.sh ./worklocal.sh
Yes, I dream of a more user-friendly find. But macos's Spotlight-related mdfind is, by light-years, even more unfriendly when it comes to options
是的,我梦想有一个更加用户友好的发现。但是 macos 的与 Spotlight 相关的mdfind 就光年而言,在选项方面更加不友好
回答by dba.in.ua
just use: find . "*-e" -type f exec rm '{}' +
it works with Mac OSX find version
只需使用:find . "*-e" -type f exec rm '{}' +
它适用于 Mac OSX 查找版本