文件查找詳解

查找命令有locate和find

一、locate

    根據全系統文件數據庫進行查找,並非實時查找。按計劃任務會自動將本機的文件收集到文件數據庫中,非精確查找。

    手動生成文件數據庫,updatedb,執行速度慢,查找速度快

二、find

    實時、精確、遍歷、查找速度慢、支持衆多查找類型

    find 查找路徑 查找標準 查找的處理動作

    查找路徑:默認爲當前路徑

    查找標準:默認爲路徑下所有文件

    查找的處理動作:默認爲顯示

    匹配標準:

        -name 'filename':根據文件名作精確查找並區分大小寫,支持文件名通配*,?,[]       

         [root@station01 ~]# find /etc -name 'passwd'

        -iname 'filename':文件名匹配不區分大小寫

        -regex PATTERN:基於正則表達式進行文件名匹配

        -user USERNAME:根據文件的屬主進行查找

        -group:GROUPNAME:根據文件的屬組進行查找

        -uid:根據uid查找

        -gid:根據gid查找

        -nouser:查找沒有屬主的文件

        -nogroup:查找沒有屬組的文件

        -type TYPE:根據文件類型查找

            f:普通文件

            d:目錄

            c:字符

            b:塊設備

            l:連接文件

            p:管道

            s:套接字

        -size:文件大小查找

            M

            K

            G

            +-表示範圍

        組合條件

            -a 與

            -o    或

            -not 非

        找出/tmp非普通文件

 [root@station01 ~]# find /tmp -not -type f

        找出不是用戶1的又不是用戶2的文件

    1)

 [root@station01 tmp]# find ./ -not -user user1 -a -not -user user2

     2)

 [root@station01 tmp]# find ./ -not \( -user user1 -o -user user2 \)

    根據文件的時間戳查找文件

    -mtime [+|-]#:修改時間天數

    -ctime [+|-]#:改變時間天數

    -atime [+|-]#:訪問時間天數

        #:到此刻剛好#天

        -#:最近#天

        +#:至少有#天

      -mmin[+|-]#:修改時間分鐘

     -cmin[+|-]#:改變時間分鐘

     -amin [+|-]#:訪問時間分鐘

    根據文件權限查找

    -perm mode 權限精確查找

    -perm -mode每一位權限必須全部匹配

    -perm /mode有任何一位匹配都可以

    

    動作

        -print

        -ls

        -ok command {} \;每一次操作都需要用戶確認

        -exec command {} \; 其中{}號表示引用匹配到的文件名

 [root@station01 tmp]# find ./ -perm 004 -exec chmod o-r {} \;

        文件改名

 [root@station01 tmp]# find ./ -perm 000 -exec mv {} {}.new \;
 [root@station01 tmp]# find /root/ -name "*.sh" -a -perm -111 -exec chmod o-x {} \;

    1、查找/var目錄下屬主爲root並且屬組爲mail的所有文件

    [root@station01 tmp]# find /var/ -user root -group mail -ls

    2、查找/var目錄下不屬於root,bin或student用戶的文件

    [root@station01 tmp]# find ./ -not \( -user user1 -o -user root -o -user bin \)

    3、查找/etc目錄下最近一週修改過並且不屬於root及user2用戶的文件

    [root@station01 tmp]# find /etc/ -mtime -7 -not \( -user root -o -user user2 \)

    4、查找當前系統上沒有屬主和屬組且最近一天內曾被訪問過的文件,並將其屬主屬組修改爲root

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

    5、查找/etc目錄下大於1M的文件,並將其文件名寫入/tmp/largefiles中

    [root@station01 tmp]# find /etc/ -size +1M >>/tmp/largefiles

    6、查找/etc目錄下所有用戶都沒有寫權限的文件,顯示出其詳細信息

    [root@station01 tmp]# find /etc/ -not -perm /222  -ls
     [root@station01 tmp]# find /etc/ -size +1M -exec echo {} >>/tmp/largefiles \;
    [root@station01 tmp]# find /etc/ -size +1M | xargs echo {} >>/tmp/largefiles \;

 

GB2312

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章