2024-08-17 19:09阅读: 6评论: 0推荐: 0

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的结果

1find查找到的内容进行删除,交给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


2find查找到的内容进行复制,交给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


3find查找到的内容进行复制,交给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的结果

1find查找到的内容进行删除,交给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


2find查找到的内容进行复制,交给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


3find查找到的内容进行复制,交给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

本文作者:Dusk_Yun

本文链接:https://www.cnblogs.com/9Dusk/p/18364821

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Dusk_Yun  阅读(6)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起