檔案的搜尋


		which 		 尋找執行檔。

whereis 尋找特定檔案。
locate 尋找檔案(建立於資料庫)。
updatesdb 建立檔案資料庫。
find 尋找檔案。
  1. which:到環境變數 $PATH 指定的路徑,尋找可執行檔。
    [root@dywHome2 test]# which [執行檔名稱] 
    參數說明: 
    #範例: 
    [root@dywHome2 test]# which ls
    /bin/ls
    [root@dywHome2 test]# which passwd
    /usr/bin/passwd
    [root@dywHome2 test]# which fdisk
    /sbin/fdisk
    
  2. whereis:在特定的目錄,如 /bin, /usr/bin, /etc, /usr/share/man ... 等,尋找可執行檔、原始碼檔、manpage 檔案。
    [root@dywHome2 test]# whereis [-bmsu] filename 
    參數說明: 
    -b    :只找 binary 的檔案 
    -m    :只找在說明檔 manual 路徑下的檔案 
    -s    :只找 source 來源檔案 
    -u    :沒有說明檔的檔案 
    
    #範例: 
    [root@dywHome2 test]# whereis updatedb
    updatedb: /usr/bin/updatedb /etc/updatedb.conf /usr/share/man/man1/updatedb.1.bz2
    [root@dywHome2 test]# whereis -b updatedb
    updatedb: /usr/bin/updatedb /etc/updatedb.conf
    [root@dywHome2 test]# whereis -m updatedb
    updatedb: /usr/share/man/man1/updatedb.1.bz2
    [root@dywHome2 test]# man whereis
    [root@dywHome2 test]# whereis -s updatedb
    updatedb:
    

  3. locate:到檔案資料庫尋找檔案,資料庫可使用 updatedb 更新。
    [root@dywHome2 test]# locate filename 
    參數說明: 
    #範例: 
    [root@dywHome2 test]# locate updatedb
    locate: fatal error: Could not find user database '/var/lib/slocate/slocate.db':
    No such file or directory
    [root@dywHome2 test]# updatedb
    /usr/bin/slocate: option requires an argument -- l
    [root@dywHome2 test]# locate updatedb
    /usr/share/man/man1/updatedb.1.bz2
    /usr/share/vim/ftplugin/updatedb.vim
    /usr/share/vim/syntax/updatedb.vim
    /usr/bin/updatedb
    /etc/updatedb.conf
    
  4. updatedb 建立資料車 slocate.db 之設定。
    [root@dywHome2 test]# cat /etc/updatedb.conf
    ## Mandriva Linux configuration.
    # Originally written by Chmouel Boudjnah <chmouel@mandrakesoft.com>
    # Modified 20010109 by Francis Galiegue <fg@mandrakesoft.com>
    # Fixes by mlord@pobox.com, 20010328
    
    # Which directories to exclude. /home and /root are excluded for privacy, but
    # YMMV
    PRUNEPATHS="/proc /tmp /var/tmp /usr/tmp /net /afs /mnt /afs /media /sfs 
    /var/spool/cups /var/spool/squid"
    
    # Which filesystems do we exclude from search?
    PRUNEFS="nfs smbfs ncpfs proc devpts supermount vfat iso9660 udf usbdevfs 
    devfs auto afs sfs"
    
  5. find:功能強大的檔案搜尋指令。
    [root@linux ~]# find [PATH] [option] [action]
    參數:
    1. 與時間有關的參數:
       -atime n :在 n 天之前的『一天之內』被 access 過的檔案;
       -ctime n :在 n 天之前的『一天之內』被 change 過狀態的檔案;
       -mtime n :在 n 天之前的『一天之內』被 modification 過的檔案;
       -newer file :比檔案 file 還新的檔案。
    2. 與使用者或群組名稱有關的參數:
       -uid n :使用者的帳號 ID,UID=n 的檔案
       -gid n :群組名稱的 ID,GID=n 的檔案
       -user name :使用者帳號名稱為 name 的檔案
       -group name:群組名稱為 name 的檔案
       -nouser    :檔案的擁有者不存在的檔案
       -nogroup   :檔案的擁有群組不存在的檔案
    3. 與檔案權限及名稱有關的參數:
       -name filename:檔案名稱為 filename 的檔案;
       -size [+-]SIZE:比 SIZE 還要大(+)或小(-)的檔案。SIZE 的規格有:
                       c:byte, k:1024bytes。例如:-size +50k。
       -type TYPE    :檔案的類型為 TYPE 的檔案,類型主要有:一般正規檔案 (f),
                       裝置檔案 (b, c), 目錄 (d), 連結檔 (l), socket (s), 
                       及 FIFO (p) 等屬性。
       -perm mode  :檔案屬性『剛好等於』 mode 的檔案
       -perm -mode :檔案屬性『必須包含所有 mode 的屬性』的檔案,權限數值大於等於 mode。
       -perm +mode :檔案屬性『包含任一 mode 的屬性』的檔案,
                     例如:-perm +755(-rwxr-xr-x),包含屬性為 -rw------- 的檔案。
    4. 額外可進行的動作:
       -exec command :-exec 後再接指令 command 來處理搜尋的結果。
       -print        :將結果列印到螢幕上(預設動作)
    #範例:
    #範例一:列出 24 小時內更動過內容 (mtime) 的檔案
    [root@linux ~]# find / -mtime 0
    # find / -mtime 3 ,今天之前的 3*24 ~ 4*24 小時之間
    # -atime 與 -ctime 的用法相同。
    
    #範例二:尋找 /etc 下檔案日期比 /etc/passwd 新的檔案
    [root@linux ~]# find /etc -newer /etc/passwd
    
    #範例三:搜尋 /home 下屬於 deyu1 的檔案
    [root@linux ~]# find /home -user deyu1
    
    #範例四:搜尋系統中不屬於任何人的檔案
    [root@linux ~]# find / -nouser
    
    #範例五:找出檔名為 passwd 這個檔案
    [root@linux ~]# find / -name passwd
    
    #範例六:搜尋檔案屬性為 f (一般檔案) 的檔案
    [root@linux ~]# find /home -type f
    # 例如 socket 與 FIFO 檔案,可以用 find /var -type p 或 -type s 來找
    
    #範例七:搜尋檔案當中含有 SGID/SUID/SBIT 的屬性
    [root@linux ~]# find / -perm +7000 
    # 7000 是 ---s--s--t ,只要含有 s 或 t 的就列出,
    # 使用 -7000 表示要含有 ---s--s--t 的所有三個權限,
    
    #範例八:將上個範例找到的檔案使用 ls -l 列出
    [root@linux ~]# find / -perm +7000 -exec ls -l {} \;
    # {} 代表『由 find 找到的內容』
    # 就是將前面找到的那些檔案以 ls -l 列出長的資料!至於 
    # \; 則表示 -exec 的指令結束,一定要存在。
    
    #範例九:找出系統中,大於 1MB 的檔案
    [root@linux ~]# find / -size +1000k
    

