Unix中的访问时间是多少 [英] What is the access time in Unix
问题描述
我想知道什么是访问时间.我在网上搜索,但定义相同:
I want to know what access time is. I searched in the web but got the same definition:
阅读-被更改
read - gets changed
我知道使用touch
我们可以更改它.谁能用一个例子来向我解释一下它的变化方式?有没有一种方法可以在UNIX中获得创建日期/时间?
I know with touch
we can change it. Could anyone explain me more about it with an example how it is changed? And is there a way to get the creating date/time in unix?
推荐答案
stat(2)
结构跟踪所有文件的日期/时间:
The stat(2)
structure keeps track of all the file date/times:
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
st_atime
是 access 时间,在read(2)
调用时进行更新(可能还包括open(2)
打开文件进行读取时)-通过<mmap(2)
. (这就是为什么我认为open(2)
将标记访问时间的原因.)
st_atime
is the access time, updated on read(2)
calls (and probably also when open(2)
opens a file for reading) -- it is NOT updated when files are read via mmap(2)
. (Which is why I assume open(2)
will mark the access time.)
st_mtime
是数据的修改时间,可通过write(2)
或truncate(2)
或open(2)
进行写入. (同样,通过mmap(2)
写入文件时不会更新.)
st_mtime
is the data modification time, either via write(2)
or truncate(2)
or open(2)
for writing. (Again, it is NOT updated when files are written via mmap(2)
.)
st_ctime
是元数据的修改时间:struct stat
中的其他任何数据被修改的时间.
st_ctime
is the metadata modification time: when any of the other data in the struct stat
gets modified.
您可以使用utime(2)
更改文件的时间戳:
You can change the timestamps on files with utime(2)
:
struct utimbuf {
time_t actime; /* access time */
time_t modtime; /* modification time */
};
请注意,您只能更改访问时间和(数据)修改时间.您可以将其中任意一个设置为任意时间,但是ctime
将被设置为当前时间-因为您更改了文件的元数据.
Note you can only change the access time and (data) modification time. You can set either of these to arbitrary times, but the ctime
is going to be set to the current time -- because you changed the metadata for the file.
此结构没有创建时间,因此无法确定何时直接从系统创建文件.
There is no create time in this structure, so it's not possible to find out when a file was created directly from the system.
如果您确实需要了解创建时间,则可以通过查看备份将其范围缩小到一个范围-假设您感兴趣的文件及其元数据已经备份.
If you really need to know the create time, you can narrow it down to a range by looking at your backups -- assuming the file you're interested in has been backed up, along with its metadata.
这篇关于Unix中的访问时间是多少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!