1. 程式人生 > >Linux find 檔案搜尋命令

Linux find 檔案搜尋命令

Linux find檔案搜尋命令

用於在指定目錄下查詢檔案。任何位於引數之前的字串都將被視為欲查詢的目錄名。如果使用該命令時,不設定任何引數,則 find . 將在當前目錄下查詢子目錄與檔案,並且將查詢到的子目錄和檔案全部進行顯示。


基本語法

find path args
path:如果 path 是空字串則使用目前路徑
args:如果 args 為空則使用 -print 為預設args


搜尋通道(args說明)

  1. 根據檔案型別(# find . -type 型別引數)

    rgs 說明
    -type d 目錄(directory)
    -type c 字型裝置檔案(character)
    -type b 區塊裝置檔案(block)
    -type p 管道(pipe)
    -type f 普通檔案(file)
    -type l 符號連結(link)
    -type s 套接字(socket)

    # find . -type f 將目前目錄的子目錄中所有一般檔案列出 ,find後面的“.”號表示在當前資料夾進行搜尋
    # find . -type d列出當前目錄的子目錄

  2. 根據時間戳(# find . -type f 時間戳)
    UNIX/Linux檔案系統每個檔案都有三種時間戳
    訪問時間 access-atime/天,-amin/分鐘):使用者最近一次訪問時間。
    修改時間 modify-mtime/天,-mmin/分鐘):檔案最後一次修改時間。
    變化時間 change

    -ctime/天,-cmin/分鐘):檔案資料元(例如許可權等)最後一次修改時間。

    find . -type f -atime -7 搜尋最近七天內被訪問過的所有檔案
    find . -type f -atime 7搜尋恰好在七天前被訪問過的所有檔案
    find . -type f -atime +7 搜尋七天以上被訪問過的所有檔案
    find . -type f -amin +10 搜尋訪問時間超過10分鐘的所有檔案
    find . -ctime -7 查詢目錄及其子目錄下所有最近 7天內更新過的檔案
    find /usr/bin -type f -mtime -7 查詢/usr/bin目錄中更改時間在7日以內的普通檔案
    find . -type f -anewer file.log 找出比file.log更晚被讀取過的檔案
    find . -type f -cnewer file.log 找出比file.log更新的檔案

  3. 根據檔案大小(# find . -type f -size 檔案大小)

    args 說明
    -size b 塊(block)(512位元組)
    -size c 位元組(character)
    -size w 字(word)(2位元組)
    -size k 千位元組(kilobyte)
    -size M 兆位元組(megabyte)
    -size G 吉位元組/千兆位元組(gigabyte)

    find . -type f -size +10k 搜尋大於10KB的檔案
    find . -type f -size -10k 搜尋小於10KB的檔案
    find . -type f -size 10k 搜尋等於10KB的檔案
    find . -type f -size +10M 搜尋大於10M的檔案

  4. 根據檔案的名稱字串長度或正則表示式(-name)
    find . -empty 查詢當前資料夾中長度為0的檔案(空檔案)
    find . -name "????" 查詢長度為4的檔案,包括資料夾一併顯示出來
    find . -type f -name "???.txt" 查詢所有名稱為3個長度,並以 .txt 結尾的一般檔案,不包括資料夾
    find . -type d -name "??" 查詢所有名稱為2個長度的資料夾目錄,不包括一般檔案
    find . -name name/-iname name 檔名稱符合 name 的檔案,包括資料夾(iname 會忽略大小寫)
    find /home -name "*.txt" 在/home目錄下查詢以.txt結尾的檔名
    find /home -iname(ignore name) "*.txt" 同上,但忽略大小寫
    find /etc -type f -name init 查詢 etc 路徑下的名為 init 一般檔案
    find /etc -name *init 查詢etc路徑下所有以 init 結尾的檔案和資料夾(* 表示從結尾倒數開始匹配,包含指定字元的內容,*init表示從結尾開始匹配,包含init的,也即以 init 結尾的檔案)
    find /etc -name ??ev 查詢 etc 路徑下,長度為4,並且包含ev的檔案
    find . -name "*.txt" 查詢當前目錄及其子目錄下所有副檔名是 .txt 的檔案(引號表示匹配字串)
    find . -regex ".*\(.txt\|.pdf\)$" 查詢當前目錄中以 .txt 或 .pdf 結尾的檔案引號裡面的第一個 " . " 號是查詢的檔案結尾.txt或.pdf的前面的任何字元的代替符號|,引號中的括號、邏輯符號等特殊字元需要 " \ " 轉義,正則表示式以 $ 結尾。[ -regex(regular expression)<範本樣式>:指定字串作為尋找檔案或目錄的範本樣式 ]
    find . -iregex ".*\(.txt\|.pdf\)$" 同上,iregex 忽略大小寫
    find . -name "*.txt" -o -name ".pdf" 當前目錄及子目錄下所有以.txt和.pdf結尾的檔案
    find . \( -name "*.txt" -o -name ".pdf" \) 同上
    find . -type f ! -name "*.txt" 找出當前目錄及其子目錄下不是以.txt結尾的檔案,注意 " ! " 和 -name 之間要有空格

    如果多個表示式需要匹配時使用 ( ) 將運算式分隔,並使用下列運算
    並:語法① exp1 -and exp2 ,語法② exp1, exp2
    非:語法① ! expr , 語法② -not expr
    或:語法 exp1 -or exp2

    實現刪除

    find . -type f -name "*txt" -delete 刪除當前目錄下所有.txt檔案,在最後直接加上 -delete
    # find . -type f -mtime -7 -ok rm {} \; 查詢當前目錄及其子目錄中更改時間在 7日以內的一般檔案,並在刪除之前詢問。在最後直接加上 -ok rm {} \;相互之間有空格

  5. 根據使用者組
    find . -type f -group a5 查詢當下目錄及子目錄中使用者組名為a5的檔案

  6. 根據路徑path
    [[email protected] home]# find . -path ./a7 查詢 /home目錄及子目錄下,路徑名為 ./a7的檔案
    [[email protected] home]# find . -ipath ./a7同上,ipath 會忽略大小寫,i是ignore忽略的首字母

  7. 根據目錄深度
    find . -maxdepth 3 -type f 查詢當前目錄中向下最大深度限制為3的檔案
    find . -mindepth 2 -type f 搜尋出深度距離當前目錄至少2個子目錄的所有檔案