find
四剑客之Find工具实战,Find工具主要用于操作系统文件、目录的查找,其语法参数格式为:
find path -option [ -print ] [ -exec -ok command ] { } \; |
(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 -m perm 644 -exec rm –rf {} \; #查找/data目录以.log结尾,30天以前的文件,大小大于10M并移动到/tmp目录; find /data/ -name "*.log" –type f -mtime +30 –size +10M -exec mv {} /tmp/ \; |