Linux檔案尋找詳解

來源:互聯網
上載者:User

標籤:檔案尋找

grep,egrep,fgrep:文本尋找(文字檔中的內容)


檔案尋找:

    locate:

        全系統尋找的命令,

        非即時,

        模糊比對

        尋找是根據全系統檔案資料庫進行的

    #updatedb,手動組建檔案資料庫,因為建立的系統可能沒有建立檔案資料庫

    速度快


    find:

        即時尋找

        精確尋找

        支援眾多尋找標準

        遍曆指定目錄中的所有檔案完成尋找,速度慢


    用法:

        find 尋找路徑 尋找標準 尋找到以後的處理運作

        尋找路徑:預設為目前的目錄

        尋找標準:預設為指定路徑下的所有檔案

        處理運作:預設為顯示


   

    匹配標準:

            -name ‘FILENAME‘:對檔案名稱做精確匹配

                檔案名稱通配:

                            * :任意長度的任一字元

                            ?:任意單個字元

                            []:範圍以內的字元

            -iname ‘FILENAME‘:檔案名稱匹配不區分大小寫

            -regex PATTERN:基於Regex進行檔案名稱匹配


            -user USERNAME:根據屬主尋找

            -group GROUPNAME:根據屬組尋找


            -uid UID:根據UID尋找

            -gid GID:根據GID尋找


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

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


            -type

                 f:普通檔案

                 d:目錄

                 c:字元裝置檔案

                 b:塊裝置檔案

                 l:連結檔案

                 p:管道檔案

                 s:通訊端檔案


            -size

               [+|-]#k

                 [+|-]#M

                 [+|-]#G


    組合條件:

        -a:與(and)

        -o:或(or)

        -not:非

        

        尋找/tmp 目錄下,不是目錄,並且還不能是通訊端類型的檔案

        find /tmp -not -type d -a -not -type s

        find /tmp -not \( -type d -o -type s \)

        尋找/tmp/test 目錄下,屬主不是user1,也不是user2的檔案;

        find /tmp/test -not -user user1 -a -not -user user2

        find /tmp/test -not \( -user user1 -o -user user2 \)


        

    關於時間戳記的參數:

         -mtime

        -ctime

        -atime

              [+|-]#  

        -mmin

        -cmin

        -amin

              [+|-]#


    關於許可權的參數

        -perm

             MODE:精確匹配

            /MODE:任意一位匹配即滿足條件

            -MODE:檔案許可權能完全包含此MODE時才能顯示 


        find ./ -perm -644

    

            -644

            644:rw-r--r--

            755:rwxr-xr-x


    運作:

         -print:顯示

        -ls:類似ls -l的形式顯示每一個檔案的詳細

        -ok COMMOND {} \;每一次操作都需要使用者確認

        -exec COMMOND {} \;不需要使用者確認

        

# find -type d -ok chmod a-x {} \;

# find ./ -perm -020 -exec mv {} {}.new \;

# find ./ -name "*.sh" -a -perm -111 -exec chmod o-x {} \;



find命令的練習:                            

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

# find /var -user root -a -group mail


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

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

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


3、尋找/etc目錄下最近一周內內容修改過且不屬於root及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 {} \;


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

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


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

# find /tmp/test -not -perm /222 -ls

                   

Linux檔案尋找詳解

聯繫我們

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