阅读 299

find 命令详解

文件查找-find

find命令的基本语法

命令路径选项表达式动作
findpathoptionsexpressaction
[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


文章分类
后端
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