Linux下文件的查找、压缩归档以及计划任务的设置

Day05

find的基本使用

格式:find [⽂件路径] [选项 选项的值]  

选项:-name 根据文件的名称搜索文件,-iname 忽略大小写,支持通配符*

-type f代表普通文件,d代表目录

作用:使用find按时间搜索,删除旧文件

[root@peter ~]# find /boot/ -type d   (是目录的)

[root@peter ~]# find /boot/ -type f (是文件的)

[root@peter ~]# find /boot/ -type l   (是快捷方式的)

案例:

1、按名字查询——-name 文件名

找到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

模糊查询——通配符:如果要查找的文件的名称不清晰,可以使用部分文件名+*搜索

搜索以http开头的文件:

[root@localhost ~]# find /etc/ -name "http*" -type f

搜索名字含tab的文件:

[root@peter ~]# find /etc/ -name *tab

[root@peter ~]# find /etc/ -name '.*'

搜索以.sh结尾的文件:

[root@peter ~]# find /etc -name "*.sh" -o -type d

[root@peter ~]# find /mnt -name "cbd*" -o -type f

[root@peter ~]# find /boot -type d | wc -l   (利用管道符统计文件个数)

  1. 按文件大小查询

(1)-size +或- 文件大小(文件大小)  [常⽤单位 k M G]

size值 搜索等于size的⽂件  -size值 (0,size值)  +size值 (size值,正⽆穷)

查找/opt目录下大于2M的文件并将其删除:

[root@web2 ~]# find /opt/ -size +2M -exec rm -rf {} \;

[root@peter ~]# find /boot/ -size +300k

查找大于10M小于50M的数据:

[root@peter ~]# find /boot/ -size +10M -size -50M

(2)扩展命令 dd,使⽤dd创建扩展命令,⽣成指定⼤⼩的测试⽂件,

格式:dd if=/dev/zero of=⽂件名称 bs=1M count=1

if表示输⼊⽂件  of表示输出⽂件  bs代表字节为单位的块⼤⼩ count代表被复制的块,其中/dev/zore是⼀个字符设备,会不断地返回0字节的⽂件

[root@web2 ~]# dd if=/dev/zero of=/opt/a.txt bs=100M count=1  #  创建名称为a.txt,⼤⼩为100M的⽂件

[root@web2 ~]# dd if=/dev/zero of=/opt/b.txt bs=5M count=1

记录了1+0 的读⼊

记录了1+0 的写出

5242880字节(5.2 MB)已复制,0.0111468 秒,470 MB/秒

[root@web2 ~]# ls -lh /opt/  # 查看⽂件信息,使⽤⽂件⼤⼩单位 默认m

-rw-r--r--. 1 root root 5.0M 7月  15 20:06 aaa.mp4

[root@localhost opt]# ls -l

总⽤量 16384

-rw-r--r--. 1 root root 1048576 7⽉ 14 14:37 a.txt

-rw-r--r--. 1 root root 5242880 7⽉ 14 14:42 b.txt

3、按文件权限查找:

[root@localhost ~]#find -perm -u=root /opt

4、按文件的最后修改时间查询

格式:find ⽂件路径 -mtime +days/-days  -mtime 根据⽂件最后修改时间搜索⽂件  +号 搜索⼏天之前的⽂件信息  -号 搜索⼏天之内的⽂件信息

案例:

(1)搜索3天以前的信息,不包含第三个的,⽽且只搜txt⽂件

[root@localhost ~]#ls -l *.txt

[root@localhost ~]#find /opt/ -name "*.txt"-mtime +3

(2)搜索3天以内的信息,不包含第三个的,⽽且只搜txt⽂件

[root@localhost ~]#find /opt/ -name "*.txt" -type f -mtime -3

(3)删除系统/var/log/ 10天之前的⽇志,格式都是.log⽂件

[root@localhost ~]#rm -rf find /opt/ -name "*.txt" -type f  # rm不支持这种写法

[root@localhost ~]#rm -rf find /opt/ -name "*.txt" -type f -mtime +3| rm -rf  # rm和ls不支持管道

