find命令

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找到的文件 

 转意 

;表示本行指令结束 

-print

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 {} ;

原文地址:https://www.cnblogs.com/pycode/p/8734258.html