【问题标题】:bash, find, exec and echobash、查找、执行和回显
【发布时间】:2011-05-27 02:54:00
【问题描述】:
find . \
  -name 'user_prefs' \
  -exec echo "whitelist_from basheer@hydrofitgroup.com" >> {} \;'

我想将whitelist_from basheer@hydrofitgroup.com 行添加到find 找到的所有文件中,但我的命令不起作用。 它只是创建文件“{}”。

我应该使用for 命令吗?

谢谢!

【问题讨论】:

    标签: bash for-loop command-line find


    【解决方案1】:

    如前所述,鼓励使用 xargs,但您也可以通过以下方式避免多次执行 sh:

    find . -name 'user_prefs' | while read filename; do echo "whitelist_from basheer@hydrofitgroup.com" >>"$filename"; done
    

    【讨论】:

    • 这比xargs 版本快了不少。
    【解决方案2】:

    你必须转义'>>',例如这样:

    find . -name 'user_prefs' -exec sh -c 'echo "whitelist_from basheer@hydrofitgroup.com" >> {}' \;
    

    【讨论】:

    • 这是一个正确的答案,但我不会完全称之为“转义”。
    猜你喜欢
    • 1970-01-01
    • 2019-02-22
    • 2019-05-19
    • 2015-09-29
    • 2017-07-08
    • 1970-01-01
    • 2012-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多