這道面試題實現對多個文件的同一內容進行統一的替換,首先我們還是模擬環境:
[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