【问题标题】:How to Replace a string pattern with another string pattern inside all files in directories and subdirectories如何在目录和子目录中的所有文件中将字符串模式替换为另一个字符串模式
【发布时间】:2012-12-28 22:32:46
【问题描述】:

我正在重新构建我的网站,并且必须重新安排文件中的引用。有一个包含许多子目录和 php 文件的目录。我正在寻找某种命令/脚本,该命令/脚本可以搜索该目录中的每个文件以查找此模式并将其替换为自定义模式。

例如:我想用 /shopping/shop/ipad 替换确切出现的 one/two/three/ 。换句话说,如果任何文件包含一些字符串例如:

location:index.php?p=one/two/three/abcdefg&aid

那么它应该被替换为

location:index.php?p=/shopping/shop/ipad/abcdefg&aid

注意内容替换应该发生在每个文件中,在主目录中的每个目录/ 子目录中(比如主目录名称是abc & 所有子目录和文件都在abc 中)。不应更改目录名称本身。我怎样才能做到这一点?我的服务器运行 linux 并且我有 root 和 shell 访问权限。

【问题讨论】:

标签: linux shell grep


【解决方案1】:
find . -type f|xargs perl -pi -e 's/one\/two\/three/shopping\/shop\/ipad/g'

经过测试并且工作正常。如果有 o,它也不会替换 仅one

你也可以找到here

【讨论】:

    【解决方案2】:

    sed 就是你要找的东西:

    find .  -exec sed -i "s/one\/two\/three/shopping\/shop\/ipad/g" '{}' \;
    

    【讨论】:

    • 这将替换one,即使有只是 one,而不是one/two/three
    【解决方案3】:

    试试这个:

    find . -type f -print0 | xargs -0 sed -i 's/one\/two\/three/shopping\/shop\/ipad/g'
    

    【讨论】:

      猜你喜欢
      • 2011-11-19
      • 1970-01-01
      • 2015-06-08
      • 2017-03-03
      • 2011-07-15
      • 2020-02-21
      • 1970-01-01
      • 1970-01-01
      • 2021-06-20
      相关资源
      最近更新 更多