文件查找

find查找

find 命令的基本语法

find名称查找

#1.创建文件
touch /etc/sysconfig/network-scripts/{ifcfg-eth1,IFCFG-ETH1}

#2.查找/etc目录下包含ifcfg-eth0名称的文件
[root@lqz ~]# find /etc -name "ifcfg-eth1"

#3.-i 忽略大小写
[root@lqz ~]# find /etc -iname "ifcfg-eth1"
#查找/etc目录下包含ifcfg-eth名称所有文件
[root@lqz ~]# find /etc/ -name "ifcfg-eth*"
[root@lqz ~]# find /etc -iname "ifcfg-eth*"

find大小查找

#1.查找大于5M的文件
[root@lqz ~]# find /etc -size +5M

#2.查找等于5M的文件
[root@lqz ~]# find /etc -size 5M

#3.查找小于5M的文件
[root@lqz ~]# find /etc -size -5M

find类型查找

# f 文件
[root@lqz ~]# find /dev -type f
# d 目录
[root@lqz ~]# find /dev -type d
# l 链接
[root@lqz ~]# find /dev -type l
# b 块设备
[root@lqz ~]# find /dev -type b
# c 字符设备
[root@lqz ~]# find /dev -type c
# s 套接字
[root@lqz ~]# find /dev -type s
# p 管道文件
[root@lqz ~]# find /dev -type p

find时间查找

#1.创建测试文件(后期shell会讲)
[root@lqz ~]# for i in {01..28};do date -s  201904$i && touch file-$i;done

#2.查找7天以前的文件(不会打印当天的文件)
[root@lqz ~]# find ./ -iname "file-*" -mtime +7

#3.查找最近7天的文件,不建议使用(会打印当天的文件)
[root@lqz ~]# find ./ -iname "file-*" -mtime -7

#4.查找第7天文件(不会打印当天的文件)
[root@lqz ~]# find ./ -iname "file-*" -mtime 7

#5.本地文件保留最近7天的备份文件, 备份服务器保留3个月的备份文件(实际使用方案)
find /backup/ -iname "*.bak" -mtime +7 -delete
find /backup/ -iname "*.bak" -mtime +90 -delete

find用户查找

#查找属主是jack
[root@lqz ~]# find /home -user jack
#查找属组是admin
[root@lqz ~]# find /home -group admin
#查找属主是jack, 属组是admin
[root@lqz ~]# find /home -user jack -group admin
#查找属主是jack, 并且属组是admin
[root@lqz ~]# find /home -user jack -a -group admin
#查找属主是jack, 或者属组是admin
[root@lqz ~]# find /home -user jack -o -group admin
#查找没有属主
[root@lqz ~]# find /home -nouser
#查找没有属组
[root@lqz ~]# find /home -nogroup
#查找没有属主或属组
[root@lqz ~]# find /home -nouser -o -nogroup

find权限查找

#精切匹配644权限
[root@lqz ~]# find . -perm 644 -ls

#包含444权限即可
[root@lqz ~]# find . -perm -444  -ls

#查找全局可写(每位权限必须包含w)
[root@lqz ~]# find . -perm -222 -ls

#包含set uid
[root@lqz ~]# find  /usr/sbin -perm -4000 -ls

#包含set gid
[root@lqz ~]# find  /usr/sbin -perm -2000 -ls

#包含sticky
[root@lqz ~]# find  /usr/sbin -perm -1000 -ls

find动作处理

![](https://raw.githubusercontent.com/cwyfengyiyuan/images/master/1598062498_20200822101122614_13280.png =720x)

#1.使用-print打印查找到的文件
[root@lqz ~]# find /etc -name "ifcfg*"
[root@lqz ~]# find /etc -name "ifcfg*" -print

#2.使用-ls打印查找到的文件,以长格式显示
[root@lqz ~]# find /etc -name "ifcfg*" -ls

#3.使用-delete删除文件,但仅能删除空目录
[root@lqz ~]# find /etc -name "ifcfg*" -delete

#4.使用-ok实现文件拷贝,但会提示是否拷贝
[root@lqz ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp ;

#5.使用-exec实现文件拷贝和文件删除。
[root@lqz ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp ;
[root@lqz ~]# find /etc -name "ifcfg*" -exec rm -f {} ;

#xargs将前者命令查找到的文件作为一个整体传递后者命令的输入
[root@lqz ~]# touch file.txt
[root@lqz ~]# find . -name "file.txt" |xargs rm -f
[root@lqz ~]# find . -name "file.txt" |xargs -I {} cp -rvf {} /var/tmp

find逻辑运算符

![](https://raw.githubusercontent.com/cwyfengyiyuan/images/master/1598062502_20200822101335840_27415.png =720x)

#1.查找当前目录下,属主不是hdfs的所有文件
[root@lqz ~]# find . -not -user hdfs 
[root@lqz ~]# find . ! -user hdfs
        
#2.查找当前目录下,属主属于hdfs,且大小大于300字节的文件
[root@lqz ~]# find . -type f -a -user hdfs -a -size +300c
            
#3.查找当前目录下的属主为hdfs或者以xml结尾的普通文件
[root@lqz ~]# find . -type f -a ( -user hdfs -o -name '*.xml' )

find相关练习题

1.查找/tmp目录下,属主不是root,且文件名不以f开头的文件
2.查找/var目录下属主为root,且属组为mail的所有文件
3.查找/var目录下不属于root、lp、gdm的所有文件
4.查找/var目录下最近一周内其内容修改过,同时属主不为root,也不是postfix的文件
5.查找/etc目录下大于1M且类型为普通文件的所有文件
6.将/etc/中的所有目录(仅目录)复制到/tmp下,目录结构不变
7.将/etc目录复制到/var/tmp/,/var/tmp/etc的所有目录权限777/var/tmp/etc目录中所有文件权限666
8.保留/var/log/下最近7天的日志文件,其他全部删除
9.创建touch file{1..10}10个文件, 保留file9,其他一次全部删除
10.解释如下每条命令含义
mkdir /root/dir1
touch /root/dir1/file{1..10}
find /root/dir1 -type f -name "file5"
find /root/dir1 ! -name "file5"
find /root/dir1 -name "file5" -o -name "file9"
find /root/dir1 -name "file5" -o -name "file9" -ls
find /root/dir1 ( -name "file5" -o -name "file9" ) -ls
find /root/dir1 ( -name "file5" -o -name "file9" ) -exec rm -rvf {} ;
find /root/dir1  ! ( -name "file4" -o -name "file8" ) -exec rm -vf {}  ;
原文地址:https://www.cnblogs.com/chenwenyin/p/13544657.html