【问题标题】:Shell notation: find . -type f -exec file '{}' \; [duplicate]壳牌符号:find . -type f -exec 文件 '{}' \; [复制]
【发布时间】:2014-02-04 23:26:12
【问题描述】:

这是一个相对简单的命令,所以如果存在重复并且有人可以推荐我,我很抱歉,我将删除/关闭这个问题。

查找手册页

   find . -type f -exec file '{}' \;

   Runs 'file' on every file in or below the current directory.  Notice that the braces are enclosed in single quote marks to protect them from interpretation
   as shell script punctuation.   The semicolon is similarly protected by the use of a backslash, though ';' could have been used in that case also.

我不明白\; 的表示法。这到底是什么?

【问题讨论】:

    标签: linux bash


    【解决方案1】:

    在 find 命令中,操作-exec 后跟一个命令和该命令的参数。因为可以有任意数量的参数,find 需要某种方式知道它何时结束。分号告诉find它已经到达命令参数的末尾。

    留给他们自己的设备,大多数贝壳会吃分号。我们希望将该分号传递给find 命令。因此,我们用反斜杠转义它。这告诉 shell 将分号视为 find 命令的参数之一。

    更多: 为什么不,有人可能会问,只是假设 exec 命令的参数只是到行尾?为什么我们需要发出结束 exec 命令参数的信号?原因是find 具有高级功能。举个例子,考虑一下:

    find . -name '*.pdf' -exec echo Yes, we have a pdf: {} \; -o -exec echo No, not a pdf: {}  \;
    

    【讨论】:

    • 为什么find 需要知道它何时结束? -exec 命令的参数是一个有限列表,所以它不应该知道什么时候结束吗?
    • @imagineerThis,好问题。查看更新的答案。
    猜你喜欢
    • 2017-01-27
    • 2015-11-18
    • 2013-11-13
    • 2014-05-25
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多