Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1906540
  • 博文数量: 498
  • 博客积分: 2078
  • 博客等级: 大尉
  • 技术积分: 1645
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-18 22:43
个人简介

安大

文章分类

全部博文(498)

文章存档

2017年(1)

2016年(2)

2015年(21)

2014年(90)

2013年(101)

2012年(267)

2011年(16)

分类: LINUX

2014-01-21 10:59:23

原文地址:find命令使用小结 作者:hexel

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

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

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(); 

?>

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

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

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

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

阅读(885) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~