您的位置:首页 > 运维架构 > Linux

linux下find命令小结

2014-01-21 16:37 309 查看
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.sh2 基于目录深度的查找默认会在指定目录及其所有子目录查找,可以指定目录深度查找。例如,只想查找当前目录下的文件,不想在子目录中查找:[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.sh3 根据文件类型查找(-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 -delete6.查找特定用户的文件:[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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: