【问题标题】:Redirect output from -exec in find command to variable将 find 命令中 -exec 的输出重定向到变量
【发布时间】:2012-03-01 01:16:29
【问题描述】:

我有一个命令格式:

find -name *.* -type f -exec rm {} -v \;

我想获取它处理成变量的文件列表。我将如何在 ksh 中做到这一点?

【问题讨论】:

    标签: shell find ksh


    【解决方案1】:

    我不是 ksh 用户,所以我可能错了。在 bash 中,我必须掩盖星号:

    find -name "*.*" -type f -exec rm {} -v \;
    

    我可以使用 -delete 代替 -exec rm,但 delete 对于 GNU-find 是特殊的:

    find -name "*.*" -type f -delete
    

    要输出文件名,请附加打印:

    find -name "*.*" -type f -delete -print
    

    并捕获输出

    deleted=$(find -name "*.*" -type f -delete -print)
    

    【讨论】:

      【解决方案2】:

      这是一个例子:

      VAR=$(your statement)
      

      【讨论】:

        【解决方案3】:
         VAR=`find /dir -name '*.*' -type f -print -exec rm {} \;`
        

        【讨论】:

        • 使用$() 代替反引号,因为后者已被弃用
        【解决方案4】:

        在你的论点后附加一个“-print”

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-05-06
          • 2012-07-05
          • 1970-01-01
          • 2011-12-15
          • 1970-01-01
          • 2019-03-20
          • 1970-01-01
          相关资源
          最近更新 更多