1. 程式人生 > >linux find 用法和常見用例

linux find 用法和常見用例

-nogroup                     #查無有效屬組的檔案,即檔案的屬組在/etc/groups中不存在
-nouser                     #查無有效屬主的檔案,即檔案的屬主在/etc/passwd中不存
-newer   f1 !f2              找檔案,-n指n天以內,+n指n天以前 
-ctime    -n +n               #按檔案建立時間來查詢檔案,-n指n天以內,+n指n天以前 
-nogroup                     #查無有效屬組的檔案,即檔案的屬組在/etc/groups中不存在
-nouser                      #查無有效屬主的檔案,即檔案的屬主在/etc/passwd中不存
-newer   f1 !f2               #查更改時間比f1新但比f2舊的檔案
-type    b/d/c/p/l/f         #查是塊裝置、目錄、字元裝置、管道、符號連結、普通檔案
-size      n[c]               #查長度為n塊[或n位元組]的檔案
-depth                       #使查詢在進入子目錄前先行查詢完本目錄
-fstype                     #查更改時間比f1新但比f2舊的檔案
-type    b/d/c/p/l/f         #查是塊裝置、目錄、字元裝置、管道、符號連結、普通檔案
-size      n[c]               #查長度為n塊[或n位元組]的檔案
-depth                       #使查詢在進入子目錄前先行查詢完本目錄
-fstype                      #查位於某一型別檔案系統中的檔案,這些檔案系統型別通常可 在/etc/fstab中找到
-mount                       #查檔案時不跨越檔案系統mount點
-follow                      #如果遇到符號連結檔案,就跟蹤連結所指的檔案
-cpio                %;      #查位於某一型別檔案系統中的檔案,這些檔案系統型別通常可 在/etc/fstab中找到
-mount                       #查檔案時不跨越檔案系統mount點
-follow                      #如果遇到符號連結檔案,就跟蹤連結所指的檔案
-cpio                        #對匹配的檔案使用cpio命令,將他們備份到磁帶裝置中
-prune                       #忽略某個目錄

=====================================================
$find   ~   -name   "*.txt"   -print    #在$HOME中查.txt檔案並顯示
$find   .    -name   "*.txt"   -print
$find   .    -name   "[A-Z]*"   -print   #查以大寫字母開頭的檔案
$find   /etc   -name   "host*"   -print #查以host開頭的檔案
$find   .   -name   "[a-z][a-z][0–9][0–9].txt"    -print   #查以兩個小寫字母和兩個數字開頭的txt檔案
$find .   -perm   755   -print
$find   .   -perm -007   -exec ls -l {} \;   #查所有使用者都可讀寫執行的檔案同-perm 777
$find   . -type d   -print
$find   .   !   -type   d   -print 
$find   .   -type l   -print

$find   .   -size   +1000000c   -print        #查長度大於1Mb的檔案
$find   .   -size   100c         -print       # 查長度為100c的檔案
$find   .   -size   +10   -print              #查長度超過期作廢10塊的檔案(1塊=512位元組)

$cd /
$find   etc   home   apps    -depth   -print   | cpio   -ivcdC65536   -o   /dev/rmt0
$find   /etc -name "passwd*"   -exec grep   "cnscn"   {}   \;   #看是否存在cnscn使用者
$find . -name "yao*"   | xargs file
$find   . -name "yao*"   |   xargs   echo    "" > /tmp/core.log
$find   . -name "yao*"   | xargs   chmod   o-w

======================================================

find   -name april*                     在當前目錄下查詢以april開始的檔案
find   -name   april*   fprint file        在當前目錄下查詢以april開始的檔案,並把結果輸出到file中
find   -name ap* -o -name may*   查詢以ap或may開頭的檔案
find   /mnt   -name tom.txt   -ftype vfat   在/mnt下查詢名稱為tom.txt且檔案系統型別為vfat的檔案


find   /mnt   -name t.txt ! -ftype vfat   在/mnt下查詢名稱為tom.txt且檔案系統型別不為vfat的檔案
find   /tmp   -name wa* -type l            在/tmp下查詢名為wa開頭且型別為符號連結的檔案
find   /home   -mtime   -2                 在/home下查最近兩天內改動過的檔案
find /home    -atime -1                  查1天之內被存取過的檔案
find /home -mmin    +60                  在/home下查60分鐘前改動過的檔案
find /home   -amin   +30                  查最近30分鐘前被存取過的檔案
find /home   -newer   tmp.txt             在/home下查更新時間比tmp.txt近的檔案或目錄
find /home   -anewer   tmp.txt            在/home下查存取時間比tmp.txt近的檔案或目錄
find   /home   -used   -2                  列出檔案或目錄被改動過之後,在2日內被存取過的檔案或目錄
find   /home   -user cnscn                列出/home目錄內屬於使用者cnscn的檔案或目錄
find   /home   -uid   +501                  列出/home目錄內使用者的識別碼大於501的檔案或目錄
find   /home   -group   cnscn              列出/home內組為cnscn的檔案或目錄
find   /home   -gid 501                   列出/home內組id為501的檔案或目錄
find   /home   -nouser                    列出/home內不屬於本地使用者的檔案或目錄
find   /home   -nogroup                   列出/home內不屬於本地組的檔案或目錄
find   /home    -name tmp.txt    -maxdepth   4   列出/home內的tmp.txt 查時深度最多為3層
find   /home   -name tmp.txt   -mindepth   3   從第2層開始查
find   /home   -empty                     查詢大小為0的檔案或空目錄
find   /home   -size   +512k                查大於512k的檔案
find   /home   -size   -512k               查小於512k的檔案
find   /home   -links   +2                查硬連線數大於2的檔案或目錄
find   /home   -perm   0700                查許可權為700的檔案或目錄
find   /tmp   -name tmp.txt   -exec cat {} \;
find   /tmp   -name   tmp.txt   -ok   rm {} \;

find    /   -amin    -10     # 查詢在系統中最後10分鐘訪問的檔案
find    /   -atime   -2        # 查詢在系統中最後48小時訪問的檔案
find    /   -empty             # 查詢在系統中為空的檔案或者資料夾
find    /   -group   cat        # 查詢在系統中屬於 groupcat的檔案
find    /   -mmin   -5         # 查詢在系統中最後5分鐘裡修改過的檔案
find    /   -mtime   -1       #查詢在系統中最後24小時裡修改過的檔案
find    /   -nouser           #查詢在系統中屬於作廢使用者的檔案
find    /   -user    fred     #查詢在系統中屬於FRED這個使用者的檔案