Linux command – find

Loading

# find paths expression

Find 是一個常用的指令,相當好用卻也是一個相當難記的指令,下面列出一些常用的參數.

  • -name
    [root@benjr root]# find / -name "passwd"
    

    依名稱來搜尋檔案,可以使用 *,? 等字元為搜尋目標.

  • -nouser , -nogroup
    依檔案的擁有者不屬於任何使用者 (User owner 為數字或不存在於 /etc/passwd 檔案中) 來搜尋,另外針對group還有 -nogroup 可用.

    [root@benjr root]# find /home -nouser
    [root@benjr root]# find /home -nogroup
    

    除了可以單獨使用還可以搭配來使用。 使用參數 -o(-or) 還有 -a(-and) 還有 !(-not)

    [root@benjr root]# find /home -nouser -o -nogroup
    

    這樣就可以找檔案的擁有者不屬於任何使用者或者不屬於任何群組.

  • -perm
    我們還可以依據檔案的 rwx 屬性來尋找.

    [root@benjr root]# find / -perm 644
    

    找出 file permition 為 644(rw-r–r–) 的檔案,另外還可以用 -644(只比較644(rw-r–r–)不管其他bit為何),+644(只要其中一個bit相符合既可)

    [root@benjr root]# find / -perm -644
    [root@benjr root]# find / -perm +644
    

    找出檔案屬性有 SUID 者. 還可用-4000(SUID) -2000 (SGID) -1000(sTicky Bit) 等

    [root@benjr root]# find / -perm -4000
    [root@benjr root]# find / -perm -2000
    [root@benjr root]# find / -perm -1000
    
  • -size
    找出檔案大於 100M的,100000單位為 kbyte. “+” 代表大於100000k, “-” 代表小於 100000k.

    [root@benjr root]# find / -size +100000
    
  • -exec
    參數 -exec 將之前的結果轉向 ls -l 來處理 , {} 表示之前的結果 , \; 結束.

    [root@benjr root]# find . -nouser -exec ls -l {} \;
    

    至於要找檔案內容的除了要使用 find 還要配合 grep -l(-l :files with matches),如果你要找 /root 目錄下所有的檔案內容包含 Ben 字串的檔案.可以用下面方法來實現.

    [root@benjr root]# find /root/* -exec grep -l Ben {} \;
    

    至於 find 的 -exec 參數請參考上面的範例,grep的參數 -l 為只會列出含有此特定字串的檔案名稱.

  • -type f , -type d , -type l
    找出屬性為檔案 f (file) ,還可針對目錄 d (directory) ,連結 l (softlink)來尋找.

    [root@benjr root]# find / -type f
    [root@benjr root]# find / -type d
    [root@benjr root]# find / -type l
    

    計算目錄中各個子目錄分別的容量

    [root@benjr root]# find /home -maxdepth 1 -type d -exec du -sh {} \;
    40M /home
    20M /home/user1
    20M /home/user2
    

    在指定的目錄內,將檔案修改成 664 (rw-r–r–)

    # find /path/to/site/ -type f -exec chmod 664 {} \;
    

    在指定的目錄內,將目錄修改成 775 (rwxrwxr-x)

    #find /path/to/site/ -type d -exec chmod 775 {} \;
    

