2012/08/26

Linux -- find 指令用法


find

  • Find [directory...] [criteria...]
  1. 不同於 locate,Find 會實際去找檔案位置,但速度較慢,若沒有指定目錄,預設為cwd 目前工作目錄
  • 語法
[lcc09@localhost ~]$ find --help
用法:find [路徑...] [表達式]

預設路徑為目前的目錄,預設的表達式是 -print
表達式可以包括運算子、選項、測試和操作模式:
.....(略)
  • 基本上可以分成三個大項來看:
  1. [路徑...]搜尋路徑
  2. [表達式]搜尋的檔案條件,支援"Globbing"的語法。
  3. 動作action
    1. 命令執行(exec),屬"附加"性質,讓我們可以直接對 find 找到的檔案,運作指定的外部指令。
    2. 列印顯示(print),支援固定顯示。也可將顯示的資訊藉由選項直接存檔。
  4. 選項說明
    1. -daystart 起始時間以當日的 -amin, -atime, -cmin, -ctime, -mmin, 及 -mtime 為基準。
    2. -depth 處理目錄內容之後,再處理目錄本身。
    3. -follow 對符號連結的檔案或目錄不做檢查。隱含了 -noleaf 選項的作用。
    4. -help, --help 顯示程式用法資訊。
    5. -maxdepth levels 指定搜尋測試的最大目錄層數(levels)。對超過目錄層數的子目錄將不予以處理。
    6. -mindepth levels 指定搜尋測試的最小目錄層數(levels)。對 levels 內的目錄層數將不予以處理。
    7. -mount 只處理當前所在的檔案系統。不處理不同檔案系統(filesystems)的目錄。此選項在某些版本的 find 指令必須改用 -xdev 選項。
    8. -noleaf 捨棄檢查目錄是否有兩個硬連結(hard link)的優化處理。(程式預設的優化處理,在不必要時,反而會減緩搜尋測試的速度。如果使用者能理解自己檔案系統的硬連結狀況,適當地關閉預設的優化處理,將有助於速度的提昇)
    9. -version, --version 顯示程式本身的版本資訊。
    10. -xdev 只處理當前所在的檔案系統。對放置在不同檔案系統(filesystems)的目錄不做處理。
  • ex1
[lcc09@localhost ~]$ find / -name '*html' -type f 2>find.err >find.out
# 從「/」根目錄開始,找出檔名為*html 只列出type為f形式的資料
# -type 形式請自行參考man page說明
[lcc09@localhost ~]$ tail find.err 
find: /dev/VolGroup00: 拒絕不符權限的操作
find: /tmp/virtual-mtchang.o4WZCh: 拒絕不符權限的操作
find: /tmp/orbit-mtchang: 拒絕不符權限的操作
find: /tmp/virtual-mtchang.HdndkY: 拒絕不符權限的操作
find: /tmp/ssh-pPqrzi2340: 拒絕不符權限的操作
...(略)
[lcc09@localhost ~]$ tail find.out 
/usr/bin/post-grohtml
/usr/bin/pre-grohtml
/usr/bin/pod2html
/usr/lib/perl5/5.8.8/CGI/eg/index.html
/usr/lib/esc-1.0.0/chrome/content/esc/hiddenWindow.html
/usr/lib/esc-1.0.0/xulrunner/res/hiddenWindow.html
...(略)
  • ex2
[lcc09@localhost ~]$ find /usr/share/doc/ -name '*html' -type d
# 找出檔名為*html 只列出type為d形式的資料
/usr/share/doc/pam-0.99.6.2/html
/usr/share/doc/pyOpenSSL-0.6/html
/usr/share/doc/selinux-policy-2.4.6/html
/usr/share/doc/system-config-services-0.9.4/html
/usr/share/doc/libxslt-1.1.17/EXSLT/html
/usr/share/doc/libxslt-1.1.17/html
/usr/share/doc/sgml-common-0.6.3/html
  • ex3
[lcc09@localhost ~]$ mkdir ~/html
[lcc09@localhost ~]$ find /usr/share/doc/ -name 'man*.html' -type  f -exec cp {} ~/html \; -print
# copy set to ~/html 目錄
  • ex4
[lcc09@localhost ~]$ find /usr/share/doc/ -name 'man*.html' -type  f -exec grep samba-user {} \; -print
# 找出集合檔案中,帶有「samba-user」的行,並把他列印出來。
samba-user[%samba-password]
samba-user[%samba-password]
-U samba-user[%samba-password] /usr/share/doc/cups-1.2.4/help/man-cupsaddsmb.html
  • 練習:請解釋下面這一段命令的動作。
[lcc09@localhost ~]$ find / -name '*.mp3' -type f -exec rm -rf {} \; -print
  • find example:
find / -name foo.png (找檔名 foo.png)
find / -iname foo.png (不分大小寫,找檔名 foo.png)
find / -name "*.png" (找所有附檔名 .png 的檔案)
find / -user joe -group joe (找擁有者以及群組為 joe 的所有檔案)

 

Find and Logical Operations
邏輯上來講預設輸入的條件為 AND,其他也可以加入 -not,-o (or)
find -user joe -not -group joe (找使用者為joe,但 group 不為 joe)
find -user joe -o -user jane (找使用者為 joe 或者使用者為 jane)
find -not \(-user joe -o -user jane\) (找使用者不是 joe 也不是 jane)

 

Find and Permissions
find / -user joe -o -uid 500 (找使用者為 joe 或者 uid 為 500 的使用者)
find -perm 755 (找符合權限 755 的檔案)
find -perm +222 (找可寫入權限的檔案,不管任何人)
find -perm -222 (較嚴格,找全部人都有寫入權限的檔案)
find -perm -002 (other 有寫入權限的檔案)

 

find and Numberic Criteria
find -size 1024K (找檔案剛好等於 1MB)
find -size +1024K (找檔案超過 1MB)
find -size -1024K (找檔案小於 1MB)
find / -atime +5 (找檔案讀取日期超過 5 天)
find / -atime 5 (找檔案讀取日期剛好 5 天)
find / -atime -5 (找檔案讀取日期在 5 天以內)

-atime,when file was last read
-mtime,when file data last changed
-ctime,when file or metadata last changed 
find -ctime -10 (找10天以下有更動過檔案)
find -newer recent_file.txt (也可以跟某一個檔案比較有沒有比它更新)
find -not -newer recent_file.txt (也可以跟某一個檔案比較有沒有比它更舊)

 

Executing Commands with find
找到特定檔案之後,利用 -exec 或者 -ok 再進行進一步處理,其中 -ok 處理檔案時會逐一確認
find -size +1024K -ok gzip {} \; (代表檔案位置{},結束一定要用\;結束)
find -name "*.conf" -exec cp {} {}.orig \; (把所有 xxx.conf 檔案複製一份 xxx.conf.orig)
find /tmp -ctime +3 -user joe -ok rm {} \; (/tmp 中屬於 joe 的檔案超過三天沒更動,刪除,逐一確認)
find ~ -perm +o+w -exec chmod o-w {} \; (把 other 的寫入權限取消)

沒有留言: