outsec信息脱敏小技巧
sed替换文件夹内所有层级的文件内容
方法一:使用find命令配合sed命令进行替换
find /path/to/directory -type f -exec sed -i 's/old_string/new_string/g' {} +
将/path/to/directory替换为目标文件夹的路径,将old_string替换为要替换的字符串,将new_string替换为替换后的字符串。
方法二:使用find命令配合xargs命令和sed命令进行替换
find /path/to/directory -type f -print0 | xargs -0 sed -i 's/old_string/new_string/g'
将/path/to/directory替换为目标文件夹的路径,将old_string替换为要替换的字符串,将new_string替换为替换后的字符串。
方法三:使用递归替换命令(如rpl)进行替换
rpl -R 'old_string' 'new_string' /path/to/directory
将/path/to/directory替换为目标文件夹的路径,将old_string替换为要替换的字符串,将new_string替换为替换后的字符串。
最后记得,文件名和文件夹名也要改
下边函数没完全实现,可以让AI回答下可以试试 shell替换文件夹内所有层级的文件名和文件夹名中特定字符串
#!/bin/bash
# 替换函数
replace_name() {
old_string=$1
new_string=$2
find . -type f -o -type d -depth | while read file; do
if [[ $file == *"old_string"* ]]; then
newfile=$(echo "$file" | sed "s/$old_string/$new_string/g")
mv "$file" "$newfile"
fi
done
}
# 调用函数,将旧名称替换为新名称
replace_name "OldName" "NewName"
本文来自博客园,作者:ThreeFlower,转载请注明原文链接:https://www.cnblogs.com/huangjinbang1996/p/18617451

浙公网安备 33010602011771号