【问题标题】:Why are the backslash and semicolon required with the find command's -exec option?为什么 find 命令的 -exec 选项需要反斜杠和分号?
【发布时间】:2014-01-21 16:28:38
【问题描述】:

我已经开始在linux终端中组合不同的命令了。我想知道为什么以下命令需要反斜杠和分号:

find ./ -name 'blabla' -exec cp {} ./test \;

当一个简单的 cp 命令很简单时:

cp randomfile ./test

没有\;

它们是清楚地表明命令的结束,还是只是在文档中要求?基本原理是什么?

【问题讨论】:

  • ; 自己会结束命令。通过使用\;,文字; 将作为参数传递给find

标签: linux command find backslash


【解决方案1】:

来自“找人”:

所有关注 find 的参数被视为命令的参数,直到 由 ';' 组成的参数遇到了。

find 需要知道 exec 的参数何时终止。用 ; 终止 shell 命令是很自然的。因为shell也使用这个字符。出于同样的原因,这样的字符在通过 shell 插入时必须被转义。

【讨论】:

    【解决方案2】:

    使用分号前的反斜杠,因为; 是用于分隔shell 命令的列表运算符(或&&||)之一。例如:

    command1; command2
    

    find 实用程序正在使用;+ 来终止-exec 调用的shell 命令。

    因此,为了避免解释特殊的 shell 字符,需要使用反斜杠对其进行转义,以消除读取下一个字符和行继续的任何特殊含义。

    因此,find 命令允许使用以下示例语法:

    find . -exec echo {} \;
    find . -exec echo {} ';'
    find . -exec echo {} ";"
    find . -exec echo {} \+
    find . -exec echo {} +
    

    另见:

    【讨论】:

    • 感谢您列出替代方案。
    猜你喜欢
    • 2011-01-23
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 2018-08-07
    • 2010-11-07
    相关资源
    最近更新 更多