find and replace string in linux
# 预览结果
find . -name '*.py' -exec sed -i 's/"w") as f:/"w", encoding="utf-8") as f:/g' {} +
find . -name '*.py' -exec sed -i 's/"w") as f:/"w", encoding="utf-8") as f:/g' {} +
find /path/to/directory -type f -exec sed -i 's/old_string/new_string/g' {} +
/path/to/directory:要替换的文件夹路径。
-type f:只查找文件。
-exec:对找到的文件执行后面的命令。 sed -i ‘s/old_string/new_string/g’:使用 sed 替换文件中的 old_string 为 new_string,-i 表示直接修改文件。
# or
find /path/to/directory -type f -exec perl -pi -e 's/old_string/new_string/g' {} +
# or
grep -rl 'old_string' /path/to/directory | xargs sed -i 's/old_string/new_string/g'