• linux中删除目录中指定文件外的其他文件


    1、创建测试数据

    [root@centos7 test2]# touch {1..9}.txt
    [root@centos7 test2]# ls
    1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

    2、删除3.txt、7.txt外的其他文件

    [root@centos7 test2]# ls | grep -v "3.txt"
    1.txt
    2.txt
    4.txt
    5.txt
    6.txt
    7.txt
    8.txt
    9.txt
    [root@centos7 test2]# ls | grep -v "3.txt" | grep -v "7.txt"
    1.txt
    2.txt
    4.txt
    5.txt
    6.txt
    8.txt
    9.txt
    [root@centos7 test2]# ls | grep -v "3.txt" | grep -v "7.txt" | xargs rm -f
    [root@centos7 test2]# ls
    3.txt  7.txt

    3、扩展grep

    [root@centos7 test2]# touch {1..9}.txt
    [root@centos7 test2]# ls
    1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt
    [root@centos7 test2]# ls | grep -v -E "3.txt|7.txt" | xargs rm -f
    [root@centos7 test2]# ls
    3.txt  7.txt

    4、

    [root@centos7 test2]# touch {1..9}.txt
    [root@centos7 test2]# ls
    1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt
    [root@centos7 test2]# rm -rf !(3.txt)
    [root@centos7 test2]# ls
    3.txt
    [root@centos7 test2]# touch {1..9}.txt
    [root@centos7 test2]# ls
    1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt
    [root@centos7 test2]# rm -rf !(2.txt|7.txt)
    [root@centos7 test2]# ls
    2.txt  7.txt
  • 相关阅读:
    nyoj--767--因子和(模拟)
    poj--1703--Find them, Catch them(并查集巧用)
    nyoj--1009--So Easy[Ⅰ](数学)
    nyoj--1011--So Easy[II](数学几何水题)
    nyoj--311--完全背包(动态规划,完全背包)
    morhpia(4)-更新
    morphia(5)-删除
    morphia(6-1)-查询
    redis 分页
    mongodb-安装&配置&启动
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14660914.html
Copyright © 2020-2023  润新知