練習題

  1. 那個指令只到環境變數 $PATH 指定的路徑,尋找檔案?故只能尋找可執行檔。
    Sol. which
  2. 那個指令在特定的目錄,如 /bin, /usr/bin, /etc, /usr/share/man ... 等,尋找可執行檔、原始碼檔、manpage 檔案?
    Sol. whereis
  3. 如何利用指令 whereis,只找出指令 ls 的可執行檔之路徑?
    Sol. whereis -b ls
  4. 如何利用指令 whereis,只找出指令 ls 的說明檔之路徑?
    Sol. whereis -m ls
  5. 如何利用指令 whereis,只找出指令 ls 的原始碼檔之路徑?
    Sol. whereis -s ls
  6. 如何利用指令 whereis,只找出指令 ls 的可執行檔、原始碼檔、說明檔之路徑?
    Sol. whereis ls
  7. 那個指令只到檔案資料庫 slocate.db 尋找檔案?故必須先建立檔案資料庫。
    Sol. locate
  8. 檔案資料庫 slocate.db,必須使用什麼指令建立及更新?
    Sol. updatedb
  9. updatedb 建立檔案資料庫 slocate.db 之設定存在那個檔案?
    Sol. /etc/updatedb.conf
  10. 如何找出 /etc 下,兩天前的 24 小時內,變動過內容的檔案?
    Sol. find /etc -mtime 2
  11. 如何以指令 find 搜尋 24 小時內更動過內容 (mtime) 的檔案?
    Sol. find / -mtime 0
  12. 如何以指令 find 搜尋三天之前的 24 小時內更動過內容 (mtime) 的檔案?
    Sol. find / -mtime 3
  13. 如何以指令 find 搜尋二天之前的 24 小時內更動過狀態 (ctime) 的檔案?
    Sol. find / -ctime 2
  14. 如何以指令 find 搜尋五天之前的 24 小時內存取過 (atime) 的檔案?
    Sol. find / -atime 5
  15. 如何以指令 find 尋找 /etc 下檔案日期比 /etc/passwd 新的檔案?
    Sol. find /etc -newer /etc/passwd
  16. 如何找出 /etc 下,比 /home/csie/testfile 新的檔案?
    Sol. find /etc -newer /home/csie/testfile
  17. 如何以指令 find 搜尋 /home 下屬於 csie 的檔案?
    Sol. find /home -user csie
  18. 如何以指令 find 搜尋系統中不屬於任何人的檔案?
    Sol. find / -nouser
  19. 如何以指令 find 搜尋系統中檔名為 foo 的檔案?
    Sol. find / -name foo
  20. 如何以指令 find 搜尋 /home 下檔案屬性為 f (一般檔案) 的檔案?
    Sol. find /home -type f
  21. 如何找出 /etc 下,所有含有 SUID/SGID 的檔案?
    Sol. find /etc -perm +6000
  22. 如何找出 /etc 下,權限至少為 755 以上的檔案?
    Sol. find /etc -perm -0755
  23. 如何找出 /etc 下,檔案大小大於 1MB 以上的檔案?
    Sol. find /etc -size +1024k
  24. 如何找出 /etc 下,檔案大小小於 50KB 的檔案?
    Sol. find /etc -size -50k

  DYWANG_HOME