linux获得文件夹修改时间,linux 查看文件相应修改时间

What is the difference between mtime, atime and ctime?

mtime, atime and ctime are timestamps associated with the modification, access and change of any file respectively.

mtime

mtime (modification time) indicates the time the contents of the file has been changed. Mind you, only the contents. Not the attributes. For instance, if you open a file and change some (or all) of its content, its mtime gets updated. If you change a file's attribute (like read-write permissions, metadata), its mtime doesn't change, but ctime will.

atime

atime (access time) is the timestamp that indicates the time that a file has been accessed. The file may have been opened by you, or may have been accessed by some other program or a remote machine. Anytime a file has been accessed, its access time changes.

ctime

ctime (change time) is the timestamp of a file that indicates the time that it was changed. Now, the modification can be in terms of its content or in terms of its attributes. Whenever anything about a file changes (except its access time), its ctime changes.

Let me demonstrate it by the following example.

On a Unix-like system, lets create a file and check its atime, mtime and ctime.

9a78306f70883a232300d53157c808c5.png

We can see, all the timestamps are same.

Now, let us print its contents.

a78f19fdbc9ac66996175be54a3ef01e.png

Since the text file was blank, it didn't print anything. But looking at its stats, we see the access time has changed, while the others remain unchanged.

Now, let us write something into the file.

3705824e0f14bb9f08c0a502799d0544.png

Notice how all of its timestamps have changed! Since the file had to be accessed to modify it, the atime has changed. And since the contents of the file changed, its mtime and ctime both changed.

Now, instead of contents, what happens if we change its attributes? Say, file permissions?

9afa23a773cf88bff1086e960a03318c.png

We added the 'execute' permission to the file and see how its atime and ctime changes! As is obvious, to make any changes to the file, you have to access it. Hence the changes to atime. Also, since only the attributes were changed and not the contents, the ctime changed while mtime remained as it is.

In general, this is how the timestamps behave:

mtime <= ctime <= atime

Credits: Thanks to Gami Nipulkumar for correcting the answer.

具体应用:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值