使用sed命令进行替换:
sed -i 's/old_string/new_string/g' /path/to/directory/*
使用find命令结合sed进行替换:
find /path/to/directory -type f -exec sed -i 's/old_string/new_string/g' {} +
使用grep命令找到包含指定字符串的文件,再使用sed进行替换:
grep -rl 'old_string' /path/to/directory | xargs sed -i 's/old_string/new_string/g'
使用awk命令进行替换:
awk '{gsub(/old_string/, "new_string"); print > FILENAME}' /path/to/directory/*
使用perl命令进行替换:
perl -pi -e 's/old_string/new_string/g' /path/to/directory/*
使用Python脚本进行替换:
import os def replace_string(path, old_string, new_string): for root, dirs, files in os.walk(path): for file in files: file_path = os.path.join(root, file) with open(file_path, 'r') as f: content = f.read() content = content.replace(old_string, new_string) with open(file_path, 'w') as f: f.write(content) replace_string('/path/to/directory', 'old_string', 'new_string')
linux查找指定目录下包含指定字符串文件,包含子目录
2024-01-01 11:58:02 16 阅读