Linux文件查找

深入Linux:学习Linux文件查找_Linux

1.find查找概述

为什么要有文件查找,因为很多时候我们可能会忘了某个文件所在的位置,此时就需要通过find来查找。

find命令可以根据不同的条件来进行查找文件,例如:文件名称、文件大小、文件修改时间、属主属组、权限、等等方式。同时find命令是Linux下必须掌握的。

*find 命令的基本语法如下*

命令

路径

选项

表达式

动作

find

[path…]

[options]

[expression]

[action]

查找

地区

妹纸

18-25岁

约?

是linux里面的一个实时查找工具,通过制定路径完成文件查找

find [options] ..... [查找路径] [查找条件] [处理动作]

查找路径:查找的位置,默认是当前文件夹

查找条件:制定查找的标准,文件名、大小、类型、日期等等

处理动作:对符合条件的文件做什么操作,默认是输出到屏幕上

2.find查找示例

*以下列出所有find常用的选项*

1.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*"

2.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

3.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

4.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

5.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

6.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

查找条件

  • 根据文件名查找
  • -name 指定名称,可以使用正则
  • -iname 忽略大小写
  • -links n 引用次数为n的文件
  • -regex 后面跟完整路径,而不是文件名, 必须整个路径完全匹配
  • 制定搜索的层级
  • -maxdepth level 最大的搜索深度,指定的目录为第1层
  • -mindepth level 最小的搜索深度,包括level层
  • 根据属主、属组来查找
  • -user username 查找属主为username的文件
  • -group groupname 查找属组为groupname的文件
  • -uid id 查找属主为id的文件
  • -gid id 查找属组为id的文件
  • -nouser 查找没有属主的文件
  • -nogroup 查找没有属组的文件
m[root@192 test]#chown qiao b
m[root@192 test]#ll
total 0
drwxr-xr-x. 4 root root 24 Dec  4 22:50 a
-rw-r--r--. 1 qiao root  0 Dec  6 17:53 b
m[root@192 test]#chown :llx b
m[root@192 test]#ll
total 0
drwxr-xr-x. 4 root root 24 Dec  4 22:50 a
-rw-r--r--. 1 qiao llx   0 Dec  6 17:53 b
m[root@192 test]#find -group llx
./b
m[root@192 test]#id root
uid=0(root) gid=0(root) groups=0(root)
m[root@192 test]#id qiao
uid=1000(qiao) gid=1000(qiao) groups=1000(qiao)
m[root@192 test]#find -uid 1000
./b

m[root@192 test]#useradd xiaobao
m[root@192 test]#chown xiaobao b
m[root@192 test]#ll
total 0
drwxr-xr-x. 4 root    root 24 Dec  4 22:50 a
-rw-r--r--. 1 xiaobao llx   0 Dec  6 17:53 b
m[root@192 test]#userdel xiaobao
m[root@192 test]#ll
total 0
drwxr-xr-x. 4 root root 24 Dec  4 22:50 a
-rw-r--r--. 1 1002 llx   0 Dec  6 17:53 b
m[root@192 test]#find -nouser
./b

# 全盘找
m[root@192 test]#find / -nouser
  • 根据文件类型 -type
  • d 目录
  • f 文件
  • l 符号链接
  • s 套接字
  • b 块设备
  • c 字符设备
  • p 管道文件
m[root@192 test]#find -type f
./b
  • 空文件或者空目录
  • -empty
m[root@192 test]#find -empty
  • 条件
  • 与 -a
  • 或 -o
  • 非 -not
m[root@192 test]#find -empty -o -type d
m[root@192 test]#find -empty -not -type d
./b
  • 摩根定律
  • 非(A或者B) 非A 且非B
  • 非(A且B)非A或非B
m[root@192 ~]#find !(-empty -a -tpye d)
  • 排除目录
  • -path
[root@localhost test]#find /etc -name *_config
/etc/ssh/ssh_config
/etc/ssh/sshd_config
[root@localhost test]#find /etc -path /etc/ssh -name *_config
  • 按照大小来查找
  • -size # (#-1,#] 不包括#-1,包括#
  • -size -# [0,#-1] 包括#-1
  • -size +# (#,......)
  • 按照时间来查找
  • -atime # [#,#+1)
  • -atime -# (0,#)
  • -atime +# [#+1,....]
  • 查找7天以后的文件 find -atime +7
  • -mtime
  • -ctime
  • 以分钟为单位
    • -amin
    • -mmin
    • -cmin

3、处理动作

find动作处理,比如查找到一个文件后,需要对文件进行如何处理, find的默认动作是 -print

1.find查找后的动作命令示例

动作

含义

-print

打印查找到的内容(默认)

-ls

以长格式显示的方式打印查找到的内容

-delete

删除查找到的文件(仅能删除空目录)

-ok

后面跟自定义 shell 命令(会提示是否操作)

-exec

后面跟自定义 shell 命令(标准写法 -exec 😉

- -print 默认的处理动作,显示在屏幕上
- -ls 类似于ls -l 显示长格式
- -delete 删除查找到的文件
- -fls file 将查找的结果以长格式保存到文件中
- -ok command {} \; 对每一个查找到的文件执行command命令,在执行命令之前要先提示用户是否要执行

find -size 2M -ok rm -rf {} \;
找到2M的文件,删除,提示删除
  • -exec command {} \; 对查到的每一个文件执行command命令,不需要确认,一次性交给后面命令处理
find -size 2M -exec rm -rf {} \;
m[root@192 test]#find -size 2M -delete
#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 {} \;

2.使用find命令结合xargs

  • 有的命令不支持管道
  • 命令参数过长
  • xargs 将管道前面的内容一条一条的交给后面命令处理
echo file{1..50000}|xargs touch
  • 一般会跟find使用
#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

3.find逻辑运算符

符号

作用

-a

-o

-not|!

#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' \)

4.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 {}  \;

关注 工 仲 好:IT运维大本营获取60个G的《网工系统大礼包》+1000页Linux学习笔记