linux 中删除当前目录下指定文件外所有的文件
2022/1/7 7:03:27
本文主要是介绍linux 中删除当前目录下指定文件外所有的文件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1、ls + grep + xargs 实现
a、
root@DESKTOP-1N42TVH:/home/test# touch test{1..10}.txt root@DESKTOP-1N42TVH:/home/test# touch abc{1..10}.csv root@DESKTOP-1N42TVH:/home/test# touch kk.map mn.ped root@DESKTOP-1N42TVH:/home/test# ls abc1.csv abc2.csv abc4.csv abc6.csv abc8.csv kk.map test1.txt test2.txt test4.txt test6.txt test8.txt abc10.csv abc3.csv abc5.csv abc7.csv abc9.csv mn.ped test10.txt test3.txt test5.txt test7.txt test9.txt root@DESKTOP-1N42TVH:/home/test# ls | grep -v "kk.map" abc1.csv abc10.csv abc2.csv abc3.csv abc4.csv abc5.csv abc6.csv abc7.csv abc8.csv abc9.csv mn.ped test1.txt test10.txt test2.txt test3.txt test4.txt test5.txt test6.txt test7.txt test8.txt test9.txt root@DESKTOP-1N42TVH:/home/test# ls | grep -v "kk.map" | xargs rm -rf ## 删除除kk.map外以外的所有文件 root@DESKTOP-1N42TVH:/home/test# ls kk.map
b、
root@DESKTOP-1N42TVH:/home/test# ls root@DESKTOP-1N42TVH:/home/test# touch test{1..10}.txt root@DESKTOP-1N42TVH:/home/test# touch abc{1..10}.csv root@DESKTOP-1N42TVH:/home/test# touch kk.map mn.ped root@DESKTOP-1N42TVH:/home/test# ls abc1.csv abc2.csv abc4.csv abc6.csv abc8.csv kk.map test1.txt test2.txt test4.txt test6.txt test8.txt abc10.csv abc3.csv abc5.csv abc7.csv abc9.csv mn.ped test10.txt test3.txt test5.txt test7.txt test9.txt root@DESKTOP-1N42TVH:/home/test# ls | grep -E -v "kk.map|mn.ped" abc1.csv abc10.csv abc2.csv abc3.csv abc4.csv abc5.csv abc6.csv abc7.csv abc8.csv abc9.csv test1.txt test10.txt test2.txt test3.txt test4.txt test5.txt test6.txt test7.txt test8.txt test9.txt root@DESKTOP-1N42TVH:/home/test# ls | grep -E -v "kk.map|mn.ped" | xargs rm -rf ## 删除除kk.map和mn.ped以外的所有文件 root@DESKTOP-1N42TVH:/home/test# ls kk.map mn.ped
2、find实现
a、
root@DESKTOP-1N42TVH:/home/test# ls root@DESKTOP-1N42TVH:/home/test# touch abc{1..10}.csv root@DESKTOP-1N42TVH:/home/test# touch kk.map mn.ped root@DESKTOP-1N42TVH:/home/test# touch test{1..10}.txt root@DESKTOP-1N42TVH:/home/test# ls abc1.csv abc2.csv abc4.csv abc6.csv abc8.csv kk.map test1.txt test2.txt test4.txt test6.txt test8.txt abc10.csv abc3.csv abc5.csv abc7.csv abc9.csv mn.ped test10.txt test3.txt test5.txt test7.txt test9.txt root@DESKTOP-1N42TVH:/home/test# find ./ -name "kk.map" ./kk.map root@DESKTOP-1N42TVH:/home/test# find ./ ! -name "kk.map" ./ ./test8.txt ./abc1.csv ./abc8.csv ./abc3.csv ./test6.txt ./test10.txt ./test2.txt ./abc10.csv ./test1.txt ./test9.txt ./test5.txt ./abc7.csv ./test7.txt ./abc2.csv ./abc6.csv ./abc4.csv ./test3.txt ./abc5.csv ./abc9.csv ./mn.ped ./test4.txt root@DESKTOP-1N42TVH:/home/test# find ./ ! -name "kk.map" -exec rm -rf {} \; #删除除kk.map文件以外的所有文件 rm: refusing to remove '.' or '..' directory: skipping './' root@DESKTOP-1N42TVH:/home/test# ls kk.map
b、
root@DESKTOP-1N42TVH:/home/test# touch kk.map mn.ped root@DESKTOP-1N42TVH:/home/test# touch test{1..10}.txt root@DESKTOP-1N42TVH:/home/test# touch abc{1..10}.csv root@DESKTOP-1N42TVH:/home/test# ls abc1.csv abc2.csv abc4.csv abc6.csv abc8.csv kk.map test1.txt test2.txt test4.txt test6.txt test8.txt abc10.csv abc3.csv abc5.csv abc7.csv abc9.csv mn.ped test10.txt test3.txt test5.txt test7.txt test9.txt root@DESKTOP-1N42TVH:/home/test# find ./ ! -name "kk.map" ! -name "mn.ped" ./ ./test8.txt ./abc1.csv ./abc8.csv ./abc3.csv ./test6.txt ./test10.txt ./test2.txt ./abc10.csv ./test1.txt ./test9.txt ./test5.txt ./abc7.csv ./test7.txt ./abc2.csv ./abc6.csv ./abc4.csv ./test3.txt ./abc5.csv ./abc9.csv ./test4.txt root@DESKTOP-1N42TVH:/home/test# find ./ ! -name "kk.map" ! -name "mn.ped" -exec rm -rf {} \; ## 删除除kk.map和mn.ped以外的所有文件 rm: refusing to remove '.' or '..' directory: skipping './' root@DESKTOP-1N42TVH:/home/test# ls kk.map mn.ped
3、rm !实现
a、
root@DESKTOP-1N42TVH:/home/test# touch kk.map mn.ped root@DESKTOP-1N42TVH:/home/test# touch test{1..10}.txt root@DESKTOP-1N42TVH:/home/test# touch abc{1..10}.csv root@DESKTOP-1N42TVH:/home/test# ls abc1.csv abc2.csv abc4.csv abc6.csv abc8.csv kk.map test1.txt test2.txt test4.txt test6.txt test8.txt abc10.csv abc3.csv abc5.csv abc7.csv abc9.csv mn.ped test10.txt test3.txt test5.txt test7.txt test9.txt root@DESKTOP-1N42TVH:/home/test# rm !(kk.map) ## 删除除kk.map文件以外的所有文件 root@DESKTOP-1N42TVH:/home/test# ls kk.map
b、
root@DESKTOP-1N42TVH:/home/test# touch kk.map mn.ped root@DESKTOP-1N42TVH:/home/test# touch test{1..10}.txt root@DESKTOP-1N42TVH:/home/test# touch abc{1..10}.csv root@DESKTOP-1N42TVH:/home/test# ls abc1.csv abc2.csv abc4.csv abc6.csv abc8.csv kk.map test1.txt test2.txt test4.txt test6.txt test8.txt abc10.csv abc3.csv abc5.csv abc7.csv abc9.csv mn.ped test10.txt test3.txt test5.txt test7.txt test9.txt root@DESKTOP-1N42TVH:/home/test# rm !(kk.map|mn.ped) ## 删除除kk.map和mn.ped以外的所有文件 root@DESKTOP-1N42TVH:/home/test# ls kk.map mn.ped
这篇关于linux 中删除当前目录下指定文件外所有的文件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-12如何创建可引导的 ESXi USB 安装介质 (macOS, Linux, Windows)
- 2024-11-08linux的 vi编辑器中搜索关键字有哪些常用的命令和技巧?-icode9专业技术文章分享
- 2024-11-08在 Linux 的 vi 或 vim 编辑器中什么命令可以直接跳到文件的结尾?-icode9专业技术文章分享
- 2024-10-22原生鸿蒙操作系统HarmonyOS NEXT(HarmonyOS 5)正式发布
- 2024-10-18操作系统入门教程:新手必看的基本操作指南
- 2024-10-18初学者必看:操作系统入门全攻略
- 2024-10-17操作系统入门教程:轻松掌握操作系统基础知识
- 2024-09-11Linux部署Scrapy学习:入门级指南
- 2024-09-11Linux部署Scrapy:入门级指南
- 2024-08-21【Linux】分区向左扩容的方法