find命令
find 主要进行文件搜索
基本语法
find [文件路径] [选项 选项的值]
常见的选项
-name 根据文件的名称搜索文件,支持通配符*
-type f代表普通文件,d代表目录
*通配符
在linux 系统中,如果要查找的文件的名称不清晰,可以使用部分文件名+*搜索案例,找到httpd.conf文件
[root@localhost ~]# find / -name "httpd.conf" -type f # 下面就是查找出的文件
/etc/httpd/conf/httpd.conf
/usr/lib/tmpfiles.d/httpd.conf
[root@localhost ~]# find /etc/ -name "httpd.conf" -type f # 将/范围换成/etc/目录范围,节省计算自资源
/etc/httpd/conf/httpd.conf
通过文件的最后修改时间搜索文件
语法:find 文件路径 -mtime +days/-days
-mtime 根据文件最后修改时间搜索文件
+号 搜索几天之前的文件信息
-号 搜索几天之内的文件信息[root@localhost ~]# find /opt/ -name "*.txt -mtime +3 # 搜索 3 天以前的txt文件,不包含第三天[root@localhost ~]# find /opt/ -name "*.txt -mtime -3 # 搜索三天以内的 txt文件, 包含今天
find的exec选项# 使用 -exec 文件调用 rm 函数 {} 表示前面 find 查到的内容 \; 表示标识符[root@localhost opt]# find /opt/ -name "*.txt" -type f -mtime +3 -exec rm - rf {} \;
根据文件size大小搜索文件
语法:find 路径 -size 文件大小 [常用单位 k M G]
size值 搜索等于size的文件
-size值 【0,size值)
+size值 (size值,正无穷)# 搜索文件大小为 5M 的文件[root@localhost ~]# find ./ -size 5M
扩展命令 dd
使用dd创建扩展命令
生成指定大小的测试文件
语法:dd if=/dev/zero of=文件名称 bs=1M count=1
if表示输入文件
of表示输出文件
bs代表字节为单位的块大小
count代表被复制的块
其中/dev/zore是一个字符设备,会不断地返回0字节的文件# 创建名称为 1.txt ,大小为 1M 的文件[root@localhost ~]# dd if=/dev/zero of=1.txt bs=1M count=1
文件的时间的概念
window中的时间
1.创建时间
2.修改时间
3.访问时间
使用stat命令获取文件的时间信息
语法:stat 文件名创建文件,并配置文件的修改时间
语法:touch -m -d 日期时间格式 文件名
[root@localhost ~]# touch -m -d "2000-1-1 00:00" /opt/1.txt
tree指令
创建文件列表,将文件名称以树的形式展示#安装tree[root@localhost opt]# yum -y install tree语法 tree 文件目录![]()
scp
scp要求两台主机的系统都是linux系统
1.使用scp下载文件和目录
语法:scp [选项] 用户名@linux主机地址:/资源路径 linux本地文件路径
scp复制文件-r 代表递归,主要作用文件夹
[root@localhost ~]# scp root@192.168.10.10:1./opt/1.txt /opt/scp 上传文件语法: scp [ 选项 ] 本地主机资源路径 { 远程主机 } 用户名 @ 主机 ip: 放置路root@localhost ~]# scp /opt/1.txt root@192.168.10.10:/opt/启用ssh服务systemctl start sshd
计划任务
crontab [ 选项 ]-l list 查看当前用户的计划任务信息-e edit 编写计划任务
![](https://i-blog.csdnimg.cn/direct/50404c9a65a64c56b93000f08446072f.png)
练习
1.使⽤ls查看/etc/⽬录下所有的⽂件信息
[root@ROOT ~]# ls /etc/*
2. 使⽤ls查看/etc/⽬录下名包含“a”字⺟的⽂件或者⽬录信息
[root@ROOT ~]# ls /etc -name *a
3. 使⽤ls查看/etc/⽬录下以".conf"结尾的⽂件信息
[root@ROOT ~]#ls /etc -name ".conf" -type f
4. 使⽤ls查看/etc/⽬录中以"y"字⺟开头的⽂件信息
[root@ROOT ~]#ls /etc -name "y*" -type f
5. find查找/var/⽬录中以“.log”⽂件
[root@ROOT ~]# find /var -name "*.log" -type f
6. 在opt⽬录下创建test⽬录
[root@ROOT ~]# mkdir -p /opt/test
7.在test⽬录中创建abc.txt,def.txt.ghi.txt,xxx.txt.yyy.txt五个⽂件
[root@ROOT ~]# touch /opt/test/abc.txt /opt/test/def.txt /opt/test/ghi.txt /opt/test/xxx.txt /opt/test/yyy.txt
[root@ROOT ~]# ls -l /opt/test/
-rw-r--r--. 1 root root 0 7月 15 22:06 abc.txt
drwxr-xr-x. 2 root root 6 7月 15 18:59 d0
-rw-r--r--. 1 root root 0 7月 15 22:06 def.txt
-rw-r--r--. 1 root root 0 7月 15 22:06 ghi.txt
drwxr-xr-x. 2 root root 6 7月 15 22:05 test
-rw-r--r--. 1 root root 0 7月 15 22:06 xxx.txt
-rw-r--r--. 1 root root 0 7月 15 22:06 yyy.txt
8. 修改以上5个⽂件的最后修改时间分别为15,14,13,12,11,10⽇
[root@ROOT ~]# touch /opt/test/abc.txt -m -d "2024-7-15 00:00"
您在 /var/spool/mail/root 中有新邮件
[root@ROOT ~]# touch /opt/test/abc.txt -m -d "2024-7-15 00:00"
[root@ROOT ~]# touch /opt/def.txt -m -d "2024-7-14 00:00"
[root@ROOT ~]# touch /opt/ghi.txt -m -d "2024-7-13 00:00"
[root@ROOT ~]# touch /opt/test/xxx.txt -m -d "2024-7-12 00:00"
[root@ROOT ~]# touch /opt/test/yyy.txt -m -d "2024-7-11 00:00"
[root@ROOT ~]# ls -l /opt/test/
-rw-r--r--. 1 root root 0 7月 15 00:00 abc.txt
-rw-r--r--. 1 root root 0 7月 14 00:00 def.txt
-rw-r--r--. 1 root root 0 7月 13 00:00 ghi.txt
-rw-r--r--. 1 root root 0 7月 12 00:00 xxx.txt
-rw-r--r--. 1 root root 0 7月 11 00:00 yyy.txt
9. 在test⽬录下创建a⽬录
[root@ROOT ~]# mkdir -p /opt/test/a
10. 将以上5个⽂件复制⼀份到a⽬录中
[root@ROOT ~]# cp -r /opt/test/* /opt/test/a
[root@ROOT ~]# ls /opt/test/a
abc.txt def.txt ghi.txt xxx.txt yyy.txt
11.将a⽬录⽂件做成bak.tar.gz⽂件保存到家⽬录中
[root@ROOT ~]# pwd
/root
[root@ROOT ~]# tar -zcvf /home/bak.tar.gz /opt/test/a
[root@ROOT ~]# ls /home
anaconda-ks.cfg bak.tar.gz echo.txt list minute.txt
12. 使⽤find删除test⽬录下3天前的⽂件
[root@ROOT ~]# find /test -mtime +3 -type f -exec rm -rf {} \;
13. find删除opt⽬录下3天内的⽂件
[root@ROOT ~]# touch /opt/m.txt /opt/n.txt
[root@ROOT ~]# ls -l /opt/
-rw-r--r--. 1 root root 13022098 7月 8 14:27 apache-tomcat-10.1.25.tar.gz
-rw-r--r--. 1 root root 267 7月 8 19:00 application.perperties
-rw-r--r--. 1 root root 101 7月 8 22:10 Main.java
-rw-r--r--. 1 root root 0 7月 16 00:41 m.txt
-rw-r--r--. 1 root root 0 7月 16 00:41 n.txt
[root@ROOT ~]# find /opt/ -mtime -3 -type f -exec rm -rf {} \;
[root@ROOT ~]# ls -l /opt/
总用量 12728
drwxr-xr-x. 2 root root 6 7月 16 00:41 123.txt
-rw-r--r--. 1 root root 13022098 7月 8 14:27 apache-tomcat-10.1.25.tar.gz
-rw-r--r--. 1 root root 267 7月 8 19:00 application.perperties
-rw-r--r--. 1 root root 101 7月 8 22:10 Main.java
14. find删除正好第三天的⽂件
[root@mj ~]# find /opt -time 3 -type f -exec rm -rf {} \;
15. 将/opt/test/a⽬录中的⽂件复制⼀份到/opt/test/⽬录下
[root@ROOT ~]# cp -r /opt/test/a/* /opt/test/
16. 创建⽬录/opt/test0
[root@ROOT ~]# mkdir -p /opt/test0
[root@ROOT ~]# ls /opt
test0
17. 在/opt/test0/⽬录中创建三个⽂件 a.mp4(5M),b.mp4(20M),c.mp4(80M)
[root@ROOT ~]# dd if=/dev/zero of=/opt/test0/a.mp4 bs=5M count=1
[root@ROOT ~]# dd if=/dev/zero of=/opt/test0/b.mp4 bs=200M count=1
[root@ROOT ~]# dd if=/dev/zero of=/opt/test0/c.mp4 bs=80M count=1
18. 创建⽬录/opt/test0/b/
[root@ROOT ~]# cd /opt/test0
[root@ROOT test0]# pwd
/opt/test0
[root@ROOT test0]# mkdir b
19. 将/opt/test0/中的⽂件复制⼀份/opt/test0/b/⽬录中
[root@ROOT test0]# ls
mp4 b b.mp4 c.mp4
[root@ROOT test0]# cp -r ./* /opt/test0/b[root@ROOT test0]# ls /opt/test0/b
a.mp4 b b.mp4 c.mp4
20. find查询/opt/test0/⽬录中⽂件⼤于20M的,并删除
[root@ROOT test0]# ls
a.mp4 b b.mp4 c.mp4
[root@ROOT test0]# find /opt/test0/ -type f -size +20M -exec rm -rf {} \;
[root@ROOT test0]# ls /opt/test0
a.mp4 b
21. find查询/opt/test0/⽬录中⽂件⼩于20M的⽂件并删除
[root@ROOT test0]# find /opt/test0/ -type f -size -20M -exec rm -rf {} \;
[root@ROOT test0]# ls -l /opt/test0
drwxr-xr-x. 3 root root 15 7月 15 23:15 b
22. find查找/opt/test0/⽬录中⽂件size为20M的⽂件并删除
[root@ROOT test0]# find /opt/test0/ -type f -size 20M -exec rm -rf {} \;
23. /opt/test0/b中的⽂件复制⼀份到/opt/test0中
[root@ROOT test0]# cp -r /opt/test0/b/* /opt/test0/
[root@ROOT test0]# ls /opt/test0
b test0
oot@ROOT test0]# mkdir b
25. 将家⽬录中的bak.tar.gz⽂件上传到新主机的/opt⽬录中
[root@ROOT ~]#scp /home/bak.tar.gz root@192.168.10.10:/opt/
26. 将新主机的/etc/skel/⽬录下载到当前主机的/opt⽬录中
[root@ROOT ~]#scp /etc/skel/ root@192.168.10.10:/opt
27. 设置计划任务,每周3将/etc/yum.repos.d/⽬录下的.repo⽂件压缩保存到tmp,在⽂件
名中添加时间戳
[root@ROOT ~]# whereis tar
tar: /usr/bin/tar /usr/include/tar.h /usr/share/man/man1/tar.1.gz
[root@ROOT ~]# crontab -e
* * * * 3 /usr/bin/tar -zcvf /tmp/etc_$(date "+\%Y\%m\%d\%H\%M\%S").tar.gz /etc/yum.repos.d/*.repo