• find 命令详解


    文件查找-find

    find命令的基本语法

    命令 路径 选项 表达式 动作
    find path options express action
    [root@oldboy ~]# find /etc/ -name '*.sh'
    /etc/profile.d/colorgrep.sh
    /etc/profile.d/colorls.sh
    /etc/profile.d/which2.sh
    /etc/profile.d/less.sh
    /etc/profile.d/256term.sh
    

    根据文件名查找 -name


    选项:	-name 'filename'
    
    # 包含ifcfg-eth0的文件
    [root@oldboy ~]# find /etc/sysconfig/network-scripts/ -name 'ifcfg-eth0'
    /etc/sysconfig/network-scripts/ifcfg-eth0
    
    [root@oldboy ~]# find /etc/sysconfig/network-scripts/ -name 'ifcfg-eth*'
    /etc/sysconfig/network-scripts/ifcfg-eth0
    /etc/sysconfig/network-scripts/ifcfg-eth2
    
    [root@oldboy ~]# find /etc/ -iname 'ifcfg-eth*'		# -iname 忽略大小写
    /etc/sysconfig/network-scripts/ifcfg-eth0
    /etc/sysconfig/network-scripts/ifcfg-eth2
    /etc/sysconfig/network-scripts/ifcfg-eth3
    /etc/sysconfig/network-scripts/ifcfg-ETH3
     
    [root@oldboy ~]# find /etc/ -name '*eth*'	# 包含eth的文件
    [root@oldboy ~]# find /etc/ -iname '*eth*'	# 包含eth的文件,不区分大小写
    注:不管是开头还是结尾eth都会被匹配
    

    根据数据大小查找 -size


    选项:	-size n
                  `b'    for 512-byte blocks (this is the default if no suffix is used)
                  `c'    for bytes
                  `w'    for two-byte words
                  `k'    for Kilobytes (units of 1024 bytes)
                  `M'    for Megabytes (units of 1048576 bytes)
                  `G'    for Gigabytes (units of 1073741824 bytes)
    
    # 查找大于5M的文件
    [root@gong ~]# find /etc/ -size +5M
    /etc/udev/hwdb.bin
    [root@gong ~]# find /etc/ -size +5M|xargs du -sh
    7.6M	/etc/udev/hwdb.bin
    [root@gong ~]# find /etc/ -size +5M -delete
    -delete 后面的动作,删除
    -exec
    
    # 查找等于5M的文件
    [root@gong ~]# find /etc/ -size 5M
    
    # 查找小于5M的文件
    [root@gong ~]# find /etc/ -size -5M -ls
    -ls 后面添加的动作-查看
    
    # 文件大小和名字组合起来使用
    [root@gong ~]# find /etc/ -size -5M -name '*.sh'
    
    # 需求:在/etc/ 找到.sh和.conf结尾的文件,并且小于5M
    [root@gong ~]# find /etc/ -size -5M -name '*.sh' -o -name '*.rules'
    

    根据文件类型查找 -type


    文件类型:
    	l d - s p b
    # 根据文件类型查找
    	-type
    		b	块设备
    		f	file文件
    		d	dire目录
    		c	字符设备
    		p	管道文件
    		l	链接文件
    		s	socket文件
            
    [root@oldboy ~]# find /etc/ -type f
    [root@oldboy ~]# find /etc/ -type d
    [root@oldboy ~]# find /etc/ -type l
    [root@oldboy ~]# find /etc/ -type f -ls
    [root@oldboy ~]# find /etc/ -type f |xargs ls -l
    

    条件语句


    -a		并且
    [root@oldboy ~]# find /etc/ -size +3M -a -size -5M -name '*.txt'
    /etc/ff.txt
    
    -o		或者
    [root@oldboy ~]# find /etc/ -size -5M -name '*.sh' -o -name '*.rules'
    
    
    !		取反
    find /etc/ ! ( -size +5M  -o -name '*eth0' )
    
    

    根据日期查找 -mtime


    选项:	-mtime
    
    # 查找7天前的文件,不包括第七天(不包括今天)
    for i in `seq -w 30`
    do 
    date -s "202004$i"
    touch /tmp/file$i.log
    done
    
    [root@oldboy /tmp]# find /tmp/ -mtime +7
    
    
    # 查找进7天的文件,不包含前第七天的
    [root@oldboy /tmp]# find /tmp/ -mtime -7
    
    # 查找第七天的内容(不包含今天的)
    [root@oldboy /tmp]# find /tmp/ -mtime 7
    /tmp/file23.log
    
    # 企业需求,只保留七天的备份
    find /opt ! -mtime -7 -name '*.log' -delete
    [root@oldboy /tmp]# find /tmp/ ! -mtime -7 |xargs rm -fr
    
    

    动作


    -ls
    -exec
    -delete
    

    find动作很少使用。


    find根据用户查找文件


    -user		# 查找属主是某个用户
    -group		# 查找属组是哪个属组的
    -nouser		# 查找没有用户
    -nogroup	# 查找没有属组
    
    [root@oldboy /tmp]# find / -type f -user gong| xargs ls -l
    
    [root@oldboy /tmp]# find / -type f -user gong
    /home/gong/gong.txt
    
    [root@oldboy /tmp]# find / -type f -user gong -group jiang
    
    [root@oldboy /tmp]# find / -type f -user gong -o -group jiang
    /home/jiang/xia.txt
    /home/gong/gong.txt
    
    

    find根据深度查找


    -maxdepth
    
    [root@oldboy /tmp]# find /etc/ -maxdepth 2 -type f -name '*.conf'
    
    

    find根据文件权限查找


    -perm
    [root@oldboy /tmp]# find /tmp -perm 755 -ls   精确的匹配权限位
    
    find /tmp -perm -222 -ls		# 属主、属组、其它都包含w权限的
    
    [root@oldboy /tmp]# find /tmp -perm /442 -ls # 属主位至少有4,或者属组位至少有4,或者其它至少有2
    
    

    find动作


    -print      # 打印出查找的内容但是默认就可以打印。
    -ls			# 找出的结果打印详细信息
    find /opt/ -type f -perm 222 -ls
    
    -delete		# 默认会删除空目录,能删除文件。
    find ./ -type f -mtime +20 -delete
    find ./ -type f -mtime +5|xargs rm -fr
    
    
    -ok			
        find /opt/ -type f -name '*.log' -ok cp {} /tmp ;   # 会在操作的时候询问
        < cp ... /opt/file01.log > ? y
    -exec
    
    find ./ -mtime +5 |xargs -i cp {} /opt
    find /tmp -mtime +5 -exec cp {} /opt ;
    find ./ -mtime +5 |xargs cp -t /opt
     find /opt/ -mtime +5 |xargs -I {} cp {} /tmp/
     
     
    # find结合xargs使用
    # 拷贝
    find / -type f |xargs cp -t /tmp
    
    # 查看
    find / -type f |xargs ls -l
    
    # 替换
    find / -type f |xargs sed -i 's#a##Ag'
    
    # 移动
    find / -type f |xargs mv -t /tmp
    
    # 删除
    find / -type f | xargs rm -fr
    
  • 相关阅读:
    CentOS6.4 安装nmon
    CentOS6.4 访问域局网中Windows的共享
    将类似 12.56MB 36.89KB 转成 以K为单位的数字【备忘】
    ICE中间件相关
    HDFS介绍
    漫画描述HDFS工作原理
    离线安装Cloudera Manager 5和CDH5
    storm集群相关资料
    kafka相关资料
    jstatd
  • 原文地址:https://www.cnblogs.com/gshelldon/p/13276215.html
Copyright © 2020-2023  润新知