Shell编程四剑客之Find

四剑客之Find工具实战,Find工具主要用于操作系统文件、目录的查找,其语法参数格式为:

find   path   -option   [   -print ]   [ -exec   -ok   command ]   { }  \;

其option常用参数详解如下:

-name   filename                    #查找名为filename的文件;

-type    b/d/c/p/l/f                  #查是块设备、目录、字符设备、管道、符号链接、普通文件;

-size      n[c]                       #查长度为n块[或n字节]的文件;

-perm                              #按执行权限来查找;

-user    username                   #按文件属主来查找;

-group   groupname                 #按组来查找;

-mtime    -n +n                     #按文件更改时间来查找文件,-n指n天以内,+n指n天以前;

-atime    -n +n                          #按文件访问时间来查找文件;

-ctime    -n +n                          #按文件创建时间来查找文件;

-mmin     -n +n                        #按文件更改时间来查找文件,-n指n分钟以内,+n指n分钟以前;

-amin     -n +n                         #按文件访问时间来查找文件;

-cmin     -n +n                         #按文件创建时间来查找文件;

-nogroup                               #查无有效属组的文件;

-nouser                                 #查无有效属主的文件;

-newer   f1 !f2                           #找文件,-n指n天以内,+n指n天以前;

-depth                                  #使查找在进入子目录前先行查找完本目录;

-fstype                                  #查更改时间比f1新但比f2旧的文件;

-mount                                 #查文件时不跨越文件系统mount点;

-follow                                 #如果遇到符号链接文件,就跟踪链接所指的文件;

-cpio                                      #查位于某一类型文件系统中的文件;

-prune                                 #忽略某个目录;

-maxdepth                                         #查找目录级别深度。

(1)、Find工具-name参数案列:

find   /data/    -name   "*.txt"                  #查找/data/目录以.txt结尾的文件;

find   /data/    -name   "[A-Z]*"               #查找/data/目录以大写字母开头的文件;

find   /data/    -name   "test*"                 #查找/data/目录以test开头的文件;

(2)、Find工具-type参数案列:

find   /data/    -type d                         #查找/data/目录下的文件夹;

find   /data/    !   -type   d              #查找/data/目录下的非文件夹;

find   /data/    -type  l                        #查找/data/目录下的链接文件。

find  /data/ -type d|xargs chmod 755 -R      #查目录类型并将权限设置为755;

find  /data/ -type f|xargs chmod 644 -R      #查文件类型并将权限设置为644;

(3)、Find工具-size参数案列:

find   /data/    -size   +1M              #查文件大小大于1Mb的文件;

find   /data/    -size   10M                 #查文件大小为10M的文件;

find   /data/    -size   -1M                  #查文件大小小于1Mb的文件;

(4)、Find工具-perm参数案列:

find   /data/    -perm   755                #查找/data/目录权限为755的文件或者目录;

find   /data/    -perm   -007               #与-perm 777相同,表示所有权限;

find   /data/    -perm   +644         #文件权限在644以上;

(5)、Find工具-mtime参数案列:

atime,access time         文件被读取或者执行的时间;

ctime,change time        文件状态改变时间;

mtime,modify time       文件内容被修改的时间;

find /data/ -mtime +30   -name     "*.log"  #查找30天以前的log文件;

find /data/ -mtime -30    -name     "*.txt"  #查找30天以内的log文件;

find /data/ -mtime 30     -name    "*.txt"     #查找第30天的log文件;

find /data/ -mmin  +30  -name    "*.log"  #查找30分钟以前修改的log文件;

find /data/ -amin  -30   -name    "*.txt"  #查找30分钟以内被访问的log文件;

find /data/ -cmin  30    -name    "*.txt"     #查找第30分钟改变的log文件。

(6)、Find工具参数综合案列:

#查找/data目录以.log结尾,文件大于10k的文件,同时cp到/tmp目录;

find /data/  -name "*.log"  -type f  -size +10k -exec cp {} /tmp/ \;

#查找/data目录以.txt结尾,文件大于10k的文件,权限为644并删除该文件;

find /data/ -name "*.log"  –type f  -size +10k  -perm 644 -exec rm –rf {} \;

#查找/data目录以.log结尾,30天以前的文件,大小大于10M并移动到/tmp目录;

find /data/ -name "*.log"  –type f  -mtime +30 –size +10M -exec mv {} /tmp/ 

posted @ 2019-10-24 09:59  走天涯123  阅读(88)  评论(0编辑  收藏  举报