例如所在目录为/abc,替换目标为其内部所有文件(含所有子目录).
替换目录下所有文件的所有字符串:
例如:http替换为helloWold,命令:
# cd /abc && beforeName="http";afterName="helloWorld";sed -i "s/$beforeName/$afterName/g" `grep "$beforeName" -rl ./`;
替换文件名:
例如:某目录下所有含有http的文件或目录均换为helloWold,命令:
# cd /abc && beforeName="http";afterName="helloWorld";for file in `find -name "*$beforeName*" -type d ` ;do newfile=`echo $file | sed "s/$beforeName/$afterName/g"`;mv $file $newfile;done;for file in `find -name "*$beforeName*" -type f ` ;do newfile=`echo $file | sed "s/$beforeName/$afterName/g"`;mv $file $newfile;done;
本文介绍了如何在Linux的CentOS环境下批量替换目录下所有文件的内容,包括特定字符串的删除和替换,以及根据特定字符串批量更改文件名和目录名称的方法。
订阅专栏 解锁全文
1130

被折叠的 条评论
为什么被折叠?



