把扩展名为.XX结尾的文件中内容包含name字符串替换为name1与删除/tmp/xusx/下除passwd以外的其他文件

1、替换文本内容name为name2

[root@localhost a]# touch 1.txt 2.txt 3.txt
[root@localhost a]# echo "ni hao">1.txt
[root@localhost a]# echo "ni hao">2.txt
[root@localhost a]# echo "ni hao">3.txt

[root@localhost a]# find ./ -type f -name "*.txt"|xargs sed 's#ni hao# xiexie#g'
xiexie
xiexie
xiexie

注意sed -i 是永久改变内容。没加-i是临时查看改变后效果。

#####################################################################################################

2、删除/tmp/xusx/下除passwd以外的其他文件:

方法一:

[root@localhost xusx]# ls
1.txt 2.txt 3.txt a passwd passwd1 test.txt xusx.txt
[root@localhost xusx]# ls |grep -v "passwd"|xargs rm -f    ===>###(不是特别好)
rm: 无法删除 “a”: 是一个目录
[root@localhost xusx]# ls
a passwd passwd1

方法二:

[root@localhost a]# ls
1.sh 1.txt 2.sh 2.txt 3.sh 3.txt passwd passwd1 passwd2
[root@localhost a]# find ./ -type f ! -name "passwd"|xargs rm -f
[root@localhost a]# ls
passwd

原文地址:https://www.cnblogs.com/xusx/p/6040388.html