find命令实用小结

find命令在工作中用的是相当多的,下面总结下find命令的常见用法:

1.查找文件或者路径:

[root@hexel ~/shell]#find /etc -name ifcfg-eth2

/etc/sysconfig/network-scripts/ifcfg-eth2

/etc/sysconfig/networking/profiles/default/ifcfg-eth2

/etc/sysconfig/networking/devices/ifcfg-eth2

[root@hexel ~/shell]#find /root/shell/ -name "tim*.log"   --使用通配符

/root/shell/timing.log

[root@hexel ~/shell]#find /root/shell/ -iname "tim*.log"    -- -iname忽略大小写查找

/root/shell/timing.log

/root/shell/TIMING.LOG

[root@hexel ~/shell]#find ./ -iname  "*.log" -o -name "*.sh"  --  -o选项用于或关系查询,默认是and关系

或者

[root@hexel ~/shell]#find   ./ \( -name  "*.log" -o -name "*.sh" \)  --这时候使用\(和\)把括号内部的看成一个整体

./IFS/passwd.sh

./IFS/csv.sh

./timing.log

./taw_capinfo.sh

./file_operation/2_read_ouput.sh

./file_operation/ok.sh

查找不以.log和.sh结尾的文件和目录:

[root@hexel ~/shell]#find   ./  ! \( -name  "*.log" -o -name "*.sh" \) | head

./

./output.session

./IFS

./IFS/passwd

./scriptfifo

./file_operation

./function

./tty

./TIMING.LOG

./user_parameter

[root@hexel ~/shell]#find ./ -path "*sort*"   --   -name用于查找文件,-path可以使用通配符查找文件路径

./sort

./sort/s.sh

./sort/sort.sh

[root@hexel ~/shell]#find ./ -regex "^.*\(\.sh\|\.log\)$" | head -- -regex用于正则匹配,注意转义字符的应用

./IFS/passwd.sh

./IFS/csv.sh

./timing.log

./taw_capinfo.sh

./file_operation/2_read_ouput.sh

./file_operation/ok.sh

./file_operation/1_delete_file.sh

./function/fun_out.sh

./function/parameter.sh

[root@hexel ~/shell]#find ./ -iregex "^.*\(\.sh\|\.LOG\)$" | head    --不考虑大小写

./IFS/passwd.sh

./IFS/csv.sh

./timing.log

./taw_capinfo.sh

./file_operation/2_read_ouput.sh

./file_operation/ok.sh

./file_operation/1_delete_file.sh

./function/fun_out.sh

./function/parameter.sh

./function/function.sh

2 基于目录深度的查找

默认会在指定目录及其所有子目录查找,可以指定目录深度查找。例如,只想查找当前目录下的文件,不想在子目录中查找:

[root@hexel ~/shell]#find ./ -maxdepth 1 -iregex "^.*\(\.sh\|\.LOG\)$"  

./timing.log

./taw_capinfo.sh

./TIMING.LOG

./timing.sh

有时候可能只需要在某个深度目录查找,可以这样:

[root@hexel ~/shell]#find ./ -maxdepth 2 -mindepth 2 -iregex "^.*\(\.sh\|\.LOG\)$"  | head

./IFS/passwd.sh

./IFS/csv.sh

./file_operation/2_read_ouput.sh

./file_operation/ok.sh

./file_operation/1_delete_file.sh

./function/fun_out.sh

./function/parameter.sh

./function/function.sh

./function/variable.sh

./function/fork.sh

3 根据文件类型查找(-type)

例如:

[root@hexel ~/shell]#find ./ -type f  -iregex "^.*\(\.log\|\.ln\)$"  --只列出文件

./timing.log

./TIMING.LOG

[root@hexel ~/shell]#find ./ -type l  -iregex "^.*\(\.log\|\.ln\)$"  --只列出链接

./timing.ln

[root@hexel ~/shell]#find ./ -type p -name "*"

./scriptfifo

还有其他类型;

f:普通文件

d: 目录

l: 符号链接

b: 块设备

s: 套接字文件

c:字符块设备

p: fifo文件

4.基于相关时间搜索

搜索最近五天被访问过的文件;

[root@hexel ~/shell]#find ./ -type f  -name "*.sh" -atime -5

./IFS/passwd.sh

