rm命令在删除文件时排除指定文件
准备测试环境[root@zw ~]# cd[root@zw ~]# mkdir tmp[root@zw ~]# touch ./tmp/test{1..10}[root@zw ~]# cd tmp/[root@zw tmp]# lltotal 0-rw-r--r-- 1 root root 0 Jan4 15:36 test1-rw-r--r-- 1 root root 0 Jan4 15:36
·
准备测试环境
[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]#
更多推荐
已为社区贡献1条内容
所有评论(0)