zoukankan      html  css  js  c++  java
  • 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
    
  • 相关阅读:
    WPF换肤之八:创建3D浏览效果
    ADPlus
    由INotifyPropertyChanged,BindingList绑定引发的跨线程异常及其解决办法
    无服务器端的UDP群聊功能剖析(重构版本)
    A Short Guide to DBI[转]
    绑定到异步的ObservableCollection
    使用反射+缓存+委托,实现一个不同对象之间同名同类型属性值的快速拷贝
    ORM查询语言(OQL)简介概念篇
    无需重新编译代码,在线修改表单
    LJMM平台( Linux +Jexus+MySQL+mono) 上使用MySQL的简单总结
  • 原文地址:https://www.cnblogs.com/sanduzxcvbnm/p/12743097.html
Copyright © 2011-2022 走看看