./IFS/csv.sh

./timing.sh

搜索最近五天被修改过的文件

[root@hexel ~/shell]#find ./ -type f  -name "*.sh" -mtime -5 

./timing.sh

搜索最近五天被修改过元数据(权限,所有者等属性)的文件

[root@hexel ~/shell]#find ./ -type f  -name "*.sh" -ctime -5

./taw_capinfo.sh

./timing.sh

注:可以按照分钟搜索,具体同上

-amin:访问分钟

-mmin:修改分钟

-cmin:变化分钟

5.基于文件大小的搜索

这个功能特别有用,例如,查找大于5k大小的文件

[root@hexel ~/shell]#find ./ -type f -size +5k

./xm_l_change/11.tar.gz

./xm_l_change/hx/log.sh

./xm_l_change/hx/11.tar.gz

./tcpdump/121111-00:01:50

./tcpdump/121110-23:38:50

./tcpdump/121110-23:40:35

同理,-5k表示小于5k,5k表示大于等于5k。还可以以b(block),c(字节),w(字),G(吉字节)为单位进行搜索

找到size大于某个数值的文件后,可以使用-delete进行删除操作:

[root@hexel ~/shell]#find ./ -type f -size +50k

./xm_l_change/hx/log.sh

[root@hexel ~/shell]#find ./ -type f -size +50k -delete

查找大于50m的文件,并展示具体的大小(考虑文件可能存在的空格)

hx@huangxing:/home$ find ./ -type f -size +50M -print0 |xargs -0 ls -alh

6.查找特定用户的文件:

[root@hexel /var/www/html/ecshop]#find ./ -type f  -iname "*.php"  -user root

./oracle/oracle.php

./oracle/config.php

./oracle/oracle3.php

./oracle/oracle1.php

./md5.php

7.查找完成后使用-exec执行其他操作

例如,找到上面的文件后,把他们的属主修改为vsftp

[root@hexel /var/www/html/ecshop]#find ./ -type f  -iname "*.php"  -user root  -exec chown vsftp {} \;

[root@hexel /var/www/html/ecshop]#find ./ -type f  -name "*.php" -cmin -5   

./oracle/oracle.php

./oracle/config.php

./oracle/oracle3.php

./oracle/oracle1.php

./md5.php

[root@hexel /var/www/html/ecshop]#find ./ -type f  -name "*.php" -cmin -5 -exec cat {} \; | head

<?php

if ($conn=Ora_Logon("systm@localhost/orcl.localdomain","huida")) 

{ echo "SUCCESS ! Connected to database\n"; 

}

else 

{echo "Failed :-( Could not connect to database\n";} 

Ora_Logoff($conn); 

phpinfo(); 

?>

<?php 

find: “cat” 由于信号 13 而终止

find: “cat” 由于信号 13 而终止

find: “cat” 由于信号 13 而终止

\;后面可以是任何命令,也可以是一个脚本,把输出作为脚本的参数

8.实例-删除某个目录下最后访问时间超过一定值的文件

 

#!/bin/bash
#删除文件方法
##1.使用循环查找比较麻烦
cd /u01/diag/rdbms/hexel/hexel/incident
NOW=$(date +%s)
for FILE in $(find ./ -name *.trc -o  -name *.trm); do
  FILE_CHANGE_DATE=$(date -r $FILE +%s)
  if (( $NOW-$FILE_CHANGE_DATE >= 3600 )); then
    echo -n `date -r $FILE` "  "
    rm $FILE -rvf
  fi
done
cd /u01/diag/rdbms/hexel/hexel/alert
rm * -rf
#2.使用find命令的一些常用方法,注意-o,-mmin -exec \;
find /u01/diag/rdbms/hexel/hexel/trace/ -name  "*.trm" -o -name  "*.trc" \
      -mmin +60  -exec rm -rvf {} \;
#3.上面的方法也不是最好的,因为文件名可能有空格,这个时候可能有错,最好使用xargs,使用null分割
find /u01/diag/rdbms/hexel/hexel/trace/ -name  "*.trm" -o -name  "*.trc" \
      -mmin +1  -print0 | xargs -0 rm -rvf



 
posted @ 2021-05-23 00:14  hexel  阅读(136)  评论(0编辑  收藏  举报