bash 查找:缺少 -exec 的参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2961673/
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
find: missing argument to -exec
提问by Abs
I was helped out today with a command, but it doesn't seem to be working. This is the command:
我今天得到了一个命令的帮助,但它似乎不起作用。这是命令:
find /home/me/download/ -type f -name "*.rm" -exec ffmpeg -i {} -sameq {}.mp3 && rm {}\;
The shell returns
外壳返回
find: missing argument to `-exec'
What I am basically trying to do is go through a directory recursively (if it has other directories) and run the ffmpeg command on the .rm
file types and convert them to .mp3
file types. Once this is done, remove the .rm
file that has just been converted.
我基本上要做的是递归遍历一个目录(如果它有其他目录)并对.rm
文件类型运行 ffmpeg 命令并将它们转换为.mp3
文件类型。完成后,删除.rm
刚刚转换的文件。
I appreciate any help on this.
我很感激这方面的任何帮助。
采纳答案by Abs
I figured it out now. When you need to run two commands in exec in a find you need to actually have two separate execs. This finally worked for me.
我现在想通了。当您需要在 exec 中在 find 中运行两个命令时,您实际上需要有两个单独的 exec。这最终对我有用。
find . -type f -name "*.rm" -exec ffmpeg -i {} -sameq {}.mp3 \; -exec rm {} \;
回答by Marian
A -exec
command must be terminated with a ;
(so you usually need to type \;
or ';'
to avoid interpretion by the shell) or a +
. The difference is that with ;
, the command is called once per file, with +
, it is called just as few times as possible (usually once, but there is a maximum length for a command line, so it might be split up) with all filenames. See this example:
甲-exec
命令需要与被终止;
(因此通常需要输入\;
或';'
由外壳避免解译)或+
。不同之处在于 with ;
,每个文件调用一次命令, with +
,它被调用的次数尽可能少(通常是一次,但命令行有最大长度,因此它可能会被拆分)与所有文件名. 看这个例子:
$ cat /tmp/echoargs
#!/bin/sh
echo - -
$ find /tmp/foo -exec /tmp/echoargs {} \;
/tmp/foo - -
/tmp/foo/one - -
/tmp/foo/two - -
$ find /tmp/foo -exec /tmp/echoargs {} +
/tmp/foo - /tmp/foo/one - /tmp/foo/two
Your command has two errors:
您的命令有两个错误:
First, you use {};
, but the ;
must be a parameter of its own.
首先,您使用{};
,但;
必须是它自己的参数。
Second, the command ends at the &&
. You specified “run find, and if that was successful, remove the file named {};
.“. If you want to use shell stuff in the -exec
command, you need to explicitly run it in a shell, such as -exec sh -c 'ffmpeg ... && rm'
.
其次,命令以&&
. 您指定了“运行查找,如果成功,删除名为{};
.”的文件。如果要在-exec
命令中使用 shell 内容,则需要在 shell 中显式运行它,例如-exec sh -c 'ffmpeg ... && rm'
.
However you should not add the {} inside the bash command, it will produce problems when there are special characters. Instead, you can pass additional parameters to the shell after -c command_string
(see man sh
):
但是你不应该在bash命令中添加{},当有特殊字符时会产生问题。相反,您可以在-c command_string
(参见man sh
)之后将附加参数传递给 shell :
$ ls
$(echo damn.)
$ find * -exec sh -c 'echo "{}"' \;
damn.
$ find * -exec sh -c 'echo ""' - {} \;
$(echo damn.)
You see the $
thing is evaluated by the shell in the first example. Imagine there was a file called $(rm -rf /)
:-)
您会$
在第一个示例中看到该事物由外壳程序评估。想象一下有一个名为$(rm -rf /)
:-)的文件
(Side note: The -
is not needed, but the first variable after the command is assigned to the variable $0
, which is a special variable normally containing the name of the program being run and setting that to a parameter is a little unclean, though it won't cause any harm here probably, so we set that to just -
and start with $1
.)
(旁注:-
不需要,但命令后的第一个变量被分配给变量$0
,这是一个特殊的变量,通常包含正在运行的程序的名称并将其设置为参数有点不干净,虽然它赢了可能不会在这里造成任何伤害,所以我们将其设置为 just-
并从$1
.)
So your command could be something like
所以你的命令可能是这样的
find -exec bash -c 'ffmpeg -i "" -sameq "".mp3 && rm "".mp3' - {} \;
But there is a better way. find supports and
and or
, so you may do stuff like find -name foo -or -name bar
. But that also works with -exec
, which evaluates to true if the command exits successfully, and to false if not. See this example:
但是有更好的方法。找到支持and
和or
,所以你可以做类似的事情find -name foo -or -name bar
。但这也适用于-exec
,如果命令成功退出,则计算为 true,否则为 false。看这个例子:
$ ls
false true
$ find * -exec {} \; -and -print
true
It only runs the print if the command was successfully, which it did for true
but not for false
.
它只能运行该命令是否成功打印,它没有为true
而不为false
。
So you can use two exec statements chained with an -and
, and it will only execute the latter if the former was run successfully.
所以你可以使用两个用 an 链接的 exec 语句-and
,如果前者运行成功,它只会执行后者。
回答by Dustin Cowles
Try putting a space before each \;
尝试在每个 \ 之前放一个空格;
Works:
作品:
find . -name "*.log" -exec echo {} \;
Doesn't Work:
不起作用:
find . -name "*.log" -exec echo {}\;
回答by Dominique
Just for your information:
I have just tried using "find -exec" command on a Cygwin system (UNIX emulated on Windows), and there it seems that the backslash before the semicolon must be removed:find ./ -name "blabla" -exec wc -l {} ;
仅供参考:
我刚刚尝试在 Cygwin 系统(在 Windows 上模拟 UNIX)上使用“find -exec”命令,似乎必须删除分号前的反斜杠:find ./ -name "blabla" -exec wc -l {} ;
回答by John Cooper
Just in case anyone sees a similar "missing -exec args" in Amazon Opsworks Chef bash scripts, I needed to add another backslash to escape the \;
以防万一有人在 Amazon Opsworks Chef bash 脚本中看到类似的“缺少 -exec args”,我需要添加另一个反斜杠来转义 \;
bash 'remove_wars' do
user 'ubuntu'
cwd '/'
code <<-EOH
find /home/ubuntu/wars -type f -name "*.war" -exec rm {} \;
EOH
ignore_failure true
end
回答by sellmaurer
Also, if anyone else has the "find: missing argument to -exec" this might help:
此外,如果其他人有“查找:缺少 -exec 的参数”,这可能会有所帮助:
In some shells you don't need to do the escaping, i.e. you don't need the "\" in front of the ";".
在某些 shell 中,您不需要进行转义,即您不需要“;”前面的“\”。
find <file path> -name "myFile.*" -exec rm - f {} ;
回答by Matthew Smith
You need to do some escaping I think.
我认为你需要做一些逃避。
find /home/me/download/ -type f -name "*.rm" -exec ffmpeg -i {} \-sameq {}.mp3 \&\& rm {}\;
回答by VeeArr
Both {} and && will cause problems due to being expanded by the command line. I would suggest trying:
{} 和 && 都会因为被命令行展开而导致问题。我建议尝试:
find /home/me/download/ -type f -name "*.rm" -exec ffmpeg -i \{} -sameq \{}.mp3 \; -exec rm \{} \;
回答by Azafo Cossa
You have to put a space between {}
and \;
你必须在{}
和之间留一个空格\;
So the command will be like:
所以命令将是这样的:
find /home/me/download/ -type f -name "*.rm" -exec ffmpeg -i {} -sameq {}.mp3 && rm {} \;
回答by woo
If you are still getting "find: missing argument to -exec" try wrapping the execute argument in quotes.
如果您仍然收到“find:缺少 -exec 的参数”,请尝试将 execute 参数用引号括起来。
find <file path> -type f -exec "chmod 664 {} \;"