[root@localhost ~]#find /opt/ -name "*.txt" -type f -mtime +3|xargs rm -rf  # 使用xargs将查询结果交给rm

(4)删除/opt目录下三天以内修改过的文件:

[root@localhost ~]# find /opt/ -mtime -3 -exec rm -rf {} \;   # 使用-exec文件调用rm函数 {}表示前面的find查到的内容 \;表示标识符

tree  创建⽂件列表,将⽂件名称以树的形式展示

[root@web2 ~]#yum -y install tree  # 需要使⽤yum指令进⾏安装

[root@web2 ~]#tree

[root@localhost opt]# tree /var/log/  # 以树状结构显示/var/log⽬录中的⽂件

[root@web2 ~]# tree -l 2  #  只显示2级目录

stat 获取文件的时间信息(创建、修改和访问)

格式:[root@web1 ~]# stat /opt/test.conf

创建文件

格式:touch -m -d 日期时间格式 文件名称(文件不存在就创建,存在时只修改时间)

案例:

1、创建文件并用p选项指定文件修改时间

[root@web1 ~]# touch /opt/b.txt -m -d "2024-7-14 00:00"

  1. 修改或创建文件,并设置最后修改时间

[root@web1 ~]#touch -m -d "2024-7-14 00:00" /opt/abc.txt  # 设置最后修改时间为7-14

[root@web1 ~]# stat /opt/abc.txt  # 查看文件信息

scp 实现linux系统和linux之间的克隆操作

  1. scp下载⽂件

语法: scp [选项] 本地主机资源路径 {远程主机}⽤户名@主机ip:放置路

上传⽂件,将克隆机中的a.txt⽂件上传到源主机中

[root@localhost ~]# rm -rf /opt/*  # 清空opt⽬录中的⽂件

[root@localhost ~]# ls /opt/

[root@localhost ~]# ssh -lroot -p22 192.168.2.11 #ssh管理克隆机

[root@localhost ~]# scp /opt/a.txt root@192.168.135.129:/opt/  # 将克隆机上的a.txt 上传到 源主机上

root@192.168.2.11's password:

  1. txt

[root@localhost ~]# ssh -lroot -p22 192.168.2.11  # 把克隆机中的folder⽬录上传到源主机的opt⽬录

[root@web1 ~]#scp -r /opt/d0/a.txt root@192.168.2.12:/opt  #-r表示递归,上传目录及目录下的文件

[root@localhost ~]# scp -r /opt/folder/ root@192.168..2.11:/opt/

[root@localhost ~]#systemctl start sshd # 必须启⽤ssh服务

[root@localhost ~]#systemctl stop sshd

计划任务cron

选项:-e 编辑计划任务    -l 查看现有计划任务

用途:按照设置的时间间隔为用户反复执行某一项固定系统任务

软件包:cronie、 crontabs   系统服务:crond    日志文件:/var/log/cron

[root@web2 ~]# ls /opt/

d0

[root@server0~]# tail /var/log/cron     //了解cron执行消息....

Nov 11 21:20:01 localhost CROND[12759]: (root) CMD(/usr/lib64/sa/sa1 1 1)

分、时、日、月、周 

30、23、 *、 *、 *

30、23、 *、 *、 5

30、23、 *、 *、 7

30、23、 *、 *、 1-5

30、23、 *、 *、 1,3,7

 *、23、 *、 *、 1,3,7

 5、 *、 *、 *、 7      # 每个小时的第5分钟执行一次

*/5、 *、 *、 *、 7     # 每5分钟执行一次

30、 8、 *、 *、 */8    # 每8周早上8点半

30、 8、 1、 *、 1      # 满足任何一个1,就执行一次

案例:

[root@web2 ~]# which ls  # 注意:要使用完整路径  which命令

alias ls='ls --color=auto'

/usr/bin/ls

每分钟自动将/opt下的文件名写到root目录下的list中

[root@web2 ~]# crontab -e  # 编辑计划任务

[root@web2 ~]# crontab -l  # 查看计划任务

