天天看點

linux中檔案查找

linux中檔案查找常用的有兩個指令,分别是locate和find。

#locate 根據locate資料庫查找,不是實時查找,不是精确查找.速度比較快

#find 不根據資料庫查找,實時查找,周遊目錄查找,精确查找,速度慢.

# locate 要查找的關鍵字

        -n 隻顯示被比對到的前10行.

        -c 統計被比對到的結果總共有多少.

locate是根據locate資料庫進行查找的,是以速度比較快,如果一個檔案剛存入linux系統,然後用locate進行查找,有可能會查找不到,因為locate的資料庫裡面,還沒有該檔案的相關記錄,這時候,我們就需要手動更新locate的資料庫,使用指令

updatedb   将手動生成locate指令所依賴的資料庫

find [dir,...] [criteria,...] [action...] 

如果[dir]省略,就是目前路徑.如果find後面不跟任何參數,會把目前目錄裡的所有檔案及目錄全部顯示出來,包括隐藏檔案

action的預設動作為顯示到螢幕上.

criteria 查找标準

-name 指定檔案名.還支援globbing方式的檔案名通配

find /etc/ -name "passwd?"

find /etc/ -name "*passwd*"

-iname 忽略檔案名中的檔案名大小寫.

-regex "PATTERN" 查找檔案名中符合PATTERN中的檔案,支援正則.

-user USERNAME 基于使用者查找(查找屬于某個使用者的檔案)

-group GROUP_NAME

find /tmp -user redhat | ls -l 這樣不行,因為find的傳遞機制跟普通指令不太一樣

ll `find /tmp -user redhat` 這樣是可以的,引用指令.

-uid UID 基于使用者的ID号查找

-gid GID 基于組的ID号查找.

-nouser 查找所有的,沒有屬主的檔案

-nogroup 查找所有的,沒有屬組的檔案

-type 指定類型進行查找

    f 普通檔案

    d 目錄

    l 軟連接配接檔案

    b 塊裝置

    c 字元裝置

    p 管道檔案

    s 套接字檔案

組合條件:

   -a

   -o

   -not

-size 指定檔案大小

10M 大小為10M的(9-10M的都符合,10M以上和9M以下不符合)

-10M 小于10M的

+10M 大于10M的

-atime 根據檔案的通路時間(機關為天)

-mtime 根據檔案的修改時間 (機關為天)

-ctime 根據檔案的改變時間(機關為天)

-amin 根據檔案的通路時間(機關為分鐘)

-mmin 根據檔案的修改時間(機關為分鐘)

-cmim 根據檔案的改變時間(機關為分鐘)

time 的預設時間為天,min的預設機關為分鐘.

例:

-atime 3 距離現在,剛好3天沒通路的檔案

-atime -3 3天以内被通路的檔案

-atime +3 已經超過3天沒被通路的檔案

-perm 755 根據檔案的權限進行查找.

+755|/755 其中任何一類使用者滿足其權限都可以. /444 任何一類使用者有讀權限都可以.

-755, 每一類使用者都必須要滿足條件.

Ctrl+a 跳到指令的首部.

Ctrl+e 跳到指令的尾部.

action:

-print 把比對到的結果列印到螢幕(預設動作,可省略.)

-ls 以長格式顯示檔案資訊

-ok COMANDN {} \; 查找到檔案後,執行指定指令(提醒确認)

-exec COMMAND {} \; 查找到檔案後,執行指定指令(不提醒确認)

-exec rm {} \; 删除查找到的檔案 {}表示引用前面查找到的檔案,\;表示指令到此結束

-exec mv {} {}.txt \; 把查找到的檔案重命名,在原有的名字後面加上.txt的擴充名

下面做幾個練習題

練習:

1,查找/var/ 目錄下屬主為root并且屬組為mail的所有檔案;

[root@Honway scripts]# find /var -user root -group mail

/var/spool/mqueue

/var/spool/mail

2,查找/usr目錄下不屬于root,bin或student的檔案;

[root@Honway scripts]#find /usr/ -not -user root -not -user bin -not -user student

/usr/local/apache2/logs/cgisock.3302

/usr/local/apache2/logs/cgisock.32138

/usr/local/apache2/logs/cgisock.3327

3,查找/etc/目錄下最近一周内内容修改過且不屬于root及apache的檔案

[root@Honway scripts]# find /etc -not \( -user root -o -user apache \) -mtime -7

4,查找目前系統上沒有屬主或屬組且最近1天内曾被通路過的檔案,并将其屬主屬組均修改為root;

[root@Honway scripts]# find / \( -nouser -o -nogroup \) -atime -1 -exec chown root:root {} \;

5,查找/etc目錄下大于1M的檔案,并将其檔案名寫入/tmp/etc/largefiles檔案中;

[root@Honway scripts]#for i in `find /etc/ -size +1M -exec basename {} \;`;do echo $i >>/tmp/etc.largefiles ;done

[root@Honway ~]# find /etc/ -size +1M -exec basename {} >/tmp/find.out \;

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

[root@Honway scripts]# ll `find /etc/ -not -perm +222`

-r-------- 1 root root 1225 02-11 10:00 /etc/gshadow

-r-------- 1 root root 1213 02-11 00:19 /etc/gshadow-

-r--r--r-- 1 root root 41286 2006-11-28 /etc/mail/submit.cf

-r--r--r-- 1 root root 628 2010-07-21 /etc/selinux/config,v

-r-------- 1 root root 3159 02-11 10:00 /etc/shadow

-r-------- 1 root root 3129 02-11 00:29 /etc/shadow-

本文轉自 gm100861 51CTO部落格,原文連結:http://blog.51cto.com/gm100861/788831