• 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
  • 相关阅读:
    20145328 《信息安全系统设计基础》第6周学习总结
    20145328 《信息安全系统设计基础》第5周学习总结
    2017-2018-2 《网络对抗技术》 20155322 第二周 Exp1 PC平台逆向破解(5)M
    2017-2018-1 《信息安全系统设计基础》 20155322 十六周 课上实践
    2017-2018-1 《信息安全系统设计基础》 20155322 十六周 课下实践
    20155322 2017-2018-1《信息安全系统设计基础》课程总结
    20155322 2017-2018-1《信息安全系统设计基础》第十四周学习总结
    20155322 2017-2018-1 《信息安全系统设计基础》 第十三周学习总结
    20155322 2017-2018-1《信息安全系统设计基础》实验五-通信协议设计
    20155322 2017-2018-1《信息安全系统设计基础》实验四-外设驱动程序设计
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/15773337.html
Copyright © 2020-2023  润新知