10-find命令详解

10 文件详细属性

知识点回顾
ls :命令
	-l 详细
	-a 隐藏
	-d 查看目录本身
	-r #逆序
	-i inode号码
	-t	文件创建时间
2.du 统计目录大小
[root@oldboyedu ~]# du -sh /etc/
33M	/etc/
3.inode号码
	df -i
	inode 号码或者磁盘空间任意一个满,都会导致磁盘无法写入
4.文件类型
- 普通文件 数据文件 压缩包 媒体文件
d 目录
b 块设备
l 软链接
c 字节文件  /dev/zero(生成指定大小的文件)  /dev/null(黑洞设备)
s socket

5.同时接收正确和错误的结果定向到文件
echo oldboy > 1.txt 2> 2.txt
echo oldboy >>1.txt 2>&1
echo oldboy &>>1.txt  #常用

6.yum 安装
yum -y install 包名1 包2
yum -y remove 包名  #卸载
yum repolist #查看仓库
yum clean all #清理缓存
yum reinstall #覆盖安装
yum -y localinstall #本地安装 扩展
#yum下载安装包默认存放位置:
[root@oldboyedu ~]# ll /var/cache/yum/x86_64/7/base/packages/

7.rpm 安装
rpm -qa |grep wget
rpm -qa wget
rpm -ivh 安装包(xxx.rpm)	#安装
rpm -e 	wget	#卸载
rpm -ql wget #查看wget安装了哪些文件
文件详细属性
01.find查找文件
语法结构:
	find 在哪里查找 按照什么类型查找
注意:find默认递归查找文件
案例1:按照文件的类型查找
f  普通文件 -
d  目录
l  软链接
c  字节设备
b  块设备

语法结构: find ./ -type 文件类型
#创建实验环境
# 创建测试环境
[root@db01 ~]# mkdir oldboy
[root@db01 ~]# cd oldboy/
[root@db01 oldboy]# touch {1..3}.txt
[root@db01 oldboy]# touch {1..3}.TXT
[root@db01 oldboy]# touch {1..3}.log
[root@db01 oldboy]# ll
total 0
-rw-r--r-- 1 root root 0 Mar 13 09:29 1.log
-rw-r--r-- 1 root root 0 Mar 13 09:29 1.txt
-rw-r--r-- 1 root root 0 Mar 13 09:29 1.TXT
-rw-r--r-- 1 root root 0 Mar 13 09:29 2.log
-rw-r--r-- 1 root root 0 Mar 13 09:29 2.txt
-rw-r--r-- 1 root root 0 Mar 13 09:29 2.TXT
-rw-r--r-- 1 root root 0 Mar 13 09:29 3.log
-rw-r--r-- 1 root root 0 Mar 13 09:29 3.txt
-rw-r--r-- 1 root root 0 Mar 13 09:29 3.TXT
[root@db01 oldboy]# mkdir oldboy-{1..4}
[root@db01 oldboy]# ll
total 0
-rw-r--r-- 1 root root 0 Mar 13 09:29 1.log
-rw-r--r-- 1 root root 0 Mar 13 09:29 1.txt
-rw-r--r-- 1 root root 0 Mar 13 09:29 1.TXT
-rw-r--r-- 1 root root 0 Mar 13 09:29 2.log
-rw-r--r-- 1 root root 0 Mar 13 09:29 2.txt
-rw-r--r-- 1 root root 0 Mar 13 09:29 2.TXT
-rw-r--r-- 1 root root 0 Mar 13 09:29 3.log
-rw-r--r-- 1 root root 0 Mar 13 09:29 3.txt
-rw-r--r-- 1 root root 0 Mar 13 09:29 3.TXT
drwxr-xr-x 2 root root 6 Mar 13 09:30 oldboy-1
drwxr-xr-x 2 root root 6 Mar 13 09:30 oldboy-2
drwxr-xr-x 2 root root 6 Mar 13 09:30 oldboy-3
drwxr-xr-x 2 root root 6 Mar 13 09:30 oldboy-4

按照深度等级查找文件:
案例:只查找1级目录下的文件
[root@oldboyedu oldboy]# find ./ -maxdepth 1 -type f
./1.txt
./2.txt
./3.txt
./1.log
./2.log
./3.log

案例1:查找当前目录下所有的普通文件
[root@oldboyedu oldboy]# find ./ -type f 
./1.txt
./2.txt
./3.txt
./1.log
./2.log
./3.log
./1.TXT

案例2:查找当前目录下所有的目录
[root@oldboyedu oldboy]# find ./ -type d
./
./oldboy-1
./oldboy-2
./oldboy-3
./oldboy-4


