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 –user xu"所查找到的內容
查找/server/scripts/下以.sh結尾的文件,並篩選出mysql
[root@fuzj fuzj]# 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,建議使用后者清理日志文件
find 排除文件或目錄
把當前目錄下排除 以.php結尾的文件和.sh結尾的文件,並刪除剩余的文件
find ./ -type f ! -name "*.php" -type f ! -name "*.sh" -exec rm -fr {} ;\