【问题标题】:Bash: How to find and replace word in files name [duplicate]Bash:如何在文件名中查找和替换单词[重复]
【发布时间】:2017-09-28 03:20:18
【问题描述】:

用于查找名称中包含foo 的所有文件。

find / -name *foo*

例如结果是

/etc/lib/football
/var/log/some/somefooandother
/home/me/anotherfoo
/etc/all/files/includingfooinnamefoo

但是我如何替换所有 foo -> bar 在这些文件的名称中?

/etc/lib/bartball
/var/log/some/somebarandother
/home/me/anotherbar
/etc/all/files/includingbarinnamebar

【问题讨论】:

    标签: bash replace find


    【解决方案1】:

    你可以试试这个:

    for i in $(ls | grep "foo"); do
    j="$(echo $i | sed s/foo/bar/)"
    mv "$i" "$j"
    done;
    
    • 查找包含字符串 foo 的文件并存储到变量 i

          for i in $(ls | grep "foo");
      
    • 重命名并存储到另一个变量 j

      j="$(echo $i | sed s/foo/bar/)"
      
    • 使用 mv 重命名文件名

      mv "$i" "$j"
      

    我已经在我的系统上检查了这个,它运行良好,但在继续之前备份您的数据总是安全的。你永远不知道一个错字(我的或你的)会导致什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-30
      • 1970-01-01
      • 2022-11-19
      • 2017-11-04
      • 2022-01-18
      • 2011-04-25
      • 1970-01-01
      • 2016-08-04
      相关资源
      最近更新 更多