案例3:按照文件的名称查找
语法: find ./ -name "文件名称/目录名称"
[root@oldboyedu oldboy]# find ./ -name "1.txt"
./1.txt

案例4:按照文件的名称模糊查找
[root@oldboyedu oldboy]# find ./ -name "*.txt"
./1.txt
./2.txt
./3.txt
./2008-1.txt
./2008-2.txt
./2008-3.txt
./2008-4.txt
./2008-5.txt
./s.txt
./all.txt
./22.txt

案例5:按照文件名称查找忽略大小写
[root@oldboyedu oldboy]# find ./ -iname "*.txt"
./1.txt
./2.txt
./3.txt
./1.TXT
./2.TXT
./3.TXT
./4.TXT

案例6.按照文件的目录名查找
[root@oldboyedu oldboy]# find ./ -name "oldboy-1" -type d
./oldboy-1

案例7:按照文件的inode号码查找
[root@oldboyedu oldboy]# find ./ -inum 51308098
./2.log

案例8:按照类型和名称联合查找
[root@oldboyedu oldboy]# find ./ -name "1.txt" -type f
./1.txt

案例9:find按照大小查找
语法:  find ./ -size 10M  #查找大小是10M的文件
					 +10M #查找大于10M的文件
					 -10M #查找小于10M的文件
					 
案例:创建实验环境
[root@db01 oldboy]# cat /etc/services /etc/services /etc/services > s.txt
[root@db01 oldboy]# wc -l s.txt
33528 s.txt
[root@db01 oldboy]# wc -l /etc/services
11176 /etc/services
[root@db01 oldboy]# ll -h s.txt
-rw-r--r-- 1 root root 2.0M Mar 13 10:06 s.txt
[root@db01 oldboy]# cat s.txt s.txt s.txt s.txt s.txt s.txt s.txt s.txt > all.txt
[root@db01 oldboy]# ll -h all.txt
-rw-r--r-- 1 root root 16M Mar 13 10:07 all.txt
[root@db01 oldboy]# dd if=/dev/zero of=test.txt bs=1M count=10

案例1:查找等于10M的文件
[root@oldboyedu oldboy]# find ./ -size 10M
./test.log

案例2:查找大于10M的文件
[root@oldboyedu oldboy]# find ./ -size +10M
./all.txt
./22.txt

案例3:查找小于10M的文件
[root@oldboyedu oldboy]# find ./ -size -10M
./
./1.txt
./2.txt
./3.txt
./1.log
./2.log
./3.log
./oldboy-1
./oldboy-2
./oldboy-3
./oldboy-4
./1.TXT
./2.TXT
./3.TXT
./4.TXT

案例4:查找普通文件大于10M的
[root@oldboyedu oldboy]# find ./ -type f -size +10M
./all.txt
./22.txt

案例5:查找文件大于5M并且小于15M的文件
find命令默认参数之间是且的关系
并且:and  同时成立
或者: or  成立一个即可
[root@oldboyedu oldboy]# find ./ -type f -size +10M

案例6:查找文件等于10M或者文件大于15M的文件
[root@oldboyedu oldboy]# find ./ -type f -size 10M -or -size +15M
./test.log

案例:查找文件名.txt 并且大于5M的文件
[root@oldboyedu oldboy]# find ./ -name "*.txt" -size +5M
./all.txt
./22.txt

案例7:find按照时间查找
-mtime # 文件的修改时间
find ./ -mtime 7 # 7天以前那天(上周三那天)修改过的文件 无意义可忽略。
			  +7 # 7天前修改过的文件
			  -7 # 7天内修改过的文件
			  
第一步:实验环境
[root@oldboyedu oldboy]# date -s 20081010
Fri Oct 10 00:00:00 CST 2008
[root@oldboyedu oldboy]# touch 2008-{1..5}.txt
[root@oldboyedu oldboy]# ll
total 0
-rw-r--r-- 1 root root 0 Mar 13  2024 1.log
-rw-r--r-- 1 root root 0 Mar 13  2024 1.txt
-rw-r--r-- 1 root root 0 Mar 13  2024 1.TXT
-rw-r--r-- 1 root root 0 Oct 10 00:00 2008-1.txt
-rw-r--r-- 1 root root 0 Oct 10 00:00 2008-2.txt
-rw-r--r-- 1 root root 0 Oct 10 00:00 2008-3.txt
-rw-r--r-- 1 root root 0 Oct 10 00:00 2008-4.txt
-rw-r--r-- 1 root root 0 Oct 10 00:00 2008-5.txt
-rw-r--r-- 1 root root 0 Mar 13  2024 2.log
-rw-r--r-- 1 root root 0 Mar 13  2024 2.txt
-rw-r--r-- 1 root root 0 Mar 13  2024 2.TXT
-rw-r--r-- 1 root root 0 Mar 13  2024 3.log
[root@oldboyedu oldboy]# ntpdate ntp1.aliyun.com
[root@oldboyedu oldboy]# hwclock -w

