首页 > 其他分享 >三剑客详解之find

三剑客详解之find

时间:2024-08-17 19:16:54浏览次数:5  
标签:rw -- 详解 test txt root find 三剑客

一、文件类型:

  • f:表示普通文件
  • b:表示块设备
  • c:表示字节文件
  • d:标识目录
  • l:标识软链接

二、实践案例:

1、准备工作:

[root@web01 web_test]# ll
total 0
-rw-r--r--. 1 root root 0 Aug 17 18:32 1.txt
-rw-r--r--. 1 root root 0 Aug 17 18:32 2.txt
-rw-r--r--. 1 root root 0 Aug 17 18:32 3.txt
-rw-r--r--. 1 root root 0 Aug 17 18:32 4.txt
-rw-r--r--. 1 root root 0 Aug 17 18:32 5.txt
-rw-r--r--. 1 root root 0 Aug 17 18:32 6.txt
-rw-r--r--. 1 root root 0 Aug 17 18:32 7.txt
drwxr-xr-x. 2 root root 6 Aug 17 18:41 web1
drwxr-xr-x. 2 root root 6 Aug 17 18:41 web2
drwxr-xr-x. 2 root root 6 Aug 17 18:41 web3

2、实践

1、查找所有普通文件
[root@web01 web_test]# find ./ -type f
./1.txt
./2.txt
./3.txt
./4.txt
./5.txt
./6.txt
./7.txt

2、查找所有目录文件
[root@web01 ~]# find web_test/ -type d		# 使用绝对路径
web_test/
web_test/web1
web_test/web2
web_test/web3

3、查找字节文件
[root@web01 ~]# find /dev/ -type c|grep null
/dev/null
[root@web01 ~]# find /dev/ -type c|grep zero
/dev/zero

3、根据需求创建指定1G文件大小文件

[root@web01 ~]# dd if=/dev/zero of=test.txt bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 3.72317 s, 288 MB/s
[root@web01 ~]# ll -d test.txt 
-rw-r--r--. 1 root root 1073741824 Aug 17 18:53 test.txt

三、find按照修改时间查找所需文件/目录

  • atime:访问时间
  • mtime:修改时间[常用]
  • ctime:改变时间

1、实践:

mtime 1	# 修改时间为1天前
mtime 0	# 表示当天1天内
mtime +1	# 表示1天前的

案例1:查找修改时间为10天前的
[root@web01 web_test]# find ./ -mtime +10
./
./1.log
./2.log
./3.log

四、小结:

find ./ -name	# 按照名称查找
find ./ -iname	# 不区分大小写
find ./ -name "*.txt"	# 表示所有的.txt结尾
find ./ -name "oldboy*"	# 表示所有oldboy开头的文件
find ./ -type f	# 按照类型查找
	类型:f d b l
find ./ -inum inode号码
find ./ -size +10M	# 按照大小查找
	# 大小10M 等于10M | +10M 大于10M | -10M 小于10M
find ./ -mtime +7	# 按照文件的修改时间查找
	# 时间 +7 大于7天前 | -7 7天内 | 7 第7天的
find ./ -maxdepth 1 -name 	# 按照深度等级查找

并且关系:-a and
find ./ -type f -name "*.txt"
find ./ -size +10M -size -1G
或者关系:-o or
find ./ -type f -o -size +10M

五、find查找到的内容交给其他命令如何处理

1、使用xargs处理find的结果

1、find查找到的内容进行删除,交给rm处理
xargs后面的别名无效
[root@web01 web_test]# find ./ -name "3.txt" | xargs rm
[root@web01 web_test]# ll
total 0
-rw-r--r-- 1 root root 0 Nov 24 16:43 1.txt
-rw-r--r-- 1 root root 0 Nov 24 16:43 2.txt

查找到内容交给ls查看
[root@web01 web_test]# find ./ -name "2.txt" | xargs ls -l
-rw-r--r-- 1 root root 0 Nov 24 16:43 ./2.txt