*/1 * * * * /usr/bin/ls /opt/ >> /root/list  # 每分钟执行一次

49 * * * * /usr/bin/echo "我是希腊女神echo" > /root/echo.txt  # 每小时的49分执行一次

[root@web2 ~]# whereis tar

tar: /usr/bin/tar /usr/include/tar.h /usr/share/man/man1/tar.1.gz

每2分钟将/etc目录下的文件打包放到/tmp下,命名为/tmp/etc.tar.gz

[root@web2 ~]# crontab -l

*/2 * * * * /usr/bin/tar -zcvf /tmp/etc.tar.gz /etc/  # 以上的任务会覆盖前⾯的⽂件,需要在指令中添加时间因素

*/1 * * * * /usr/bin/tar -zcvf /tmp/etc_$(date "+\%Y\%m\%d\%H\%M\%S").tar.gz /etc/    # 在脚本中出现%,必须使⽤转义符\

[root@web2 ~]# ls /tmp/  # 查看备份⽂件

etc_20240715205001.tar.gz

时间

[root@localhost ~]# date "+%T"  # 输出时间

17:24:56

[root@localhost ~]# date "+%F%T" #输出⽇期和时间

2024-07-1417:25:03

[root@localhost ~]# date "+%F-%T"  #在⽇期和时间中添加间隔

2024-07-14-17:25:11

[root@localhost ~]# date "+%F %T"

2024-07-14 17:25:15

[root@localhost ~]# date "+%F_%T"

2024-07-14_17:25:29

[root@localhost ~]# date "+%Y"  # 输出年

2024

[root@localhost ~]# date "+%Y%m%d"  # 输出年⽉⽇

20240714

[root@localhost ~]# date "+%Y%m%d%H%M%S"  # 输出年⽉⽇时分秒

#练习1

[root@localhost ~]# ls

anaconda-ks.cfg jdk-17_linux-x64_bin.tar.gz

a.txt           stus

index.html       数据统计分析卷⾯分数.xls

java17.tar.gz

1.使⽤ls查看/etc/⽬录下所有的⽂件信息

[root@web2 ~]# ls /etc/*

2. 使⽤ls查看/etc/⽬录下名包含“a”字⺟的⽂件或者⽬录信息

[root@web2 ~]# ls /etc -name *a

3. 使⽤ls查看/etc/⽬录下以".conf"结尾的⽂件信息

[root@web2 ~]#ls /etc -name ".conf" -type f

4. 使⽤ls查看/etc/⽬录中以"y"字⺟开头的⽂件信息

[root@web2 ~]#ls /etc -name "y*" -type f

5. find查找/var/⽬录中以“.log”⽂件

[root@web2 ~]# find /var -name "*.log" -type f

6. 在opt⽬录下创建test⽬录

[root@web2 ~]# mkdir -p /opt/test

7.在test⽬录中创建abc.txt,def.txt.ghi.txt,xxx.txt.yyy.txt五个⽂件

[root@web2 ~]# touch /opt/test/abc.txt /opt/test/def.txt /opt/test/ghi.txt /opt/test/xxx.txt /opt/test/yyy.txt

[root@web2 ~]# 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@web2 ~]# touch /opt/test/abc.txt -m -d "2024-7-15 00:00"

您在 /var/spool/mail/root 中有新邮件

[root@web2 ~]# touch /opt/test/abc.txt -m -d "2024-7-15 00:00"

[root@web2 ~]# touch /opt/def.txt -m -d "2024-7-14 00:00"

[root@web2 ~]# touch /opt/ghi.txt -m -d "2024-7-13 00:00"

[root@web2 ~]# touch /opt/test/xxx.txt -m -d "2024-7-12 00:00"

[root@web2 ~]# touch /opt/test/yyy.txt -m -d "2024-7-11 00:00"

[root@web2 ~]# 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@web2 ~]# mkdir -p /opt/test/a

10. 将以上5个⽂件复制⼀份到a⽬录中

[root@web2 ~]# cp -r /opt/test/* /opt/test/a

[root@web2 ~]# ls /opt/test/a