案例1:查找7天前的文件,企业中数据备份
[root@oldboyedu oldboy]# find ./ -type f -mtime +7
./2008-1.txt
./2008-2.txt
./2008-3.txt
./2008-4.txt
./2008-5.txt
案例2:查找修改时间小于7天(7天内的)的文件。系统中毒被黑客攻击,文件篡改
[root@oldboyedu oldboy]# find ./ -type f -mtime -7
./1.txt
./2.txt
./3.txt
./1.log
./2.log
./3.log
./1.TXT
./2.TXT
./3.TXT
./4.TXT

案例3:按照类型和名称联合查找
[root@oldboyedu oldboy]# find ./ -name "1.txt" -type f
./1.txt




面试题:查找oldboy目录7天前的文件并删除
[root@oldboyedu oldboy]# find ./ -mtime +7 -type f |xargs rm -rf

find查找到的结果交给其他命令
方法1:
1.find命令结果交给rm 命令
xargs将find查找到的结果甩到命令的最后面
xargs后面的别名失效
语法: find ./ -type f |xargs rm
案例:查找.TXT文件后删除
[root@oldboyedu oldboy]# find ./ -name "*.TXT" |xargs rm

案例1:删除目录需要-r
[root@oldboyedu oldboy]# find ./ -type d -name "oldboy-4" |xargs rm -r

案例2:将find的执行结果交给ls 查看详细信息
[root@oldboyedu oldboy]# find ./ -name "1.txt" |xargs ls -l
-rw-r--r-- 1 root root 0 Mar 13 09:29 ./1.txt

案例3:查找的文件交给cat查看
[root@oldboyedu oldboy]# find ./ -name "2008-[0-9].txt"     ##`[] 任意单个的意思`,这个位置只能表示1个字符。或0 或1 或2 或9  。[0-9]不能生成序列
[root@oldboyedu oldboy]# find ./ -name 2008-"[12].txt"
./2008-1.txt
./2008-2.txt

[root@oldboyedu oldboy]# find ./ -name "2008-[12].txt" |xargs cat
aaaaa
bbbbb

[root@oldboyedu oldboy]# find ./ -name "2008-[0-9][3].txt"
./2008-33.txt							#【0-9】匹配单个数字0-9
										#【3】匹配单个数字3

[root@oldboyedu oldboy]# find ./ -name "2008-[22-45].txt"															 #【22-45】匹配2或3或4或5


#cp file 路径

案例4:find结果交给cp  # -i 参数:把结果放在{}的位置
[root@oldboyedu oldboy]# find ./ -name "all.txt" |xargs -i cp {} /opt
[root@oldboyedu oldboy]# ll /opt/
total 15360
-rw-r--r-- 1 root root 15728640 Mar 13 18:15 all.txt

案例5:find结果交给mv
[root@oldboyedu oldboy]# find ./ -name "all.txt" |xargs -i mv {} /opt
[root@oldboyedu oldboy]# ll /opt/all.txt 
-rw-r--r-- 1 root root 15728640 Mar 13 15:55 /opt/all.txt

案例6:find结果交给


小结:
# 把所有的find结果都甩在了命令的最后位置。
find ./ -type f |xargs rm
find ./ -type f |xargs cat 
find ./ -type f |xargs ls -l

#把find结果放在指定的位置 使用-i 参数。放在{}的位置上。
find ./ -name "a.txt" |xargs  -i cp {} /opt
find ./ -name "a.txt" |xargs -i mv {} /opt

#方法二:使用-exec 参数实现. 把find的结果放在{}的位置上。注意命令结尾需要加上 \;
案例1:将find的结果交给 rm
find ./ -name "2008-1.txt" -exec rm {} \;
案例2:将find的结果交给 ls
find ./ -name "2008-2.txt" -exec ls {} \;
案例3:将find的结果交给 cp mv
[root@oldboyedu oldboy]# find ./ -name "2008-2.txt" -exec cp {} /tmp/ \;
[root@oldboyedu oldboy]# ll /tmp/
total 4
-rw-r--r--  1 root root 6 Mar 13 20:19 2008-2.txt

