Home > php教程 > PHP开发 > body text

Thoroughly understand the meaning of the -mtime parameter of the find command

高洛峰
Release: 2016-12-14 17:16:32
Original
1593 people have browsed it

I have never understood the true meaning of the -mtime parameter of find before
When I write shell scripts and delete some files regularly, I often use the -mtime parameter, so I plan to take a closer look and figure it out.

Explanation in man find:
-mtime n
File's data was last modified n*24 hours ago. See the comments for -atime to understand how
rounding affects the interpretation of file modification times.

-atime n
File was last accessed n*24 hours ago. When find figures out how many 24-hour periods ago the
file was last accessed, any fractional part is ignored, so to match -atime +1, a file has to have
been accessed at least two days ago.

find $HOME -mtime 0
Search for files in your home directory which have been modified in the last twenty-four hours. This
command works this way because the time since each file was last modified is divided By 24 HourS and ANY
Remainder is discarded. That Means that to match -mtime 0, a file will have to have a modification in
the past which is less, The following file list is used to do experiments
[oracle@goolen arch]$ ll -h
total 1.1G
-rw-r----- 1 oracle oinstall 40M Apr 29 22:00 archive_1_53_844522225.arc
-rw-r----- 1 oracle oinstall 45M Apr 29 22:29 archive_1_54_844522225.arc
-rw-r----- 1 oracle oinstall 38M Apr 30 16:30 archive_1_55_844522225.arc
-rw-r----- 1 oracle oinstall 38M Apr 30 16:35 archive_1_56_8 44522225 .arc
-rw-r----- 1 oracle oinstall 42M Apr 30 22:00 archive_1_57_844522225.arc
-rw-r----- 1 oracle oinstall 45M Apr 30 22:36 archive_1_58_844522225.arc
-rw- r----- 1 oracle oinstall 38M May 1 14:00 archive_1_59_844522225.arc
-rw-r----- 1 oracle oinstall 38M May 1 22:00 archive_1_60_844522225.arc
-rw-r----- 1 oracle oinstall 41M May 1 22:37 archive_1_61_844522225.arc
-rw-r----- 1 oracle oinstall 41M May 2 22:00 archive_1_62_844522225.arc
-rw-r----- 1 oracle oinstall 45M May 2 22:38 archive_1_63_844522225.arc
-rw-r----- 1 oracle oinstall 42M May 3 06:00 archive_1_64_844522225.arc
-rw-r----- 1 oracle oinstall 40M May 3 14:09 archive_1_65_84452222 5.arc
-rw-r----- 1 oracle oinstall 45M May 3 22:39 archive_1_66_844522225.arc
-rw-r----- 1 oracle oinstall 39M May 4 06:00 archive_1_67_844522225.arc
-rw-r- ---- 1 oracle oinstall 38M May 4 17:00 archive_1_68_844522225.arc
-rw-r----- 1 oracle oinstall 45M May 5 08:10 archive_1_69_844522225.arc
-rw-r----- 1 oracle oinstall 45M May 5 10:28 archive_1_70_844522225.arc
-rw-r----- 1 oracle oinstall 6.7M May 5 14:57 archive_1_71_844522225.arc
-rw-r----- 1 oracle oinstall 38M May 5 22 :00 archive_1_72_844522225.arc
-rw-r----- 1 oracle oinstall 38M May 6 08:28 archive_1_73_844522225.arc
-rw-r----- 1 oracle oinstall 38M May 6 14:00 archive_1_74_844522225.ar c
-rw-r----- 1 oracle oinstall 39M May 6 22:00 archive_1_75_844522225.arc
-rw-r----- 1 oracle oinstall 45M May 7 08:29 archive_1_76_844522225.arc
-rw-r-- --- 1 oracle oinstall 38M May 7 22:00 archive_1_77_844522225.arc
-rw-r----- 1 oracle oinstall 38M May 8 05:00 archive_1_78_844522225.arc
-rw-r----- 1 oracle oinstall 38M May 8 09:00 archive_1_79_844522225.arc

[oracle@goolen arch]$ date
Thu May 8 16:29:09 CST 2014
++It’s May 8th 16:30

