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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章