Linux中尋找指定類型檔案以及刪除例子

來源:互聯網
上載者:User

在Linux伺服器上運行Weblogic伺服器,每次需要關閉Weblogic伺服器後,都會在domain下留下一些*.lok的檔案,導致下次啟動的時候因為這些檔案無法啟動,因此,需要刪除產生的所有.lok檔案。
手動一個一個的刪除比較麻煩,需要domain的每個目錄都進去看看有沒有,有的話刪除。
可以通過執行下面的命令查看有哪些這樣的檔案和大量刪除它們。

find ./ -name "*.lok"   // 尋找檔案

find ./ -name "*.lok" |xargs rm -fr  // 尋找檔案並刪除

1. find -name '*lck*' -exec rm {} \;

尋找當前檔案夾及其子檔案夾下所有檔案名稱中帶有『lck』字元的檔案並刪除之。需要注意的是find -name *lck*,也就是沒有加單引號,則只搜尋目前的目錄下的檔案而不會搜尋子檔案夾內的檔案。

順便列一下find的相關使用

1. find   /  -name test  | xargs rm -rf      (這個命令可以尋找test檔案或者目錄,並刪除!)

2. 用下面的命令可以尋找 /home下最近兩天修改過的檔案:find /home -type f -mtime -2

如果要把這些檔案也刪掉,那麼可以:find /home -type f -mtime -2 -exec rm {} \;

-type f  尋找檔案
-type d 尋找目錄

-mtime -2 修改時間在2天內
-mtime +3 修改時間在3天前

-exec rm {} \;   將找到的檔案 (假定找到檔案的名字為 a.txt), 執行 rm a.txt 命令

find有很多參數,find一次尋找多個指定檔案或者排除某類檔案,在 GREP 中匹配多個關鍵字的方法

(1)Linux下find一次尋找多個指定檔案:

尋找a.html和b.html
find . -name "a.html"  -name "b.html" 

find . -regex '.*\.txt\|.*\.doc\|.*\.mp3'
find . -regex '.*\.txt\|.*\.doc\|.*\.mp3' 
./a.txt 
./a.doc 
./a.mp3 

(2)排除某些檔案類型:

排除目錄下所有以html結尾的檔案:
find . -type f ! -name "*.html"   

find . -type f ! -name "*.html"      
./ge.bak.02.09 
./ge.html.changed.by.jack 
./a.txt 
./a.doc 
./a.mp3 

(3)排除多種檔案類型的樣本:

find . -type f ! -name "*.html" -type  f ! -name "*.php" -type  f ! -name "*.svn-base"  -type  f ! -name "*.js"  -type  f ! -name "*.gif"  -type  f ! -name "*.png"  -type  f ! -name "*.cpp"  -type  f ! -name "*.h"  -type  f ! -name "*.o"  -type  f ! -name "*.jpg"  -type  f ! -name "*.so"  -type  f ! -name "*.bak"  -type  f ! -name "*.log"  

(3)在 GREP 中匹配多個關鍵字的方法:

grep尋找多個數位檔案:

-r 遞迴,-E:正則  -l:只顯示檔案名稱

root@116.255.139.240:~/a# grep -r -E '0341028|100081|10086|10001' * 
a.txt:100081 
b.txt:10086 
c/cc.txt:0341028 
c/cc.txt:100081 
c/cc.txt:10086 
c/cc.txt:10001 
c.txt:10001 
d.txt:0341028 

grep -r  -E -l '0341028|100081|10086|10001' *    
a.txt 
b.txt 
c/cc.txt 
c.txt 
d.txt 

多種類型檔案樣本:

find . -name "*.html" -o -name "*.js"|xargs grep -r "BusiTree"  

用Awk:

find . -name "*.php"|awk '{print "cat " $0 " |grep -H dbsys. www.111cn.net"}'|sh

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.