+++ Let’s go first Take a look at the output of the -mtime +1 parameter:
[oracle@goolen arch]$ find ./* -name "*.arc" -mtime +1 -exec ls -l {} ;
-rw-r-- --- 1 oracle oinstall 41009664 Apr 29 22:00 ./archive_1_53_844522225.arc
-rw-r---- 1 oracle oinstall 46589440 Apr 29 22:29 ./archive_1_54_844522225.arc
-rw-r---- -1 Oracle OINSTALL 39376384 APR 30 16:30 ./archive_1_55_84452225.ARC
-RW-R ---- 1 Oracle Oinstall 39756800 APR 30 16:35 ./archive_56_844452225.arc
-RW-R ----- 1 oracle oinstall 43597312 Apr 30 22:00 ./archive_1_57_844522225.arc
-rw-r----- 1 oracle oinstall 46589440 Apr 30 22:36 ./archive_1_58_844522225.arc
-rw-r----- 1 oracle o install 4270 1824 May 1 22:37 ./archive_1_61_844522225.arc
-rw-r----- 1 oracle oinstall 42967040 May  2 22:00 ./archive_1_62_844522225.arc
-rw-r----- 1 oracle oinstall 46589440 May  2 22:38 ./archive_1_63_844522225.arc
-rw-r----- 1 oracle oinstall 43959808 May  3 06:00 ./archive_1_64_844522225.arc
-rw-r----- 1 oracle oinstall 41638400 May  3 14:09 ./archive_1_65_844522225.arc
-rw-r----- 1 oracle oinstall 46589440 May  3 22:39 ./archive_1_66_844522225.arc
-rw-r----- 1 oracle oinstall 39922176 May  4 06:00 ./archive_1_67_844522225.arc
-rw-r----- 1 oracle oinstall 39352832 May  4 17:00 ./archive_1_68_844522225.arc
-rw-r----- 1 oracle oinstall 46589440 May  5 08:10 ./archive_1_69_844522225.arc
-rw-r----- 1 oracle oinstall 46373888 May  5 10:28 ./archive_1_70_844522225.arc
-rw-r----- 1 oracle oinstall 7001600 May  5 14:57 ./archive_1_71_844522225.arc
-rw-r----- 1 oracle oinstall 39364608 May  5 22:00 ./archive_1_72_844522225.arc
-rw-r----- 1 oracle oinstall 39477248 May  6 08:28 ./archive_1_73_844522225.arc
-rw-r----- 1 oracle oinstall 39361024 May  6 14:00 ./archive_1_74_844522225.arc

+++命令返回的文件列表为 archive_1_53_844522225.arc - archive_1_74_844522225.arc
从时间上来看,就是截至到5月6号 14点的
5月6号 还有一个文件是晚上22点生成的,输出结果里没有
-rw-r----- 1 oracle oinstall  39M May  6 22:00 archive_1_75_844522225.arc
按我以前的理解(错误的),-mtime +1 ,输出的应该是5月7号之前的所有文件,但是这里却没有输出6号晚上22点生成的archive_1_75_844522225.arc这个文件

正确的解释:
-mtime+1 表示文件修改时间为大于1天的文件,即距离当前时间2天(48小时)之外的文件
这也能解释了,为什么6号22点生成的这个文件为什么没有输出出来
这里-mtime +1 为什么是48小时以外,而不是24小时以外呢,因为n只能是整数,比1大的下一个整数是2,所以是48小时以外的

下面来看 -mtime 1的输出结果:
[oracle@goolen arch]$ find ./* -name "*.arc" -mtime 1 -exec ls -l {} ;  
-rw-r----- 1 oracle oinstall 40737280 May  6 22:00 ./archive_1_75_844522225.arc
-rw-r----- 1 oracle oinstall 46589440 May  7 08:29 ./archive_1_76_844522225.arc

结果输出为6号22点生成的archive_1_75_844522225.arc 和7号8点29生成的archive_1_76_844522225.arc 这两个文件

解释:
-mtime 1 表示文件修改时间距离当前为1天的文件,即距离当前时间1天(24小时-48小时)的文件

下面来看 -mtime 0 的输出结果:
[oracle@goolen arch]$ find ./* -name "*.arc" -mtime 0 -exec ls -l {} ; 
-rw-r----- 1 oracle oinstall 39303680 May  7 22:00 ./archive_1_77_844522225.arc
-rw-r----- 1 oracle oinstall 39483392 May  8 05:00 ./archive_1_78_844522225.arc
-rw-r----- 1 oracle oinstall 39208448 May  8 09:00 ./archive_1_79_844522225.arc

解释:
-mtime 0 表示文件修改时间距离当前为0天的文件,即距离当前时间不到1天(24小时)以内的文件

下面看-mtime -1 的输出结果:
[oracle@goolen arch]$ find ./* -name "*.arc" -mtime -1 -exec ls -l {} ; 
-rw-r----- 1 oracle oinstall 39303680 May  7 22:00 ./archive_1_77_844522225.arc
-rw-r----- 1 oracle oinstall 39483392 May  8 05:00 ./archive_1_78_844522225.arc
-rw-r----- 1 oracle oinstall 39208448 May  8 09:00 ./archive_1_79_844522225.arc

解释:
-mtime -1 表示文件修改时间为小于1天的文件,即距离当前时间1天(24小时)之内的文件

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template