2、find查找到的内容进行复制,交给cp命令
[root@web01 web_test]# ll
total 0
-rw-r--r-- 1 root root 0 Nov 24 17:54 10.txt
-rw-r--r-- 1 root root 0 Nov 24 16:43 1.txt
-rw-r--r-- 1 root root 0 Nov 24 16:43 2.txt
-rw-r--r-- 1 root root 0 Nov 24 17:54 45.txt
[root@web01 web_test]# find ./ -name "*.txt" | xargs -i cp {} /opt/		# i属于占位符;如果需要递归查找需要让cp也递归即:cp -r--->递归复制
[root@web01 web_test]# ll /opt/
total 0
-rw-r--r--  1 root root   0 Nov 24 18:00 10.txt
-rw-r--r--  1 root root   0 Nov 24 18:00 1.txt
-rw-r--r--  1 root root   0 Nov 24 18:00 2.txt
-rw-r--r--  1 root root   0 Nov 24 18:00 45.txt


3、find查找到的内容进行复制,交给mv命令
[root@web01 web_test]# find /opt/ -name "*.txt"|xargs -i mv {} /tmp/
[root@web01 web_test]# ll /opt/
total 0
drwxr-xr-x. 9 root root 173 Nov 24 18:02 gitlab
[root@web01 web_test]# ll /tmp/
total 0
-rw-r--r-- 1 root root 0 Nov 24 18:05 10.txt
-rw-r--r-- 1 root root 0 Nov 24 18:05 1.txt
-rw-r--r-- 1 root root 0 Nov 24 18:05 2.txt
-rw-r--r-- 1 root root 0 Nov 24 18:05 45.txt

2、使用exec处理find的结果

1、find查找到的内容进行删除,交给rm处理
[root@web01 web_test]# find ./ -name "1.txt" -exec rm {} \;	# \表示跳棍,用来修饰;表示本意,这样也就修饰了“;”不是用于表示后面还有
[root@web01 web_test]# ll
total 0
-rw-r--r-- 1 root root 0 Nov 24 17:54 10.txt
-rw-r--r-- 1 root root 0 Nov 24 16:43 2.txt
-rw-r--r-- 1 root root 0 Nov 24 17:54 45.txt


2、find查找到的内容进行复制,交给cp命令
[root@web01 web_test]# find ./ -name "*.txt" -exec cp {} /opt/ \;
[root@web01 web_test]# ll /opt/
total 0
-rw-r--r--  1 root root   0 Nov 24 18:16 10.txt
-rw-r--r--  1 root root   0 Nov 24 18:16 2.txt
-rw-r--r--  1 root root   0 Nov 24 18:16 45.txt


3、find查找到的内容进行复制,交给mv命令
[root@web01 web_test]# find /opt/ -name "*.txt" -exec mv {} /tmp/ \;

3、find的结果交给其他命令使用

1、find的结果交给rm命令
[root@web01 web_test]# rm -rf `find ./ -name "2.txt"`
		或者
[root@web01 web_test]# rm -rf $(find ./ -name "2.txt")
[root@web01 web_test]# ll
total 0
-rw-r--r-- 1 root root 0 Nov 24 17:54 10.txt
-rw-r--r-- 1 root root 0 Nov 24 17:54 45.txt


2、find结果交给cp命令
[root@web01 web_test]# cp `find ./ -name "10.txt"· /opt/
[root@web01 web_test]# rm -rf `find /opt/ -type f -mtime +7`		# 7天前的


3、find结果交给ls或者mv
[root@web01 web_test]# mv `find ./ -name "*.txt"` /opt/
[root@web01 web_test]#cd /opt/
[root@web01 opt]# ll
total 0
-rw-r--r--  1 root root   0 Nov 24 17:54 10.txt
-rw-r--r--  1 root root   0 Nov 24 17:54 45.txt

标签:rw,--,详解,test,txt,root,find,三剑客
From: https://www.cnblogs.com/9Dusk/p/18364821