下面是針對 檔案的時間 ( mtime / ctime / atime ) 來做搜尋.

  • mtime / ctime / atime
    先來了解一下 Linux 會記錄檔案的三種時間 mtime / ctime / atime

    [root@benjr ~]# stat test
      File: ‘test’
      Size: 0               Blocks: 0          IO Block: 4096   regular empty file
    Device: 801h/2049d      Inode: 269772      Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2016-06-06 18:38:50.664135013 +0800
    Modify: 2016-06-06 18:38:50.664135013 +0800
    Change: 2016-06-06 18:38:50.664135013 +0800
     Birth: -
    
    1. atime(Access): time of last access
      這裡記錄著上一次最後存取這個檔案的時間,只要這檔案被存取過這時間就會被更新,如果這檔案是可執行檔在執行後這時間也會被改變.也可以透過 ls -lu 來看 atime

      [root@benjr ~]# cat test
      [root@benjr ~]# stat test
        File: ‘test’
        Size: 0               Blocks: 0          IO Block: 4096   regular empty file
      Device: 801h/2049d      Inode: 269772      Links: 1
      Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
      Access: 2016-07-26 09:33:42.795224480 +0800
      Modify: 2016-06-06 18:38:50.664135013 +0800
      Change: 2016-06-06 18:38:50.664135013 +0800
       Birth: -
      
    2. mtime(Modify): time of last modification
      紀錄上一次檔案被更改的時間,只要檔案內容被修改過 mtime 的會被更新.也可以透過 ls -l 來看 mtime
    3. ctime(Change): time of last status change
      只要是檔案或是目錄的 inode 被改變(owner, permissions, 等.)這時間就會被更新.還有當檔案被更新時這 ctime 也會被更新. dump 的備份指令就是透過 ctime 來看需不需要將這檔案做備份為依據. 可以透過 ls -lc 來看 ctime

      [root@benjr ~]# echo test >> test
      [root@benjr ~]# stat test
        File: ‘test’
        Size: 5               Blocks: 8          IO Block: 4096   regular file
      Device: 801h/2049d      Inode: 269772      Links: 1
      Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
      Access: 2016-07-26 09:33:42.795224480 +0800
      Modify: 2016-07-26 09:34:12.812208000 +0800
      Change: 2016-07-26 09:34:12.812208000 +0800
       Birth: -
      
    # find paths expression
    

    以時間為搜尋的 find 後面的 expression 可以為剛剛所提到的 mtime / ctime / atime 再加上要搜尋的 時間點

    • -mtime n
      n 表示要搜尋 n 天之前(24小時)有更動過內容的檔案.也可以用 ctime 或是 atime.

      [root@benjr ~]# find /root -mtime 0
      /root
      /root/.bash_history
      /root/test
      

      -mtime 0 表示要搜尋當下的時間點, 24小時以內有變動過的檔案.

    • -mtime +n
      搜尋 n 天之(不含 n 天本身)有更動過內容的檔案.也可以用 ctime 或是 atime.

      [root@benjr ~]# find /root -mtime +1
      /root/.viminfo
      /root/.cache
      /root/.cache/dconf
      /root/.cache/dconf/user
      /root/.bashrc
      /root/diff.lst
      /root/.local
      /root/.local/share
      /root/.local/share/recently-used.xbel
      /root/.dbus
      /root/.dbus/session-bus
      /root/.dbus/session-bus/f07b3a3bf0b28d1c5a2bbdf95744e8fc-0
      /root/.profile
      /root/new.lst
      
    • -mtime -n
      搜尋 n 天之(含 n 天本身)被更動過內容的檔案.也可以用 ctime 或是 atime.

      [root@benjr ~]# find /root -mtime -1
      /root
      /root/.bash_history
      /root/test
      
    • -newer file
      參數 -cnewer find 會依據 file 建立時間來搜尋.比 file 建立的時間還要新的檔案就會列出來.

      [root@benjr ~]# find /var/ftp -cnewer new.lst 
      

      參數 -cnewer find 會依據 new.lst 建立時間來搜尋出目錄 /var/ftp/ 中比 new.lst 時間還要新的檔案以及目錄,通常可以用於 Differential backup (差異備份) 時使用.除了 cnewer 外還有 anewer 可以使用.

    剛剛的時間點都是以天來計算,還有以分鐘來計算的時間.

    • -amin n
      File was last accessed n minutes ago.
    • -cmin n
      File’s status was last changed n minutes ago.
    • -mmin n
      File’s data was last modified n minutes ago.
沒有解決問題,試試搜尋本站其他內容

One thought on “Linux command – find

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料