檔案尋找命令 find 總結

來源:互聯網
上載者:User

Linux檔案尋找:

locate: 非即時尋找,根據資料庫(速度快);模糊尋找;

find:即時尋找,精確匹配;速度慢;


find [尋找位置] [尋找標準] [處理動作]

尋找位置:預設為目前的目錄;

尋找標準:預設為尋找指定目錄下的所有檔案;

處理動作:顯示到標準輸出;


尋找標準:

-name "檔案名稱": 根據檔案名稱尋找,精確尋找檔案。

支援glob, *, [], ?

-iname "檔案名稱":根據檔案名稱尋找,不區分字元大小寫;name ignore case


-user USERNAME: 根據屬主尋找;

-group GRPNAME: 根據屬組尋找;


-uid UID; #按照Uid 來尋找

-gid GID; #按照gid來尋找


-nouser:尋找沒屬主的檔案;

-nogroup: 尋找沒有屬組的檔案;


組合尋找條件:

-a: 與

-o:或

-not, !: 非

例如:尋找/tmp目錄下沒有屬主,並且檔案名稱以一個字元.test檔案;

# find /tmp -name "?.test" -nouser -ls


-type

f: 普通檔案

d: 目錄

b: 塊裝置

c: 字元裝置

l: 符號連結

p: 具名管道

s: 通訊端檔案


-size

12MB (11.,12)MB #表示精確12MB 大小 尋找

-size [+|-]2MB #+表示大於2MB -表示小於2MB

常用單位:

k

M

G

根據時間來尋找:

time的時間單位是day 天

-atime [+|-]# 訪問時間 access time

-atime 3#表示剛好在過去第3天訪問過的檔案

-atime -3#表示3天以內訪問的檔案

-atime +3#表示至少3天沒有訪問的檔案

-mtime [+|-]# 修改時間 modify time

-ctime [+|-]# 改變時間 change time

min的時間單位是minute 分鐘

-amin [+|-]

-amin 3#表示剛好在過去第3分鐘訪問過的檔案

-amin -3#表示3分鐘以內訪問的檔案

-amin +3#表示至少3分鐘沒有訪問的檔案

-mmin [+|-]

-cmin [+|-]

根據許可權尋找:

-perm [+|-]MODE

沒有[+|-]表示精確許可權匹配;

+MODE: 任何一類使用者的任何一位許可權匹配即可;

-MODE: 每類使用者的每位許可權都匹配;



處理動作:

-print: 顯示

-ls: 顯示尋找到的檔案的詳細資料;

-exec COMMAND \; #執行操作

find /tmp -atime +30 -exec mv {} {}.old \; #用到檔案名稱的時候用花括弧,{} 表示原來的檔案名稱,{}.old 在原來檔案名稱後面加上.old

-ok COMMAND \;


練習:

1、尋找/var目錄下屬主為root並且屬組為mail的所有檔案;

find /var -user root -group mail


2、尋找/usr目錄下不屬於root,bin,或student的檔案;

find /usr -not \( -user root -o -user bin -o -user student \)

find /usr -not -user root -a -not -user bin -a -not -user student


3、尋找/etc目錄下最近一周內內容修改過且不屬於root及student使用者的檔案;

find /etc -mtime -7 -a -not -user root -a -not -user student

find /etc -mtime -7 -a -not \( -user root -o -user student \)


4、尋找當前系統上沒有屬主或屬組且最近1天內曾被訪問過的檔案,並將其屬主屬組均修改為root;

find / \( -nouser -o -nogroup \) -a -atime -1 -exec chown root:root {} \;


5、尋找/etc目錄下大於1M的檔案,並將其檔案名稱寫入/tmp/etc.largefiles檔案中;

find /etc -size +1M -exec echo {} >> /tmp/etc.largefiles \;

find /etc -size +1M >> /tmp/etc.largefiles


6、尋找/etc目錄下所有使用者都沒有寫入權限的檔案,顯示出其詳細資料;

find /etc -not -perm +222



find / \( -nouser -o -nogroup \) -a -atime -1 | xargs -i chown root:root {}



類型不是目錄,而且沒有屬主的檔案;

find / -not -type d -a -nouser -exec rm -f {} \;

find / -not -type d -a -nouser | xargs -i rm -f {} #xargs 表示可以執行命令,可以從前面的命令傳入所需的檔案名稱也是用{}來調用。只不過調用需要用-i 選項。



find / -size +10M -a -atime +10 -exec mv {} {}.old \;


本文出自 “技術成就夢想” 部落格,請務必保留此出處http://zkw9527.blog.51cto.com/1346897/1318788


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.