V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
station
V2EX  ›  Linux

rm 命令如何保留某个或多个文件?

  •  1
     
  •   station · 2016-05-19 09:08:49 +08:00 · 5101 次点击
    这是一个创建于 2921 天前的主题,其中的信息可能已经有所发展或是发生改变。
    网上盛传 rm -rf !(文件名)这种参数,但我这里总提醒我 -bash: !: event not found
    12 条回复    2016-05-19 16:46:12 +08:00
    ToughGuy
        1
    ToughGuy  
       2016-05-19 09:20:32 +08:00
    先执行这个:
    shopt -s extglob

    然后再试

    rm -rf !(文件名)
    clino
        2
    clino  
       2016-05-19 09:21:06 +08:00
    你可以用 find .|grep xxx|xargs rm -rfv
    这样在 grep 这里可以用正则表达式之类的过滤
    station
        3
    station  
    OP
       2016-05-19 09:46:40 +08:00
    @ToughGuy 谢谢,搜索了原来是 extglob 模式没开
    station
        4
    station  
    OP
       2016-05-19 09:50:04 +08:00
    @clino 没试,还是感谢你
    ffffwh
        5
    ffffwh  
       2016-05-19 10:55:54 +08:00 via Android
    rm 一时爽
    station
        6
    station  
    OP
       2016-05-19 11:01:22 +08:00
    @ffffwh 此话怎讲
    ffffwh
        7
    ffffwh  
       2016-05-19 11:06:59 +08:00 via Android
    @station
    就是说容易误删文件
    gqkkk
        8
    gqkkk  
       2016-05-19 11:35:50 +08:00
    find 条件|xargs rm
    qwerasdf
        9
    qwerasdf  
       2016-05-19 11:38:55 +08:00
    删除 the/dir 目录下的多个指定文件
    rm `ls -a the/dir grep | xxx`
    xiaoz
        10
    xiaoz  
       2016-05-19 12:27:40 +08:00 via iPhone
    cxbig
        11
    cxbig  
       2016-05-19 15:06:33 +08:00
    建议用 find 出来结果再用 rm ,容易把握。
    reticentfat
        12
    reticentfat  
       2016-05-19 16:46:12 +08:00
    rm -f `ls *.*|egrep -v "(a.bbb|*.ccc)"`
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   892 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:51 · PVG 04:51 · LAX 13:51 · JFK 16:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.