find查找指令
常見參數:
-name 根據檔案名尋找檔案
-user 根據檔案擁有者尋找檔案
-group 根據檔案所屬組尋找檔案
-perm 根據檔案權限尋找檔案
-size 根據檔案大小尋找檔案[±Sizek]
-type 根據檔案類型尋找檔案,常見類型有: f(普通檔案) 、c(字元裝置檔案)、b(塊裝置檔案)、l(符号連結)、d(目錄)、s(套接字)
+n n天以外
-n n天以内
n 不加+或-表示目前
基于目錄深度搜尋
例如:find / -maxdepth 1 type f 隻列出目前目錄下的所有普通檔案,即使有子目錄,也不會被列印,與之類似,-maxdepth 2 最多向下周遊兩級子目錄
-mindepth類似于-maxdepth,他設定的是最小周遊的深度。
根據檔案時間進行搜尋
通路時間:-atime
修改時間:-mtime
變化時間:-ctime
邏輯操作符
-a | and && 與 | 兩個都比對 |
-o | or || 或 | 兩個隻比對一個 |
-not | ! 非 | 反向比對 |
! | 和-not作用一樣 |
對查找到的檔案進一步操作
文法
find [路徑] [參數] [表達式] -exec 指令 {} \;
{}代表find找到的檔案
\ 轉意
;表示本行指令結束
find查找到的檔案輸出到标準輸出
-exec command {} \;
對查找到的檔案執行操作(回車後沒有系統提示,直接執行)
-ok command {} \;
對查找到的檔案執行操作(會有系統提示,是否執行該操作)
例:find /etc name "host*" exec du h {} \; 、
舉例
find指令基礎執行個體
查找/etc/目錄下,檔案名為services的檔案
[root@fuzj fuzj]# find /etc -name services
/etc/services
/etc/logwatch/conf/services
/etc/logwatch/scripts/services
/etc/avahi/services
查找/etc和/usr目錄下目錄名為services的,查找多個路徑時用空格隔開
[root@fuzj fuzj]# find /etc /usr -type d -name services
/etc/logwatch/conf/services
/etc/logwatch/scripts/services
/etc/avahi/services
/usr/share/dbus-1/services
/usr/share/logwatch/dist.conf/services
/usr/share/logwatch/default.conf/services
/usr/share/logwatch/scripts/services
find模糊查找(*)通配符
查找/tmp目錄下所有以sh結尾的檔案,星号比對需要用雙引号引起來
[root@fuzj~]# find /tmp –type f -name "*.sh"
/tmp/fuzj/check_web_url.sh
/tmp/fuzj/a.sh
/tmp/fuzj/b.sh
/tmp/fuzj/test.sh
下面是使用(*)比對的三種方法
[root@fuzj~]# find /tmp –type f -name "*.sh" #雙引号,推薦使用
[root@fuzj~]# find /tmp –type f -name '*.sh' #單引号
[root@fuzj~]# find /tmp –type f -name \*.sh #屏蔽符
如果不加雙引号或單引号或屏蔽符,執行錯誤如下
[root@fuzj~]# find /tmp -name *.sh
find:paths must precede expression
Usage:find [-H] [-L] [-P] [path...] [expression]
查找目前目錄下的檔案中有club.xywy.com字樣的檔案
find./ -type f | xargs grep "club.xywy.com"
或者
grep“club.xywy.com” ./*
find執行動作
查找/tmp下屬主為xu的目錄和檔案
[root@fuzj~]# find /tmp/ -user xu
/tmp/httpd-manual-2.2.3-31.el5.i386.rpm
/tmp/.aaa.sh.swp
/tmp/ssh-myAmp14104
/tmp/ssh-myAmp14104/agent.14104
查找/tmp下屬主為xu的檔案,并删除它們
[root@fuzj~]# find /tmp/ -user xu -ok rm -rf {} \;
<rm ... /tmp/httpd-manual-2.2.3-31.el5.i386.rpm > ?
說明:使用-ok動作時,系統會提示是否執行該操作?而-exec則不會提示,回車後立即執行
後面的{}表示執行"find /tmp –userxu"所查找到的内容
查找/server/scripts/下以.sh結尾的檔案,并篩選出mysql
[root@fuzjfuzj]# find /server/scripts/ -type f -name "*.sh" -exec grep"mysql" {} \;
MYSOCK=/usr/local/mysql/tmp/mysql.sock
LOG_FILE=${DATA_PATH}/mysqllogs_`date+%F`.log
…skip…
grep過濾find查找到的檔案的關鍵字
查找/var下大小超過10M的檔案,并顯示出來
[root@fuzj~]# find /var -type f -size +10M –print
/var/lib/rpm/Packages
/var/cache/yum/base/filelists.xml.gz.sqlite
find邏輯操作符
[root@fuzj~]# find /etc/ -type f -name hosts -a -name services
說明:-a的參數一直沒有做出來,不知道這樣寫對不對
[root@fuzj~]# find /etc/ -type f -name hosts -o -name services
/etc/services
/etc/sysconfig/networking/profiles/default/hosts
/etc/logwatch/conf/services
/etc/logwatch/scripts/services
/etc/avahi/services
/etc/avahi/hosts
/etc/hosts
[root@fuzj~]# find /etc/ -type f -name hostsaaa -o -name services
/etc/services
/etc/logwatch/conf/services
/etc/logwatch/scripts/services
/etc/avahi/services
說明:沒有hostsaaa檔案,是以隻顯示了services的檔案
[root@fuzj~]# find /tmp/ -not -type d
/tmp/web_check.log
/tmp/check_mysql.log
…skip…
說明:/tmp目錄下不為目錄的全部顯示出來
[root@fuzj~]# find /server/scripts/ -type f ! -name "*.sh"
/server/scripts/tmp/fenfakey.exp
/server/scripts/tmp/for-example/i
/server/scripts/tmp/EOF
…skip…
說明:/server/scripts目錄下所有的檔案不為.sh結尾的檔案全部顯示出來
查找大小為10M-15M之間的檔案
[root@fuzj~]# find / -size +10M -a -size -15M
/etc/selinux/targeted/modules/previous/base.pp
/etc/selinux/targeted/modules/active/base.pp
…skip…
find指定目錄的深度進行查找
搜尋目前目錄的檔案
[root@fuzj~]# find /tmp/ -maxdepth 1 -type f
/tmp/etc.bak.tar.gz
/tmp/b.sh
/tmp/test
/tmp/a.sh
/tmp/aaa
/tmp/c.sh
指定目錄的深度為1也就是目前搜尋的目錄
搜尋目前目錄和第一級子目錄的檔案
[root@fuzj~]# find /tmp/ -maxdepth 2 -type f
/tmp/etc.bak.tar.gz
/tmp/b.sh
/tmp/test
/tmp/a.sh
/tmp/aaa
/tmp/fuzj/etc_bak.tar.gz
/tmp/fuzj/file1.gz
/tmp/fuzj/file2
/tmp/fuzj/etc_bak.tar.bz2
/tmp/fuzj/aaa
說明:指定目錄的深度為2也就是搜尋的目錄的下一級,同時也包括目前搜尋的目錄
[root@fuzj~]# find /logs -type f -mtime +7 -exec rm -rf {} \;
[root@fuzj~]# find /logs -type f -mtime +7 |xargs rm –rf
說明:如果日志檔案超多,且大小超過百G,建議使用後者清理日志檔案