美文网首页linux运维基础篇
Linux文件查找find day14

Linux文件查找find day14

作者: 静如止水yw | 来源:发表于2019-08-12 19:53 被阅读0次

1.find查找概述
2.find查找示例


一、find查找概述


1.什么是find
find命令用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。
2.find命令的基本语法:

命令 路径 选项 表达式 动作
find [path…] [options] [expression] [action]

二、find查找示例


1.find如何查找文件:
find主要针对文件名称、类型、大小、修改时间等方式进行查找文件。

  • 1)按名称查找
# 1.按照名称进行查找
[root@wyw ~]# find ./ -name "*ens32"
# 2.按照名称(不区分大小写)
[root@wyw ~]# find ./ -iname "*ens32"
  • 2)按文件大小查找size
# 1.查找/etc/目录下大于5M的文件
[root@wyw ~]# find /etc/ -size +5M
# 2.查找/etc/目录下小于5M的文件
[root@wyw ~]# find /etc/ -size -5M
# 3.查找/etc/目录下等于5M的文件
[root@wyw ~]# #find /etc/ -size 5M
  • 3)按文件类型查找-type
参数:
f        文件
d        目录
s        socket套接字文件
l        链接文件
c        字符设备
b        块设备

# 1.查找当前目录下类型是文件的,并且名称跟ens32相关的都列出来
[root@wyw ~]# find /etc/ -type f -iname "ens32" |xargs ls -l
# 2.查找/etc/目录下类型是文件的,大小是大于5M,名称时以.bin结尾的
[root@wyw ~]# find /etc/ -type f -size +5M -name "*.bin"
# 3.查找/etc/目录下类型是文件的,名称是.repo结尾的
[root@wyw ~]# find /etc/ -type f -name "*.repo"
#4.查找/dev下的类型是块设备的,并名称是sda开头的
[root@wyw ~]# find /dev/ -type c -name "tty*"
  • 4)按修改时间进行查找—-mtime
# 1.创建测试文件
[root@wyw ~]# for i in {1..31};do date -s 201908$i && touch file-$i;done
# 2.查找7天以前的时间
[root@wyw ~]# find ./ -iname "file-*" -mtime +7
# 3.查找最近7天的文件
[root@wyw ~]# find ./ -iname "file-*" -mtime -7
# 4.查找第7天文件
[root@wyw ~]# find ./ -iname "file-*" -mtime 7
# 5.本地文件保留最近7天的备份文件,备份服务器保留3个月的备份文件
[root@wyw ~]# find /backup/ -iname "*.bak" -mtime +7 -delete
[root@wyw ~]# find /backup/ -iname "*.bak" -mtime +180 -delete
  • 5)按用户和组进行查找—-user -group -nouser -nogroup
#1.查找属主是Jack
[root@wyw ~]# find /home -user jack
# 2.查找属组是admin
[root@wyw ~]# find /home -group admin 
# 3.查找属主是jacky,属组是jack
 [root@wyw ~]# find /home/ -type d -user jacky group jack
# 4. 查找没有属主
[root@wyw ~]# find /home -nouser
# 5.查找没有属组
[root@wyw ~]# find /home -nogroup
# 6.查找没有属主或属组
[root@wyw ~]# find /-nouser -nogroup

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

动作 含义
-print 打印查找到的内容(默认)—ignore
-ls 以长格式显示的方式打印查找到内容---ignore |xargs ls -l
- 删除操作到的文件(删除目录,仅能删除空目录)---ignore|xargs delete rm -f
-ok 后面跟自定义 shell命令(会提示是否操作)---ignore
-exec 后面跟自定义 shell命令(标准写法 -exec \;)|xargs
[root@wyw ~]# time find ./ -type f -name "file*" | xargs rm -f {} \;
real    0m0.006s
user    0m0.002s
sys     0m0.005s
[root@wyw ~]# time find ./ -type f -name "file*" | xargs rm -f

real    0m0.116s
user    0m0.002s
sys     0m0.125s
# 查找/var/log/ 类型是文件的,并且名称是.log结尾的,并且7天以前 的,然后删除 
[root@wyw ~]# find /var/log/ -type f -name "*.log" -mtime +7 -exec rm -f {} \;
[root@wyw ~]# find /var/log/ -type f -name "*.log" -mtime +7 -delete
[root@wyw ~]# find /var/log/ -type f -name "*.log" -mtime +7 | xargs rm -f

3.find 和grep 的连用

将find查询的文件结果,作为grep的参数
[root@wyw ~]# find /etc/ -type f | xargs grep "log_group" --color=auto 
/etc/audit/auditd.conf:log_group = root

4.find逻辑运算符

符号 作用
-a
-o
-not|!
# 1.查找当前目录下,属主不是root的所有文件
[root@wyw ~]# find /home/ ! -user root -ls
[root@wyw ~]# find /home/ -not -user root -ls
# 2.查找当前目录下,属主属于Jack,并且大小大于1K的文件
[root@wyw ~]# find /home/ -type f -a -user jacky -a -szie +1k
# 3.查找当前目录下的属主为root,或者以wyw结尾的普通文件
[root@wyw ~]# find . -type f -a \( -user hdfs -o -name '*.wyw' \)

相关文章

网友评论

    本文标题:Linux文件查找find day14

    本文链接:https://www.haomeiwen.com/subject/pczujctx.html