rm命令删除文件时排除特定文件
# 删除当前目录下所有 *.txt文件,除了test.txt
rm `ls *.txt|egrep -v test.txt`
#或者
rm `ls *.txt|awk '{if($0 != "test.txt") print $0}'`
#排除多个文件
rm `ls *.txt|egrep -v '(test.txt|fff.txt|ppp.txt)'`
rm -f `ls *.log.1|egrep -v '(access-2010-09-06.log|error-2010-09-06.log)'`
rm -f `ls *.log|egrep -v '(access-2010-09-06.log|error-2010-09-06.log)'`
rm -f `ls *.log|egrep -v '(20100906.log)'`
注意:上面所用的符号是‘`’,而不是单引号
rm -rf
ls |egrep -v bb
删除所有,保留bb 目录
# 注意:如下这样的写法不生效,因为带有*
rm -rf `ls /tmp/test/apptf/* | egrep -v upload`
# 这样写才可以
rm -rf `ls /tmp/test/apptf/ | egrep -v upload`
# 具体演示
/tmp/test # cd apptf
/tmp/test/apptf # pwd
/tmp/test/apptf
/tmp/test/apptf # ll
总用量 8.0K
drwxrwxr-x 2 root root 41 4月 21 09:45 context
drwxrwxr-x 3 root root 22 4月 21 09:45 export
drwxrwxr-x 2 root root 89 4月 21 09:45 images
-rw-rw-r-- 1 root root 345 3月 24 17:44 index.jsp
drwxr-xr-x 3 root root 38 4月 21 09:45 META-INF
drwxrwxr-x 38 root root 4.0K 4月 21 09:45 plug-in
drwxrwxr-x 7 root root 147 4月 21 09:45 swagger
drwxrwxr-x 2 root root 25 4月 21 09:45 swftools
drwxrwxr-x 3 root root 77 4月 21 09:45 upload
drwxrwxr-x 4 root root 62 4月 21 09:45 userfiles
drwxrwxr-x 6 root root 89 4月 21 09:45 WEB-INF
drwxrwxr-x 10 root root 117 4月 21 09:45 webpage
/tmp/test/apptf # rm -rf `ls /tmp/test/apptf/* | egrep -v upload` # 这个命令执行后只删除index.jsp文件,所有的文件夹都保留了
/tmp/test/apptf # ll
总用量 4.0K
drwxrwxr-x 2 root root 41 4月 21 09:45 context
drwxrwxr-x 3 root root 22 4月 21 09:45 export
drwxr-xr-x 3 root root 38 4月 21 09:45 META-INF
drwxrwxr-x 38 root root 4.0K 4月 21 09:45 plug-in
drwxrwxr-x 7 root root 147 4月 21 09:45 swagger
drwxrwxr-x 2 root root 25 4月 21 09:45 swftools
drwxrwxr-x 3 root root 77 4月 21 09:45 upload
drwxrwxr-x 4 root root 62 4月 21 09:45 userfiles
drwxrwxr-x 6 root root 89 4月 21 09:45 WEB-INF
drwxrwxr-x 10 root root 117 4月 21 09:45 webpage
/tmp/test/apptf # rm -rf `ls /tmp/test/apptf/ | egrep -v upload`
/tmp/test/apptf # ll
总用量 0
drwxrwxr-x 3 root root 77 4月 21 09:45 upload
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!