Linux中利用find实现一次替换多个文件的内容的实现

这道面试题实现对多个文件的同一内容进行统一的替换,首先我们还是模拟环境:

[root@localhost question]# ls 
1.txt  2.txt  3.txt
[root@localhost question]# cat *.txt
123 2.txt
123 2.txt
123

三个文件都包含内容“123”,我们如何实现讲所有文件内容中的“123”替换为“successfull”

还是要通过find 命令来实现:找到所有的文件,然后通过xargs 执行sed替换

[root@localhost question]# find /root/question/ -type f -name "*.txt" |xargs  sed -i 's#123#succfull#g' 
[root@localhost question]# ls
1.txt  2.txt  3.txt
[root@localhost question]# cat *.txt
succfull 2.txt
succfull 2.txt
succfull
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章