欢迎来到资源库(www.zyku.net)

Linux

当前位置:首页 > 服务器教程 > Linux > rm命令

rm命令在删除文件时排除指定文件

时间:2022-09-17|栏目:Linux|点击:|我要投稿

准备测试环境

[root@zw ~]# cd
[root@zw ~]# mkdir tmp
[root@zw ~]# touch ./tmp/test{1..10}
[root@zw ~]# cd tmp/
[root@zw tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Jan  4 15:36 test1
-rw-r--r-- 1 root root 0 Jan  4 15:36 test10
-rw-r--r-- 1 root root 0 Jan  4 15:36 test2
-rw-r--r-- 1 root root 0 Jan  4 15:36 test3
-rw-r--r-- 1 root root 0 Jan  4 15:36 test4
-rw-r--r-- 1 root root 0 Jan  4 15:36 test5
-rw-r--r-- 1 root root 0 Jan  4 15:36 test6
-rw-r--r-- 1 root root 0 Jan  4 15:36 test7
-rw-r--r-- 1 root root 0 Jan  4 15:36 test8
-rw-r--r-- 1 root root 0 Jan  4 15:36 test9
[root@zw tmp]# 

方法一: ls

rm -rf `ls | grep -v test10`
[root@zw tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Jan  4 15:36 test1
-rw-r--r-- 1 root root 0 Jan  4 15:36 test10
-rw-r--r-- 1 root root 0 Jan  4 15:36 test2
-rw-r--r-- 1 root root 0 Jan  4 15:36 test3
-rw-r--r-- 1 root root 0 Jan  4 15:36 test4
-rw-r--r-- 1 root root 0 Jan  4 15:36 test5
-rw-r--r-- 1 root root 0 Jan  4 15:36 test6
-rw-r--r-- 1 root root 0 Jan  4 15:36 test7
-rw-r--r-- 1 root root 0 Jan  4 15:36 test8
-rw-r--r-- 1 root root 0 Jan  4 15:36 test9
[root@zw tmp]# rm -rf `ls | grep -v test10`
[root@zw tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Jan  4 15:36 test10
[root@zw tmp]# 

方法二: 开启bash的extglob功能(此功能的作用就是用rm !(*jpg)这样的方式来删除不包括号内文件的文件)

[root@zw tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Jan  4 15:44 test1
-rw-r--r-- 1 root root 0 Jan  4 15:44 test10
-rw-r--r-- 1 root root 0 Jan  4 15:44 test2
-rw-r--r-- 1 root root 0 Jan  4 15:44 test3
-rw-r--r-- 1 root root 0 Jan  4 15:44 test4
-rw-r--r-- 1 root root 0 Jan  4 15:44 test5
-rw-r--r-- 1 root root 0 Jan  4 15:44 test6
-rw-r--r-- 1 root root 0 Jan  4 15:44 test7
-rw-r--r-- 1 root root 0 Jan  4 15:44 test8
-rw-r--r-- 1 root root 0 Jan  4 15:44 test9
[root@zw tmp]# shopt -s extglob
[root@zw tmp]#  rm -f !(test10)
[root@zw tmp]# ls
test10
[root@zw tmp]# 

方法三:shell

[root@zw tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Jan  4 16:14 test1
-rw-r--r-- 1 root root 0 Jan  4 16:14 test10
-rw-r--r-- 1 root root 0 Jan  4 16:14 test2
-rw-r--r-- 1 root root 0 Jan  4 16:14 test3
-rw-r--r-- 1 root root 0 Jan  4 16:14 test4
-rw-r--r-- 1 root root 0 Jan  4 16:14 test5
-rw-r--r-- 1 root root 0 Jan  4 16:14 test6
-rw-r--r-- 1 root root 0 Jan  4 16:14 test7
-rw-r--r-- 1 root root 0 Jan  4 16:14 test8
-rw-r--r-- 1 root root 0 Jan  4 16:14 test9
[root@zw tmp]# for i in `ls`;do if [ "$i" != test10 ];then rm -rf $i;fi;done;
[root@zw tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Jan  4 16:14 test10
[root@zw tmp]# 

方法四:find

find ./ -type f | grep -v “test10” | xargs rm -f
[root@zw tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Jan  4 16:18 test1
-rw-r--r-- 1 root root 0 Jan  4 16:18 test10
-rw-r--r-- 1 root root 0 Jan  4 16:18 test2
-rw-r--r-- 1 root root 0 Jan  4 16:18 test3
-rw-r--r-- 1 root root 0 Jan  4 16:18 test4
-rw-r--r-- 1 root root 0 Jan  4 16:18 test5
-rw-r--r-- 1 root root 0 Jan  4 16:18 test6
-rw-r--r-- 1 root root 0 Jan  4 16:18 test7
-rw-r--r-- 1 root root 0 Jan  4 16:18 test8
-rw-r--r-- 1 root root 0 Jan  4 16:18 test9
[root@zw tmp]# find ./ -type f | grep -v "test10" | xargs rm -f
[root@zw tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Jan  4 16:18 test10
[root@zw tmp]# 
find ./ -type f ! -name “test10” | xargs rm -f
[root@zw tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Jan  4 16:20 test1
-rw-r--r-- 1 root root 0 Jan  4 16:20 test10
-rw-r--r-- 1 root root 0 Jan  4 16:20 test2
-rw-r--r-- 1 root root 0 Jan  4 16:20 test3
-rw-r--r-- 1 root root 0 Jan  4 16:20 test4
-rw-r--r-- 1 root root 0 Jan  4 16:20 test5
-rw-r--r-- 1 root root 0 Jan  4 16:20 test6
-rw-r--r-- 1 root root 0 Jan  4 16:20 test7
-rw-r--r-- 1 root root 0 Jan  4 16:20 test8
-rw-r--r-- 1 root root 0 Jan  4 16:20 test9
[root@zw tmp]#  find ./ -type f ! -name "test10"|xargs rm -f 
[root@zw tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Jan  4 16:20 test10
[root@zw tmp]# 
find ./ -type f -not -name “test10” | xargs rm -rf
[root@zw tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Jan  4 17:34 test1
-rw-r--r-- 1 root root 0 Jan  4 17:34 test10
-rw-r--r-- 1 root root 0 Jan  4 17:34 test2
-rw-r--r-- 1 root root 0 Jan  4 17:34 test3
-rw-r--r-- 1 root root 0 Jan  4 17:34 test4
-rw-r--r-- 1 root root 0 Jan  4 17:34 test5
-rw-r--r-- 1 root root 0 Jan  4 17:34 test6
-rw-r--r-- 1 root root 0 Jan  4 17:34 test7
-rw-r--r-- 1 root root 0 Jan  4 17:34 test8
-rw-r--r-- 1 root root 0 Jan  4 17:34 test9
[root@zw tmp]# find ./ -type f -not -name "test10" | xargs rm -rf
[root@zw tmp]# ll
find ./ -type f ! -name “test10” -exec rm -f {} ;
[root@zw tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Jan  4 21:42 test1
-rw-r--r-- 1 root root 0 Jan  4 21:42 test10
-rw-r--r-- 1 root root 0 Jan  4 21:42 test2
-rw-r--r-- 1 root root 0 Jan  4 21:42 test3
-rw-r--r-- 1 root root 0 Jan  4 21:42 test4
-rw-r--r-- 1 root root 0 Jan  4 21:42 test5
-rw-r--r-- 1 root root 0 Jan  4 21:42 test6
-rw-r--r-- 1 root root 0 Jan  4 21:42 test7
-rw-r--r-- 1 root root 0 Jan  4 21:42 test8
-rw-r--r-- 1 root root 0 Jan  4 21:42 test9
[root@zw tmp]# find ./ -type f ! -name test10 -exec rm -f {} \;
[root@zw tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Jan  4 21:42 test10
[root@zw tmp]# 
find ./ -type f -not -name “test10” -exec rm -rf {} ;
 [root@zw tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Jan  4 21:43 test1
-rw-r--r-- 1 root root 0 Jan  4 21:43 test10
-rw-r--r-- 1 root root 0 Jan  4 21:43 test2
-rw-r--r-- 1 root root 0 Jan  4 21:43 test3
-rw-r--r-- 1 root root 0 Jan  4 21:43 test4
-rw-r--r-- 1 root root 0 Jan  4 21:43 test5
-rw-r--r-- 1 root root 0 Jan  4 21:43 test6
-rw-r--r-- 1 root root 0 Jan  4 21:43 test7
-rw-r--r-- 1 root root 0 Jan  4 21:43 test8
-rw-r--r-- 1 root root 0 Jan  4 21:43 test9
[root@zw tmp]# find ./ -type f -not -name test10 -exec rm -f {} \;
[root@zw tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Jan  4 21:43 test10
[root@zw tmp]# 

方法五: rsync

[root@zw tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Jan  4 15:58 test1
-rw-r--r-- 1 root root 0 Jan  4 15:58 test10
-rw-r--r-- 1 root root 0 Jan  4 15:58 test2
-rw-r--r-- 1 root root 0 Jan  4 15:58 test3
-rw-r--r-- 1 root root 0 Jan  4 15:58 test4
-rw-r--r-- 1 root root 0 Jan  4 15:58 test5
-rw-r--r-- 1 root root 0 Jan  4 15:58 test6
-rw-r--r-- 1 root root 0 Jan  4 15:58 test7
-rw-r--r-- 1 root root 0 Jan  4 15:58 test8
-rw-r--r-- 1 root root 0 Jan  4 15:58 test9
[root@zw tmp]# rm -rf /null/
[root@zw tmp]# mkdir /null && rsync -az --delete --exclude "test10" /null/ /root/tmp 
[root@zw tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Jan  4 15:58 test10
[root@zw tmp]# 

(资源库 www.zyku.net)

原文链接:https://blog.csdn.net/weixin_47194244/article/details/122304381

上一篇:linux过滤空格、linux过滤注释行(^#)、linux过滤空白行(^&)

栏    目:Linux

下一篇:shell中&&和||的用法

本文标题:rm命令在删除文件时排除指定文件

本文地址:https://www.zyku.net/linux/12097.html

关于我们 | 版权申明 | 寻求合作 |

重要申明:本站所有的文章、图片、评论等内容,均由网友发表或上传并维护或收集自网络,仅供个人学习交流使用,版权归原作者所有。

如有侵犯您的版权,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:95148658 | 邮箱:mb8#qq.com(#换成@)

苏ICP备2020066115号-1

本网站由提供CDN加速/云存储服务