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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM