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

linux grep命令

2013-03-21 10:43 197 查看

Linux常用命令之一:find

find,find在不指定查找目录的情况下是对整个系统进行遍历查找使用格式 : find [指定查找目录] [查找规则] [查找完后执行的action][指定查找目录]例如:[root@test ~]# find/etc -name "passwd"/etc/passwd/etc/pam.d/passwd[查找规则](1)根据文件名查找# -name //根据文件名查找(精确查找)# -iname //根据文件名查找,但是不区分大小写这里另外介绍下文件名通配的知识*表示 通配任意的字符[root@test ~]# find/etc -type f -name "passwd*"/etc/passwd/etc/passwd-/etc/pam.d/passwd ?表示 通配任意的单个字符[root@test ~]# find/etc -type f -name "passwd?"/etc/passwd-(2),根据文件所属用户和组来查找文件# -user //根据属主来查找文件# -group //根据属组来查找文件[root@test hejp]#find /home/ -user hejp /home/ddd/home/ddd/.bash_logout/home/ddd/.bashrc/home/ddd/.bash_profile/home/hejp/home/hejp/.bash_logout/home/hejp/.bashrc/home/hejp/.bash_profile(3),根据uid 和 gid来查找用户#find /tmp -uid 500 //查找uid是500 的文件#find /tmp -gid 1000// 查找gid是1000的文件(4),-a and -o and –not的使用# -a 连接两个不同的条件(两个条件必须同时满足)[root@test ~]# find/server/scripts/ -name "*.sh" -a -user root/server/scripts/ipv_server.sh # -o 连接两个不同的条件(两个条件满足其一即可)[root@test ~]# find/server/scripts/ -name "*.sh" -O -user root/server/scripts/ipv_server.sh # -not 对条件取反的 [root@test hejp]#find /home/ -name "*.test" -not -user hejp/home/hejp/hello.test(5),根据文件时间戳的相关属性来查找文件我们可以使用stat命令来查看一个文件的时间信息 如下: [root@testhejp]# stat /etc/passwd File: `/etc/passwd' Size: 1467 Blocks: 8 IO Block: 4096 regular fileDevice:803h/2051d Inode: 146713 Links: 1Access:(0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)Access: 2016-03-1310:36:08.809835981 +0800Modify: 2016-03-0321:59:49.717548713 +0800Change: 2016-03-0321:59:49.719585578 +0800#-atime#-mtime#-ctime#-amin#-mmin#-cmin 所以这里atime,mtime,ctime就是分别对应的“最近一次访问时间”“最近一次内容修改时间”“最近一次属性修改时间”,这里的atime的单位指的是“天”,amin的单位是分钟 #find /tmp –atime +5 //表示查找在五天内没有访问过的文件#find /tmp -atime -5 //示查找在五天内访问过的文件 (6),根据文件类型来查找文件-typef // 普通文件d //目录文件l //链接文件b //块设备文件c //字符设备文件p //管道文件s //socket文件 [root@test hejp]#find /home/hejp/ -type f/home/hejp/hello.test/home/hejp/.bash_logout/home/hejp/.bashrc/home/hejp/.bash_profile (7),根据大小来查找文件-size#find /tmp -size 2M //查找在/tmp 目录下等于2M的文件#find /tmp -size +2M //查找在/tmp 目录下大于2M的文件#find /tmp -size -2M //查找在/tmp 目录下小于2M的文件(8),根据文件权限查找文件-perm#find /tmp -perm 755 //查找在/tmp目录下权限是755的文件#find /tmp -perm +222 //表示只要有一类用户(属主,属组,其他)的匹配写权限就行#find /tmp -perm -222 //表示必须所有类别用户都满足有写权限(9),-nouser and -nogroup#find / -nogroup–a –nouser //在整个系统中查找既没有属主又没有属组的文件(这样的文件通常是很危险的,作为系统工程师的我们应该及时清除掉) [查找完执行的action]#-print //默认情况下的动作#-ls //查找到后用ls 显示出来#-ok [commend] //查找后执行命令的时候询问用户是否要执行# -exec[commend] //查找后执行命令的时候不询问用户,直接执行[root@test hejp]#find /home/hejp/ -name "*.test" -exec chmod 777 {} \;[root@test hejp]#ll /home/hejp/total 4-rwxrwxrwx 1 rootroot 3 Mar 3 21:10 hello.test这里要注意{}的使用:替代查找到的文件#find /tmp -atime +30 –execrm –rf {} \; #删除查找到的超过30天没有访问过文件我们也可以使用xargs来对查找到的文件进一步操fnd /server/backup –name”*.tar.gz” –mtime +7 |xargs rm -rf

本文出自 “早起的鸟儿有虫吃” 博客,请务必保留此出处http://hejianping.blog.51cto.com/11279690/1750484
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: