分享

utime -- 更改时间戳

 hj_18 2012-06-14

utime -- 更改时间戳

有时候可能需要修改一下文件的时间。关于文件的时间戳一共有 3 个:

modification time (mtime,修改时间) :该时间在修改文件后被设置。

access time (atime,访问时间) :当文件被读取时,该时间被修改。

status time (ctime,状态时间) :该时间为“状态时间”,也可以称为“改变时间”,如你对文件设置了属性或者移动它到另外的目录时,该时间被设置。

在 linux 中,可使用 touch 命令进行修改 atime 和 mtime 。

同系统函数 utime 一样,Perl 也用 utime 来修改 mtime 和 atime 这两个时间,它的用法是:
utime {最后访问时间}, {最后修改时间}, 要修改的文件列表;


考虑下面代码:
#!/usr/bin/perl


my $now = time;
my $ago = $now - 24 * 60 * 60;  #一天前

#最后访问时间为当前时间,最后修改时间为前一天
utime $now, $ago, glob "*.txt";

运行结果:
ls -l *.txt
-rw-r--r-- 1 beyes beyes 0 Oct 11 14:48 test1.txt
-rw-r--r-- 1 beyes beyes 0 Oct 11 14:48 test2.txt
-rw-r--r-- 1 beyes beyes 0 Oct 11 14:48 test3.txt
-rw-r--r-- 1 beyes beyes 0 Oct 11 14:48 test4.txt
-rw-r--r-- 1 beyes beyes 0 Oct 11 14:48 test5.txt


在 文件有任何改动时,ctime 一定会被设置为当前时间的,所以没有函数可以修改它,即使假设 utime 对其修改成功,那么它也会被系统立刻设置回 "now" ,因为 ctime 主要是该程序做增量备份的:比如磁盘上的某个文件比备份盘上的要新,那么就需要再次备份了。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多