find命令详解(exec选项),文件修改日期(创建,修改,搜索)

一、find命令

1、用途:主要进行文件搜索

2、基本语法:find [搜索路径] [查找条件] [处理动作]

搜索路径:指定 find 命令开始搜索的目录路径。如果省略,find 命令将从当前目录开始搜索。可以使用绝对路径或相对路径。

查找条件:用于指定搜索条件,以决定哪些文件或目录将被选中。查找条件可以基于文件名、文件类型、大小、修改时间等多种属性。查找条件可以组合使用,以更精确地定位文件。如果省略查找条件,find 命令将列出指定路径下的所有文件和目录。

处理动作:对查找到的文件执行的操作。如果省略处理动作,find 命令将默认打印出查找到的文件名。处理动作可以是执行命令、删除文件、复制文件等。

3、常见的选项

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

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

4.*通配符

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

案例

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

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

(2)搜索当前目录及子目录下所有 .txt 文件:

find . -name "*.txt"

. 表示当前目录,-name "*.txt" 是查找条件,表示查找文件名以 .txt 结尾的文件。

搜索 /home 目录下所有类型为目录的文件:

find /home -type d

-type d 是查找条件,表示只查找目录。

(3)搜索 /var/log 目录下修改时间在 7 天前的文件并删除它们:

find /var/log -type f -mtime +7 -exec rm {} \;

-type f 表示查找文件,-mtime +7 表示修改时间在 7 天前,-exec rm {} \; 是处理动作,表示对查找到的文件执行 rm 命令删除。

(4)搜索 /usr/bin 目录下所有可执行文件,并打印它们的权限:

find /usr/bin -type f -executable -exec ls -l {} \;

-executable 是查找条件,表示查找可执行文件。-exec ls -l {} \; 是处理动作,表示对查找到的文件执行 ls -l 命令打印其详细信息。

(5)找到httpd.conf文件

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

/etc/sysconfig/httpd

/etc/logrotate.d/httpd

/etc/httpd/conf/httpd.conf

5、find是exec选项

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

方法1 报错,rm不支持这种写法

方法2 rm和ls不支持管道

方法3 使用xargs 将查询结果交给rm

方法4 使用find执行 -exec

语法 find 文件路径

#查看目录中的txt文件

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

#没有e.txt文件,在前面被删除了

-rw-r--r--. 1 root root 0 7月 14 13:54 a.txt

-rw-r--r--. 1 root root 0 7月 13 00:00 b.txt

-rw-r--r--. 1 root root 0 7月 12 00:00 c.txt

-rw-r--r--. 1 root root 0 7月 11 00:00 d.txt

# 创建文件并且指定文件修改日期

[root@localhost opt]# touch -m -d "2024-7-10 00:00" e.txt

[root@localhost opt]# ls -l

总用量 0

-rw-r--r--. 1 root root 0 7月 14 13:54 a.txt

-rw-r--r--. 1 root root 0 7月 13 00:00 b.txt

-rw-r--r--. 1 root root 0 7月 12 00:00 c.txt

-rw-r--r--. 1 root root 0 7月 11 00:00 d.txt

-rw-r--r--. 1 root root 0 7月 10 00:00 e.txt

# 查找三天以前的文件

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

/opt/e.txt

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

# 这里在{}后面没有打空格报错了,在{}后应该打空格

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

rf {}\;

find: 遗漏“-exec”的参数

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

rf {} \;

总用量 0

-rw-r--r--. 1 root root 0 7月 14 13:54 a.txt

-rw-r--r--. 1 root root 0 7月 13 00:00 b.txt

-rw-r--r--. 1 root root 0 7月 12 00:00 c.txt

-rw-r--r--. 1 root root 0 7月 11 00:00 d.txt

二、文件修改日期(创建,修改,搜索)

1、文件的时间的概念:window中的时间

获取文件中的时间信息(stat命令)

[root@localhost ~]# stat /opt/test.conf

 文件:"/opt/test.conf"

 大小:23 块:8 IO 块:4096 普通文件

设备:fd00h/64768d Inode:34362655 硬链接:1

权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)

环境:unconfined_u:object_r:usr_t:s0

最近访问:2024-07-14 13:28:50.938662360 +0800

最近更改:2024-07-14 13:28:50.938662360 +0800

最近改动:2024-07-14 13:28:50.976662431 +080

案例

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

语法 touch -m -d 日期时间格式 文件名称

文件不存在就创建并修改时间

文件存在只配置最后修改时间

# 修改或者创建文件,并设置最后修改时间

[root@localhost ~]# touch -m -d "2020-7-7 00:00" /opt/abc.txt

[root@localhost ~]# ll /opt/

总用量 44

-rw-r--r--. 1 root root 17992 6月 17 13:58 1.png

-rw-r--r--. 1 root root 17503 6月 17 14:02 2.png

-rw-r--r--. 1 root root 0 7月 7 2020 abc.txt

-rw-r--r--. 1 root root 23 7月 14 13:28 test.conf

[root@localhost ~]# stat /opt/abc.txt

 文件:"/opt/abc.txt"

 大小:0 块:0 IO 块:4096 普通空文件

设备:fd00h/64768d Inode:33680845 硬链接:1

权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)

环境:unconfined_u:object_r:usr_t:s0

最近访问:2024-07-14 13:36:38.418740506 +0800

最近更改:2020-07-07 00:00:00.000000000 +0800

最近改动:2024-07-14 13:36:38.418740506 +0800

创建时间

创建第三个文件
查看四个文件
通过文件的最后修改时间搜索文件
语法
find 文件路径 -mtime +days/-days
-mtime 根据文件最后修改时间搜索文件
+ 搜索几天之前的文件信息
-搜索几天之内的文件信息

练习:

  1. 使用ls查看/etc/目录下所有的文件信息

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

[root@localhost ~]# find /etc/ -maxdepth 1 -name "*a*" -ls

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

[root@localhost ~]# ls -l /etc/*.conf

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

[root@localhost ~]# ls -l /etc/y*

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

[root@localhost ~]# find /var/ -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 /opt/test/abc.txt /opt/test/def.txt /opt/test/ghi.txt /opt/test/xxx.txt /opt/test/yyy.txt

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

[root@localhost ~]# touch -d "2023-01-15" /opt/test/abc.txt  

[root@localhost ~]# touch -d "2023-01-14" /opt/test/def.txt  

[root@localhost ~]# touch -d "2023-01-13" /opt/test/ghi.txt  

[root@localhost ~]# touch -d "2023-01-12" /opt/test/xxx.txt  

[root@localhost ~]# touch -d "2023-01-11" /opt/test/yyy.txt  

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

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

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

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

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

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

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

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

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

[root@localhost ~]# find /opt/ -type f -mtime -3 -exec rm {} \;  

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

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

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

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

16.创建⽬录/opt/test0

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

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

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

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

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

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

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

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

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

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

24.打开新的虚拟主机

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

[root@localhost ~]# scp ~/bak.tar.gz username@newhost:/opt/

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

[root@localhost ~]# scp -r username@newhost:/etc/skel/ /opt/

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

名中添加时间戳

[root@localhost ~]# crontab -e

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值