这道面试题实现对多个文件的同一内容进行统一的替换,首先我们还是模拟环境:
[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