linux 查找一天前的文件,Linux7.2中用 命令 find / -mtime +1 真的是查找1天前创建的文件吗?...

【问题】

设置系统时间为2016-1-11,在目录/tmp/wwwdir/下创建若干文件/目录。然后设置系统时间为2016-1-12,在同一个目录下创建若干文件/目录,最后设置系统时间为2016-1-13,查找/tmp/wwwdir/下一天前(即2016-1-12)和两天前(即2016-1-11)创建的文件。

【初步实现操作】[root@xuegod60 ~]# date -s 2016-1-11

2016年 01月 11日 星期一 00:00:00 CST

[root@xuegod60 ~]# mkdir /tmp/wwwdir

[root@xuegod60 ~]# cd /tmp/wwwdir/

[root@xuegod60 wwwdir]# touch 11.txt

[root@xuegod60 wwwdir]# mkdir 11.d

[root@xuegod60 wwwdir]# echo aaa11 >>11.d/11.txt

[root@xuegod60 wwwdir]# date -s 2016-1-12

2016年 01月 12日 星期二 00:00:00 CST

[root@xuegod60 wwwdir]# touch 12.txt

[root@xuegod60 wwwdir]# mkdir 12.d

[root@xuegod60 wwwdir]# echo bbb12>>12.d/12.txt

[root@xuegod60 ~]# date -s 2016-1-13

2016年 01月 13日 星期三 00:00:00 CST

[root@xuegod60 ~]# find /tmp/wwwdir/* -mtime +1

[root@xuegod60 ~]# find /tmp/wwwdir/* -mtime +2

[root@xuegod60 ~]# find /tmp/wwwdir/* -mtime 2

[root@xuegod60 ~]# find /tmp/wwwdir/* -mtime 1

/tmp/wwwdir/11.d

/tmp/wwwdir/11.d/11.txt

/tmp/wwwdir/11.txt

/tmp/wwwdir/12.txt

到这里问题就出来了,找不到11日和12日创建的文件了。

但是查看/tmp/wwwdir/目录中的文件,可以清楚显示创建时间[root@xuegod60 ~]# ll /tmp/wwwdir/

总用量 0

drwxr-xr-x 2 root root 19 1月  11 00:04 11.d

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

drwxr-xr-x 2 root root 19 1月  12 00:00 12.d

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

/tmp/wwwdir/目录里明明有,为什么找不到,命令错了吗?

上面的命令也奇怪,查找的命令格式为

find [目录名] -mtime +N    # N为天数

可是,同样是2天,为什么有“+”就搜不到文件呢?

后来参考网络文章找到了查找方法

【当前时间】

[root@xuegod60 ~]# date -s2016-1-13

2016年 01月 13日星期三 00:00:00 CST

【查看/tmp/wwwdir/下的文件】[root@xuegod60 ~]# ll/tmp/wwwdir/

总用量 0

drwxr-xr-x 2 root root 19 1月  11 00:04 11.d

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

drwxr-xr-x 2 root root 19 1月  12 00:0012.d

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

【查找2016-1-11的文件】

[root@xuegod60 ~]# find/tmp/wwwdir/* -mtime 1.9

/tmp/wwwdir/11.d

/tmp/wwwdir/11.d/11.txt

/tmp/wwwdir/11.txt

【查找2016-1-12的文件】[root@xuegod60 wwwdir]# find/tmp/wwwdir/ -mtime 0.9

/tmp/wwwdir/12.d

/tmp/wwwdir/12.d/12.txt

/tmp/wwwdir/12.txt

到目前,似乎找到了1天前和2天前的文件了。但这个值是怎么来的,不得而知。

半个小时后,再次查找[root@xuegod60 ~]# date

2016年 01月 13日 星期三 00:26:54 CST

[root@xuegod60 ~]# find /tmp/wwwdir/ -mtime +1

/tmp/wwwdir/11.d

/tmp/wwwdir/11.d/11.txt

/tmp/wwwdir/11.txt

