【发布时间】:2013-04-06 21:21:48
【问题描述】:
我需要编写一个脚本,在一个目录及其所有子目录中的所有文件中将某个字符串的所有出现替换为“replaceWithThisNewString”。 怎么办?
【问题讨论】:
标签: bash
我需要编写一个脚本,在一个目录及其所有子目录中的所有文件中将某个字符串的所有出现替换为“replaceWithThisNewString”。 怎么办?
【问题讨论】:
标签: bash
试试这条线:
find /aDir -type f |xargs sed -i 's/certainString/replaceWithThisNewString/g'
【讨论】:
xargs 路线,为了安全起见,我会使用find .... -print0 | xargs -0 ...。
find /path/to/dir -type f -exec sed -i 's/original/replacement/g' {} \+
或者如果您的find 不支持\+:
find /path/to/dir -type f -exec sed -i 's/original/replacement/g' {} \;
【讨论】:
使用bash4:
shopt -s globstar # if not already enabled
sed -i '/certainString/s/certainString/replaceWithThisNewString/g' **
【讨论】: