rm命令在删除文件时排除指定文件
准备测试环境
[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
您可能感兴趣的文章
- 07-05Linux rm命令
- 07-05Linux colrm命令
- 07-05Linux lprm命令
- 07-05Linux hdparm命令
- 07-05Linux testparm命令
- 11-14Windows系统cmd删除文件命令del/erase和删除目录命令r
- 11-14Windows常见bat批处理命令del/rd清理删除文件以及文件
- 02-21Linux中删除文件功能详解
- 02-19如何安装PHPCMS网站管理系统
- 03-17Swiper手动滑动之后自动轮播失效的解
- 03-11让IIS支持mp4等视频播放的设置方法
- 07-08oppo手机电池显示百分比设置步骤介绍
- 11-23ubuntu 14.04 和16.04系统镜像下载源
- 12-25跟练健身视频-跟练健身视频应用软件功
- 09-28腾讯视频防沉迷模式在哪里
- 03-04wordpress实现文章部分内容登陆后可见
- 04-10淘宝修改账号用户名操作方法
- 01-13兼职招工作帮手-兼职招工作帮手应用软
- 07-20MySQL常用命令整理
- 01-12调味相机-调味相机应用软件功能介绍

最近更新
阅读排行
猜你喜欢
- 04-28oppo手机充电提示音设置方法
- 01-11快速清理手机垃圾-快速清理手机垃圾应
- 01-09Symbolab函数-Symbolab函数应用软件功
- 02-23ipad还原出厂设置教程
- 11-17网易云音乐怎么修改个人状态
- 02-21DedeCMS隐藏模板路径防止模板被盗的几
- 10-08vivoy52s如何设置导航键
- 01-13尊美星空-尊美星空应用软件功能介绍
- 02-20一加9相机高像素模式开启教程
- 04-08小米11ultra开启显示实时网速设置方法