13.Linux文件查找-find命令
find 命令的基本语法如下:
| 命令 | 路径 | 选项 | 表达式 | 动作 |
|---|---|---|---|---|
| find | [path...] | [options] | [expression] | [action] |
| 查找 | 地区 | 妹纸 | 18-25岁 | 约? |
(1) 按名称查找
1.按照名称进行查找
[root@yinwucheng ~]# find ./ -name "*eth0"
2.按照名称查找(不区分大小写)
[root@yinwucheng ~]# find ./ -iname "*eth0"
(2) 按文件大小查找 size
1.查找/etc/目录下大于5M的文件
[root@yinwucheng ~]# find /etc/ -size +5M
2.查找/etc/目录下小于5M的文件
[root@yinwucheng ~]# find /etc/ -size -5M
3.查找/etc/目录下等于5M的文件
[root@yinwucheng ~]# find /etc/ -size 5M
(3)按文件类型查找 -type
f 文件
d 目录
s socket套接字文件
l 链接文件
c 字符设备
b 块设备
1.查找当前目录下类型是文件的,并且名称跟eth0相关的都列出来
[root@yinwucheng~]# find ./ -type f -iname "*eth0" |xargs ls -l
2.查找/etc/目录下类型是文件的,大小是大于5M,名称以.bin结尾的
[root@yinwucheng ~]# find /etc/ -type f -size +5M -name "*.bin"
3.查找/etc/目录下类型是文件的,名称是.repo结尾的
[root@yinwucheng ~]# find /etc/ -type f -name ".repo"
4.查找/dev下的类型是块设备的,并名称是sda开头的
[root@yinwucheng ~]# find /dev/ -type b -name "sda*" |xargs ls -l
5.查找/dev下的类型是字符设备,并名称是tty开头的
[root@yinwucheng ~]# find /dev/ -type c -name "tty*"
(4) 环境准备(shell 依次创建1-31的文件)
[root@yinwucheng ~]# for i in {1..31}; do date -s"2019/08/$i" ; touch file-$i ; done
1.第7天
[root@yinwucheng ~]# find ./ -type f -mtime 7
2.7天以前的内容筛选出来,然后删除. 保留最近7天的内容
[root@yinwucheng ~]# find ./ -type f -mtime +7 -name "file-*" |xargs rm -rf
3.最近7天的内容都会被筛选出来
[root@yinwucheng ~]# find ./ -type f -mtime -7 -name "file-*"
4.本地文件保留最近7天的备份文件, 备份服务器保留3个月的备份文件(实际使用方案)
find /backup/ -iname "*.bak" -mtime +7 -delete
find /backup/ -iname "*.bak" -mtime +180 -delete
(5) 按用户和组进行查找 -user -group -nouser -nogroup
查找属主是jack
[root@yinwucheng ~]# find /home -user jack
查找属组是admin
[root@yinwucheng ~]# find /home -group admin
查找属主是jacky, 属组是jack
[root@yinwucheng ~]# find /home/ -type d -user jacky -group jack
查找没有属主
[root@yinwucheng ~]# find /home -nouser
查找没有属组
[root@yinwucheng ~]# find /home -nogroup
查找没有属主或属组
[root@yinwucheng ~]# find /home -nouser -nogroup
6.find查找后的处理动作?
查找到一个文件后,需要对文件进行如何处理,find的默认动作是 -print
| 动作 | 含义 |
|---|---|
| 打印查找到的内容(默认) ---ignore | |
| -ls | 以长格式显示的方式打印查找到的内容 ---ignore |xargs ls -l |
| -delete | 删除查找到的文件 (删除目录,仅能删除空目录) ---ignore xargs|rm -f |
| -ok | 后面跟自定义 shell 命令(会提示是否操作) ---ignore |
| -exec | 后面跟自定义 shell 命令(标准写法 -exec \;)| xargs |
[root@yinwucheng ~]# time find ./ -type f -name "file*" -exec rm -f {} \;
real 0m6.585s
user 0m3.617s
sys 0m3.532s
[root@yinwucheng ~]# time find ./ -type f -name "file*" | xargs rm -f
real 0m0.152s
user 0m0.016s
sys 0m0.146s
查找/var/log/ 类型是文件的,并且名称是.log结尾的,并且7天以前的,然后删除
[root@yinwucheng ~]# #find /var/log/ -type f -name "*.log" -mtime +7 -exec rm -f {} \;
[root@yinwucheng ~]# #find /var/log/ -type f -name "*.log" -mtime +7 -delete
[root@yinwucheng ~]# #find /var/log/ -type f -name "*.log" -mtime +7 | xargs rm -f
3.记得文件的内容是什么,但是不清楚文件名称是什么,也不知道路径在哪,怎么办?
- find 是查询文件
- grep 过滤内容
将ifnd查询的文件结果,作为grep的参数
[root@yinwucheng ~]# 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@yinwucheng ~]# find /home/ ! -user root -ls
[root@yinwucheng ~]# find /home/ -not -user root -ls 使用较少
2.查找当前目录下,属主属于jack,并且大小大于1k的文件
[root@yinwucheng ~]# find /home/ -type f -a -user jacky -a -size +1k
3.查找home目录下的属主为lisi 或者 以xml结尾的普通文件
[root@yinwucheng ~]# find /home -type f -a \ ( -user lisi -o -name '*.xml' \)
5.find练习
1.查找/var目录下,属主不是root,且文件名不以f开头的文件
[root@yinwucheng ~]# find /var/ -type f ! -user root -a ! -name "f*"
2.查找/var目录下属主为root,且属组为mail的所有文件
[root@yinwucheng ~]# find /var/ -type f -user root -a -group mail
3.查找/var目录下不属于root、lp的所有文件
[root@yinwucheng ~]# find /var/ -type f ! -user root -a ! -user lp
4.查找/var目录下最近一周内产生的文件,同时属主不为root,也不是postfix的文件
[root@yinwucheng ~]# find /var/ -type f -mtime -7 ! -user root ! -name "postfix"
5.查找/etc目录下大于1M且类型为普通文件的所有文件
[root@yinwucheng ~]# find /etc/ -type f -size +1M
6.将/etc/中的所有目录(仅目录)复制到/tmp下,目录结构不变
[root@yinwucheng ~]# find /etc/ -type d -exec mkdir -p /tmp/ {} \;
7.将/etc目录复制到/var/tmp/,/var/tmp/etc的所有目录权限777/var/tmp/etc目录中所有文件权限666
[root@yinwucheng ~]# cp /etc/ /var/tmp/ -rp
[root@yinwucheng ~]# find /var/tmp/etc/ -type d -exec chmod 777 {} \;
[root@yinwucheng ~]# find /var/tmp/etc/ -type f -exec chmod 666 {} \;
8.保留/var/log/下最近7天的日志文件,其他全部删除
[root@yinwucheng ~]# find /var/log/ -type f -mtime +7 -exec rm -f {} \;
今日总结
1.find 查找文件
文件名称
文件大小
文件类型
修改时间
用户用户组
2.find处理动作
-print 忽略 默认送
-ls 长格式显示,不能加参数,所以忽略
-delete 删除文件,删除目录必须确保目录为空
-ok 执行command命令,但会提示,忽略
-exec 执行command命令
9.创建touch file{1..10}10个文件, 保留file9,其他一次全部删除
[root@oldboyedu ~]# find ./ -type f -name "file*" ! -
name "file9" -exec rm -f {} \;
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 -vf {} \;
find /root/dir1 ! \( -name "file4" -o -name "file8" \) -exec rm -vf {} \;
13.Linux文件查找-find命令的更多相关文章
- linux文件查找find命令
linux文件查找find命令 1.文件查找 基本介绍 在文件系统上查找符合条件的文件 linux上常见的文件查找工具:find命令 查找分类 实时查找 精确查找 基本语法 find [option ...
- linux 文件查找 find命令详解
一,从索引库查找文件:locate 索引库:操作系统会周期性的遍历根文件系统,然后生成索引库 手动更新索引库:updatedb 语法:locate [OPTION]... PATTERN... 只匹配 ...
- linux文件查找-find命令
find命令:用于在文件树中查找文件,并作出相应的处理 1.find命令的格式: find pathname -options [-print -exec -ok ...] {}\ 2.find命令的 ...
- Linux下的文件查找类命令(转载)
如何快速有效的定位文件系统内所需要查找的文件呢?Linux为我们提供了一些文件查找类的命令,我们需要掌握以下几个命令: http://blog.csdn.net/sailor201211/articl ...
- Linux文件查找命令find,xargs详述【转】
转自:http://blog.csdn.net/cxylaf/article/details/4069595 转自http://www.linuxsir.org/main/?q=node/137 Li ...
- Linux文件查找命令find用法整理(locate/find)
Linux文件查找查找主要包括:locate和find 1.locate 用法简单,根据数据库查找,非实时,用法: locate FILENAME 手动更新数据库(时间可能较长) updatedb 2 ...
- Linux文件查找.md
Linux 文件查找 在Linux系统的查找相关的命令: which 查看可执行文件的位置 whereis 查看文件的位置 locate 配合数据库查看文件位置 find 实际搜寻硬盘查询文件名称 w ...
- 第1章 Linux文件类基础命令
1. 关于路径和通配符 Linux中分绝对路径和相对路径,绝对路径一定是从/开始写的,相对路径不从根开始写,还可能使用路径符号. 路径展开符号: . :(一个点)表示当前目录 .. :(两个点)表示上 ...
- linux 文件查找,which,whereis,locate,find
linux 文件查找,which,whereis,locate,find 一:which 主要用于查找可执行命令的所在位置: 如图,查找命令 ls的目录: 二:whereis 主要用于查找命令的帮助文 ...
随机推荐
- PTA A1011&A1012
A1011 World Cup Betting (20 分) 题目内容 With the 2010 FIFA World Cup running, football fans the world ov ...
- java教程系列二:Java JDK,JRE和JVM分别是什么?
多情只有春庭月,犹为离人照落花. 概述 本章主要了解JDK,JRE和JVM之间的区别.JVM是如何工作的?什么是类加载器,解释器和JIT编译器.还有一些面试问题. Java程序执行过程 在深入了解Ja ...
- PMP全真模拟题真题試題含答案解析 2019年下半年PMP考試适用 PMP中文文对照试题 【香港台灣地區PMP考試也可用】
PMP全真模拟题真题试题 含答案解析 2019年下半年PMP考试适用 PMP中文文对照试题 [香港台灣地區PMP考試也可用]PMP全真模擬題真題試題 含答案解析 2019年下半年PMP考試适用 PMP ...
- opencv边缘检测
人眼怎么识别图像边缘? 比如有一幅图,图里面有一条线,左边很亮,右边很暗,那人眼就很容易识别这条线作为边缘.也就是像素的灰度值快速变化的地方. sobel算子 sobel算子是一个离散差分算子. 图像 ...
- Idea 配置Jrebel热部署
虽说Idea自带热更新功能,但是一旦mapper更改,则不能及时更新,影响开发效率. 接下来,我们来配置Jrebel热更新,简单方便实用. 第一步:进入插件下载页面. 第二步:安装jrebel插件. ...
- Spring IOC(3)----bean实例化
前面一节说到invokeBeanFactoryPostProcessors方法的调用来注册bean定义,这次来看看finishBeanFactoryInitialization这个方法实例化非懒加载的 ...
- ShaderHelper2 组件升级,支持自动枚举参数!
ShaderHelper2 组件新体验视频演示: https://www.bilibili.com/video/av69314195/ ShaderHelper2 组件我们已经介绍过两次了,不了解的伙 ...
- 最近太多人问Protobuf的问题了,把这个重新搬出来!
pb杀手 我先让pbkiller做个自我介绍 pbkiller:我是一位专业的争对 protobuf 问题训练有素的杀手,我可以为您轻松搞定 protobuf 在 Cocos Creaotr 开发中的 ...
- *.pvr.ccz文件还原成png格式
处于学习的目的,解包学习某个游戏的资源.大部分的素材都是png文件.但是一部分关键的是用的pvr.ccz文件. 百度一下知道这个文件是TexturePacker打包出来的文件,于是就又百度到了解决办法 ...
- Windows中0环与3环通信(常规方式)
Windows内核分析索引目录:https://www.cnblogs.com/onetrainee/p/11675224.html 一.知识点讲解 1. 设备对象 我们在开发窗口程序的时候,消息被封 ...