第六天7.15(find命令、通配符、文件时间)

1.find命令

主要进行文件搜索

2.基本用法

Find [文件路径]  [选型  选项的值]

-name *

--type f|d

常见的选项

-name 根据文件的名称搜索文件,支持通配符*

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

案例,找到httpd.conf文件

3.*通配符

在linux 系统中,如果要查找的文件的名称不清晰,可以使用部分文件名+*搜索

获取/etc/中以.conf结尾的文件

搜索以http开头的文件:

4.文件的时间的概念

Window中的时间

(1)创建时间

(2)修改时间

(3)访问时间

使⽤stat命令获取⽂件的时间信息

创建⽂件,并p配置⽂件的修改时间

语法 touch -m -d ⽇期时间格式 ⽂件名称

⽂件不存在就创建并修改时间,⽂件存在只配置最后修改时间

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

查看创建的文件:

通过文件的最后修改时间搜索文件

find 文件路径 -mtime +days/-days

-mtime根据文件最后修改时间搜索文件

+号 搜索⼏天之前的⽂件信息

-号 搜索⼏天之内的⽂件信息

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

搜索三天以内的文件 .txt,包括今天

案例:删除系统/var/log/ 10天之前的⽇志,格式都是.log⽂件

rm 和ls 不支持管道

(1)使用xargs将查询结果交给rm

(2)使用find执行-exec

语法find文件路径

查看三天以前的文件:

使用-exec 文件调用rm函数,{}表示前边find查到的内容, \表示标识符

5.根据文件size大小搜索文件

find 路径 -size ⽂件⼤⼩ [常⽤单位 k M G]

size值 搜索等于size的⽂件-size值 (0,size值)

+size值 (size值,正⽆穷)

扩展命令 dd

使⽤dd创建扩展命令

⽣成指定⼤⼩的测试⽂件

语法

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

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

案例:创建一个1M的文件

创建名称为a.txt大小为1M的文件

搜索系统大于100M的文件

删除root目录下文件大于100M的文件:

6.创建文件列表,将文件名称以树的形式展示

安装tree:yum -y instal tree

以树状结构显示/var/log⽬录中的⽂件:

[root@localhost ~]# tree /var/log

/var/log

├── anaconda

│   ├── anaconda.log

│   ├── ifcfg.log

│   ├── journal.log

│   ├── ks-script-I8c8Fs.log

│   ├── ks-script-PQ37xu.log

│   ├── packaging.log

│   ├── program.log

│   ├── storage.log

│   ├── syslog

│   └── X.log

├── audit

│   └── audit.log

├── boot.log

├── boot.log-20240715

├── btmp

├── chrony

├── cron

├── cron-20240715

├── dmesg

├── dmesg.old

├── firewalld

├── grubby_prune_debug

├── httpd

├── lastlog

├── maillog

├── maillog-20240715

├── messages

├── messages-20240715

├── rhsm

├── secure

├── secure-20240715

├── spooler

├── spooler-20240715

├── tallylog

├── tuned

│   └── tuned.log

├── vmware-vgauthsvc.log.0

├── vmware-vmsvc.log

├── wtmp

├── xferlog

└── yum.log

6 directories, 36 files

练习

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

[root@localhost ~]#ls

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

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

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

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

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

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

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

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

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

[root@localhost ~]# mkdir /opt/test

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

[root@localhost ~]# touch abc.txt def.txt ghi.txt xxx.txt yyy.txt

8. 修改以上5个⽂件的最后修改时间分别为15,14,13,12,11,10⽇

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

[root@localhost ~]# touch -m -d "2024-7-14" /opt/test/def.txt

[root@localhost ~]# touch -m -d "2024-7-13" /opt/test/ghi.txt

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

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

9. 在test⽬录下创建a⽬录

[root@localhost ~]# mkdir /opt/test/a

[root@localhost ~]# ls /opt/test

a

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

[root@localhost ~]# cp /opt/test/* /opt/test/a

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

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

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

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

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

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

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

[root@localhost ~]# find /opt/test/ -mtime -3 | xargs rm –rf

14. find删除正好第三天的⽂件

[root@localhost ~]# find /opt/test/ -mtime –rm –rf 3 -exec rm -rf {} \;

15. 将/opt/test/a⽬录中的⽂件复制⼀份到/opt/test/⽬录下

[root@localhost ~]# cp /opt/test/a/* /opt/test/

16. 创建⽬录/opt/test0

[root@localhost ~]# mkdir /opt/test0

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

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

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

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

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

[root@localhost ~]# mkdir /opt/test0/b/

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

[root@localhost ~]# cp /opt/test0/* /opt/test0/b

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

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

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

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

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

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

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

[root@localhost ~]# cp /opt/test/0/b/* /opt/test0

24. 打开新的虚拟主机

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

[root@localhost ~]# scp root@192.168.1.11:/opt/home/bak.tar.gz

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

[root@localhost ~]# scp root@192.168.1.11:/opt/skel /opt/

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

[root@localhost ~]# crontab –e

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值