rm命令如何在删除文件时排除忽略某特定文件

本文介绍了在Linux系统中使用rm命令删除文件时,如何排除特定文件的六种方法,包括使用find命令结合not-name选项、rsync工具、bash的extglob功能、grep过滤等,为用户提供灵活的文件管理方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

rm命令如何在删除文件时排除忽略某特定文件
[root@zc zc]# touch test{1..10}
[root@zc zc]# ls
test1  test10  test2  test3  test4  test5  test6  test7  test8  test9

方法一:find
[root@zc zc]# ls
test1  test10  test2  test3  test4  test5  test6  test7  test8  test9
[root@zc zc]# find /zc -type f ! -name "test10"|xargs rm -f 
[root@zc zc]# ls
test10
[root@zc zc]# find /zc -type f ! -name "test10" -exec rm -f {} \;     
[root@zc zc]# ls
test10
[root@zc zc]# find /zc -type f -not -name "test10" -exec rm -rf {} \;
[root@zc zc]# ls
test10
[root@zc zc]# find /zc -type f -not -name "test10" | xargs rm -rf
[root@zc zc]# ls
test10

方法二:rsync
[root@zc zc]# ls
test1  test10  test2  test3  test4  test5  test6  test7  test8  test9
[root@zc zc]# mkdir /null && rsync -az --delete --exclude "test10" /null/ /zc/
[root@zc zc]# ls
test10

方法三:开启bash的extglob功能(此功能的作用就是用rm !(*jpg)这样的方式来删除不包括号内文件的文件)
[root@zc zc]# shopt -s extglob
[root@zc zc]# ls
test1  test10  test2  test3  test4  test5  test6  test7  test8  test9
[root@zc zc]# rm -f !(test10)
[root@zc zc]# ls
test10

方法四:
[root@zc zc]# find ./ -type f|grep -v "test10"|xargs rm -f
[root@zc zc]# ls
test10

方法五:
[root@zc zc]# rm -f `ls|grep -v "test10"`
[root@zc zc]# ls
test10

方法六: shell
[root@zc zc] for i in `ls`;do if [ "$i" != test10 ];then rm -rf $i;fi;done;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值