[root@xuegod60 ~]# find /tmp/wwwdir/ -mtime +1.5

[root@xuegod60 ~]# find /tmp/wwwdir/ -mtime +1.1

[root@xuegod60 ~]# find /tmp/wwwdir/ -mtime +1.9

[root@xuegod60 ~]# find /tmp/wwwdir/ -mtime +2

[root@xuegod60 ~]# find /tmp/wwwdir/ -mtime +2.1

[root@xuegod60 ~]# find /tmp/wwwdir/ -mtime +2.5

[root@xuegod60 ~]# find /tmp/wwwdir/ -mtime +2.9

[root@xuegod60 ~]# find /tmp/wwwdir/ -mtime +3

一个半小时后结果[root@xuegod60 ~]# date

2016年 01月 13日 星期三 01:23:45 CST

[root@xuegod60 ~]# find /tmp/wwwdir/ -mtime 2

/tmp/wwwdir/11.d

/tmp/wwwdir/11.d/11.txt

/tmp/wwwdir/11.txt

[root@xuegod60 ~]# find /tmp/wwwdir/ -mtime 1

/tmp/wwwdir/12.d

/tmp/wwwdir/12.d/12.txt

/tmp/wwwdir/12.txt

[root@xuegod60 ~]#

从上面的测试结果发现,去除“+”后的命令格式是正确的。

在这里我有一个猜想:

以上面例子中

/tmp/wwwdir/12.d

/tmp/wwwdir/12.d/12.txt

/tmp/wwwdir/12.txt

文件创建的时间为00:00~00:59,均为01:00前的创建时间.一天后查询时,应该在01:00时间点后,find命令应该写成

find /tmp/wwwdir/ -mtime 1

那至于2天前的文件,那就应该是

find /tmp/wwwdir/ -mtime 2

N天前的也就会了:

find [目录名] -mtime N

要是搜索的文件距离现在不足一天,那就应该在-mtime的值上减少,用小数表示。还以上面例子

假设当前时间与文件创建时间在通过以时间范围,那查找1天前和2天前的文件情况就应该是:[root@xuegod60 ~]# date -s 2016-1-13

2016年 01月 13日 星期三 00:00:00 CST

[root@xuegod60 ~]# find /tmp/wwwdir/* -mtime 0.9

/tmp/wwwdir/12.d

/tmp/wwwdir/12.d/12.txt

/tmp/wwwdir/12.txt

[root@xuegod60 ~]# find /tmp/wwwdir/* -mtime 1.9

/tmp/wwwdir/11.d

/tmp/wwwdir/11.d/11.txt

/tmp/wwwdir/11.txt

具体的时间大概是5分钟的时差:[root@xuegod60 ~]# ll /tmp/wwwdir/

总用量 0

drwxr-xr-x 2 root root 19 1月  11 00:04 11.d

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

drwxr-xr-x 2 root root 19 1月  12 00:00 12.d

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

[root@xuegod60 ~]# date -s 00:04

2016年 01月 13日 星期三 00:04:00 CST

[root@xuegod60 ~]# find /tmp/wwwdir/ -mtime 1

/tmp/wwwdir/

/tmp/wwwdir/11.d

/tmp/wwwdir/12.txt

/tmp/wwwdir/12.d

/tmp/wwwdir/12.d/12.txt

[root@xuegod60 ~]# date -s 00:05

2016年 01月 13日 星期三 00:05:00 CST

[root@xuegod60 ~]# find /tmp/wwwdir/ -mtime 1

/tmp/wwwdir/

/tmp/wwwdir/12.txt

/tmp/wwwdir/12.d

/tmp/wwwdir/12.d/12.txt

与此同理,-ctime 和 -atime 的参数值也就可以退出了。

文中如有不妥之处,还请各位不吝指教。谢谢!

谢谢ITpub的maohaiqing0304

【参考资料】

[原创]Linux 命令 find / -ctime +1 真的是查找1天前创建的文件咩?

http://blog.itpub.net/28602568/viewspace-1404761/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值