文件的创建及配置

1.文件的时间的概念
创建时间
修改时间
访问时间
使用stat命令获取文件的时间信息
[root@tdr ~]# stat /opt/d.txt
  文件:"/opt/d.txt"
  大小:0             块:0          IO 块:4096   普通空文件
设备:fd00h/64768d    Inode:33868148    硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:usr_t:s0
最近访问:2024-07-15 09:35:15.545991127 +0800
最近更改:2024-07-13 00:00:00.000000000 +0800
最近改动:2024-07-15 09:35:15.545991127 +0800
创建时间:-


2.案例:创建文件,并配置文件的修改时间
语法 touch -m -d ⽇期时间格式 ⽂件名称
⽂件不存在就创建并修改时间
⽂件存在只配置最后修改时间
[root@tdr ~]# touch /opt/b.txt -m -d "2024-7-14 00:00"
[root@tdr ~]# touch /opt/c.txt -m -d "2024-7-11 00:00"
[root@tdr ~]# touch /opt/d.txt -m -d "2024-7-13 00:00"
[root@tdr ~]# ls -l /opt/
-rw-r--r--. 1 root root     251 7月   8 10:51 applicaiton.perperties
-rw-r--r--. 1 root root       0 7月  14 00:00 b.txt
-rw-r--r--. 1 root root       0 7月  11 00:00 c.txt
-rw-r--r--. 1 root root       0 7月  13 00:00 d.txt
[root@tdr ~]# touch /opt/c.txt -m -d "2024-7-1 00:00"
[root@tdr ~]# ls -l /opt/
-rw-r--r--. 1 root root       0 7月   1 00:00 c.txt


3.使用find按时间搜索,找到旧的文件并删除

find ⽂件路径 -mtime +days/-days
-mtime 根据⽂件最后修改时间搜索⽂件
+号 搜索⼏天之前的⽂件信息
-号 搜索⼏天之内的⽂件信息
[root@localhost ~]# find a -mtime +3      找到·修改时间在三天之前的文件
a/b
a/c
[root@localhost ~]# find a -mtime -3        找到修改时间在三天之内的文件
a
a/a.a
想要删除三天之前的文件
方法一:[root@localhost ~]# find a -mtime +3 | xargs rm -rf
[root@localhost ~]# ls -l a
-rw-r--r--. 1 root root 0 7月  15 17:36 a.a
把b,c文件再次创建回来进行方法二的测试
方法二:[root@localhost ~]# find a -mtime +3 -exec rm -rf {} \;
[root@localhost ~]# ls -l a
-rw-r--r--. 1 root root 0 7月  15 17:36 a.a
 # 查找三天以前的⽂件
[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 roo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值