abc.txt  def.txt  ghi.txt  xxx.txt  yyy.txt

11.将a⽬录⽂件做成bak.tar.gz⽂件保存到家⽬录中

[root@web2 ~]# pwd

/root

[root@web2 ~]# tar -zcvf /home/bak.tar.gz /opt/test/a

[root@web2 ~]# ls /home

anaconda-ks.cfg  bak.tar.gz  echo.txt  list  minute.txt

12. 使⽤find删除test⽬录下3天前的⽂件

[root@web2 ~]# find /test -mtime +3 -type f -exec rm -rf {} \;

13. find删除opt⽬录下3天内的⽂件

[root@mj ~]# touch /opt/m.txt /opt/n.txt 

[root@mj ~]# 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@mj ~]# find /opt/ -mtime -3 -type f -exec rm -rf {} \;

[root@mj ~]# 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@web2 ~]# cp -r /opt/test/a/* /opt/test/

16. 创建⽬录/opt/test0

[root@web2 ~]# mkdir -p /opt/test0

[root@web2 ~]# ls /opt

 test0  

17. 在/opt/test0/⽬录中创建三个⽂件 a.mp4(5M),b.mp4(20M),c.mp4(80M)

[root@web2 ~]# dd if=/dev/zero of=/opt/test0/a.mp4 bs=5M count=1

[root@web2 ~]# dd if=/dev/zero of=/opt/test0/b.mp4 bs=200M count=1

[root@web2 ~]# dd if=/dev/zero of=/opt/test0/c.mp4 bs=80M count=1

18. 创建⽬录/opt/test0/b/

[root@web2 ~]# cd /opt/test0

[root@web2 test0]# pwd

/opt/test0

[root@web2 test0]# mkdir b

19. 将/opt/test0/中的⽂件复制⼀份/opt/test0/b/⽬录中

[root@web2 test0]# ls

  1. mp4  b  b.mp4  c.mp4

[root@web2 test0]# cp -r ./* /opt/test0/b

[root@web2 test0]# ls /opt/test0/b

a.mp4  b  b.mp4  c.mp4

20. find查询/opt/test0/⽬录中⽂件⼤于20M的,并删除

[root@web2 test0]# ls

a.mp4  b  b.mp4  c.mp4

[root@web2 test0]# find /opt/test0/ -type f -size +20M -exec rm -rf {} \;

[root@web2 test0]# ls /opt/test0

a.mp4  b

21. find查询/opt/test0/⽬录中⽂件⼩于20M的⽂件并删除

[root@web2 test0]# find /opt/test0/ -type f -size -20M -exec rm -rf {} \;

[root@web2 test0]# ls -l /opt/test0

drwxr-xr-x. 3 root root 15 7月  15 23:15 b

22. find查找/opt/test0/⽬录中⽂件size为20M的⽂件并删除

[root@web2 test0]# find /opt/test0/ -type f -size 20M -exec rm -rf {} \;

23. /opt/test0/b中的⽂件复制⼀份到/opt/test0中

[root@web2 test0]# cp -r /opt/test0/b/* /opt/test0/

[root@web2 test0]# ls /opt/test0

b  test0

24. 打开新的虚拟主机

25. 将家⽬录中的bak.tar.gz⽂件上传到新主机的/opt⽬录中

[root@web2 ~]#scp /home/bak.tar.gz root@192.168.2.12:/opt/

26. 将新主机的/etc/skel/⽬录下载到当前主机的/opt⽬录中

[root@mj ~]#scp /etc/skel/ root@192.168.2.11:/opt

27. 设置计划任务,每周3将/etc/yum.repos.d/⽬录下的.repo⽂件压缩保存到tmp,在⽂件

名中添加时间戳

[root@web2 ~]# whereis tar

tar: /usr/bin/tar /usr/include/tar.h /usr/share/man/man1/tar.1.gz

[root@web2 ~]# crontab -e

* * * * 3 /usr/bin/tar -zcvf /tmp/etc_$(date "+\%Y\%m\%d\%H\%M\%S").tar.gz /etc/yum.repos.d/*.repo

  • 25
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值