[root@oldboyedu oldboy]# find -type f -exec cp {} /opt/ \;
[root@oldboyedu oldboy]# ll /opt/
total 39860
-rw-r--r-- 1 root root        0 Mar 13 20:22 1.log
-rw-r--r-- 1 root root        0 Mar 13 20:22 1.txt
-rw-r--r-- 1 root root        6 Mar 13 20:22 2008-1.txt
-rw-r--r-- 1 root root        6 Mar 13 20:22 2008-2.txt
-rw-r--r-- 1 root root        0 Mar 13 20:22 2008-333.txt

方法三:使用``先执行``里边的内容或者使用$()先执行()里边的内容
把find的结果交给rm cp ls mv
案例1:把find的结果交给rm
rm -rf `find ./ -name "all.txt"`
rm -rf $(find ./ -name "all.txt)
案例2:把find的结果交给cp
cp `find ./ -name "all.txt"` /tmp/
cp $(find ./ -name "all.txt") /tmp
案例3:把find的结果交给ls
ls -l `find ./ -type f -name "all.txt"`
ll $(find ./ -type f -name "all.txt")
小结-exec
find ./ -name "1.txt" -exec rm {} \;
find ./ -type f -exec cp {} /opt \;
find ./ -type f -exec mv {} /tmp/ \;
#把find的结果放在{}的位置上。注意命令结尾需要加上 \;
Linux命令行中具有特殊意义的符号
&& 	`作用:前面的命令必须执行成功之后才执行后面的命令 。短路与`
|| 	`作用:前面的命令必须执行失败之后才能执行后面的命令`
; 	`作用:命令连接符号`

&& 案例:前面的命令执行成功,才执行后面的命令
[root@oldboyedu oldboy]# echo hehe && touch hehe.txt
hehe
[root@oldboyedu oldboy]# ech hehe && touch hehe1.txt
-bash: ech: command not found

[root@oldboyedu oldboy]# eho hehe && touch 111111.txt
-bash: eho: command not found
[root@oldboyedu oldboy]# ll 111111.txt
ls: cannot access 111111.txt: No such file or directory

|| 案例:前面的
[root@oldboyedu data]# cd /data/
-bash: cd: /data/: No such file or directory
[root@oldboyedu data]# 
[root@oldboyedu data]# cd /data/ || mkdir /data
-bash: cd: /data/: No such file or directory
[root@oldboyedu data]# ll /data/
total 0

[root@oldboyedu data]# cd /data/ || touch /data/data.txt
[root@oldboyedu data]# 
[root@oldboyedu data]# ll
total 0

; 案例:命令连接符,不管前面的命令执行成功还是失败都执行后面的
[root@oldboyedu data]# echo hehe ;touch 111.txt ;ech hahah
hehe
-bash: ech: command not found
[root@oldboyedu data]# ll 111.txt 
-rw-r--r-- 1 root root 0 Mar 13 20:17 111.txt

[root@oldboyedu data]# ech hehhe ;tuo 111.txt ;ehc haah
-bash: ech: command not found
-bash: tuo: command not found
-bash: ehc: command not found


知识点小结:
find 查找文件
find ./ -name "" 		
find ./ -iname ""	
find ./ -inum ""	
find ./ -type f d c 	#
find ./ -type f -name "" 	#联合查找 默认条件并且
find ./ -maxdepth 1 -type f #按照深度等级查找
find ./ -size -10M -or +10M #按照文件大小查找
find ./ -size 10M -size +15M #并且
find ./ -size 10M -or -name "1.txt" #文件大小或者文件名是1.txt
find ./ -mtime +7 -7 #按照文件的修改时间查找

#find 将查找的结果交给其他命令
find ./ -name "1.txt" |xargs rm -r
find ./ -name "1.txt" |xargs ls -l
find ./ -name "1.txt" |xargs -i  cp -r {} /opt #复制

find ./ -name "1.txt" -exec rm {} \;
find ./ -name "1.txt" -exec ls -l {} \;
find ./ -name "1.txt" -exec cp {} /opt \;

rm -rf `find ./ -name "1.txt"`
ll `find ./ -name "1.txt"`
cp -rf `find ./ -name "1.txt"`
rm -rf $(find ./ -name "1.txt")
ll $(find ./ -name "1.txt")
cp $(find ./ -name "1.txt") /opt

特殊符号:
&& :前面的命令必须执行成功后面的命令才执行
|| :前面的命令必须执行失败后面的命令才执行
; : 作用命令连接符号

du -sh 文件/目录 #查看目录总大小 。d disk u usage


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

atomLg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值