相关文章

  • 详解Xilinx FPGA高速串行收发器GTX/GTP(9)--TX/RX通道
    目录1、TX端的剩余模块1.1、TXPIPEControl1.2、TXGearbox1.3、PCIEBeacon1.4、SATAOOB1.5、PhaseAdjustFIFO1.6、Polarity1.7、PISO1.8、TXPre/PostEmp和10、TXDriver1.9、TXOOBandPCIE1.10、TXDriver1.11、TXPhaseInterpolatorController(包括12......
  • 详解WizTree:一款企业级信赖的磁盘空间管理利器!
    前言你是否曾为电脑里那些“不速之客”而烦恼?那些占用大量空间,却又不知所踪的文件和文件夹,是不是让你倍感头疼?今天小江湖就介绍一款超级给力的神器——WizTree! 它就像是电脑空间管理领域的超级侦探,能够迅速而准确地找出你硬盘上的“空间吸血鬼”;无论它们藏得多深,多隐蔽,都......
  • Linux系统优化详解
    一、Linux操作系统优化1、查看操作系统版本号方法一:查看当前系统版本[root@web01~]#cat/etc/redhat-releaseCentOSLinuxrelease7.9.2009(Core)方法二:[root@web01~]#hostnamectlStatichostname:oldboyIconname:computer-vmChassis:......
  • 易优cms目录名称与系统内置冲突,去掉限制方法详解!
    第一步,屏蔽检测文件文件位置:\application\admin\controller\Arctype.php找到代码病注释掉 if(!empty($post['dirname'])&&!$this->arctypeLogic->dirname_unique($post['dirname'],$post['id'])){$arctype_is_......
  • UART 通信协议详解
    目录一、概述二、UART详解1、数据通信的基本概念1.1数据通信方式1.2数据传输方向1.3数据同步方式1.4通信速率2、UART协议2.1串口连接2.2串口协议帧一、概述UART(UniversalAsynchronousReceiver/Transmitter,通用异步收发器)是一种常用的串行通信协议,......
  • SPI 通信协议详解
    目录一、概述二、SPI详解1、基本信息1.1SPI的引脚信息1.2SPI的工作原理1.3SPI的传输方式2、SPI的工作原理3、SPI的工作模式3.1SPI时序基本单元3.2CPOL和CPHA3.3四种工作模式3.3.1工作模式03.3.2工作模式13.3.3工作模式23.3.4工作模式3一......
  • C++多线程详解 | 线程创建 | 互斥锁 | 条件变量 | 线程池
    目录前言1.线程创建2.互斥锁3.lock_guard与std::unique_lock4.condition_variable 5.线程池前言在说线程之前,先说说进程和线程的关系,以及什么是多线程(为了方便理解就用大白话来说)进程:进程就是运行中的程序,比如说一个微信的程序,你双击它,它运行起来了就是一个进程,在还......
  • C++ 模版详解 | 函数模板 | 类模版
    前言 什么是模板?模板是一个泛型编程的概念,即不考虑类型的一种编程方式,能够实现代码重用,提高效率模板可分为函数模板、类模板 模板的声明和定义模板的声明有两种,一种就是typename,另外一种就是使用class ,一般使用一种声明格式就可以了,不建议混合使用。template<typenam......
  • 专题1:树莓派Pico引脚功能详解
    1主要功能RaspberryPiPico一共有40个针脚。这是RaspberryPiPico官方提供的引脚图,应该有很多人看到上面的图标都是两眼一摸黑,接下来,我将从每种颜色分类来讲述每个引脚的功能2Power类引脚Power,顾名思义就是电源的意思。这种引脚一共有三个,Power类引脚是为接到Pico上......
  • 注解反射详解
    注解反射注解1.注解概述//什么是注解publicclassTest01extendsObject{//@Override重写的注解@OverridepublicStringtoString(){returnsuper.toString();}}2.内置注解//什么是注解@SuppressWarnings("all")//镇压警告publi......