如何在linux上显示文件上次修改时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5086471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display file last modification time on linux
提问by JavaMobile
I want to write a C program to display the file last modification time in microsecond or millisecond. How could I do? Could you give me a help?
我想编写一个 C 程序,以微秒或毫秒为单位显示文件上次修改时间。我怎么办?你能给我一个帮助吗?
Thanks very much.
非常感谢。
采纳答案by caf
The stat()
function is used. In sufficiently recent versions of glibc, st_mtim
(note: no trailing e
) is a field of type struct timespec
that holds the file modification time:
该stat()
功能使用。在足够新的 glibc 版本中,st_mtim
(注意:没有尾随e
)是一个struct timespec
保存文件修改时间的类型字段:
struct stat st;
if (stat(filename, &st)) {
perror(filename);
} else {
printf("%s: mtime = %lld.%.9ld\n", filename, (long long)st.st_mtim.tv_sec, st.st_mtim.tv_nsec);
}
You should check for the presence of st_mtim
in struct stat
in your build system, and be ready to fall back to st_mtime
(which has type time_t
, and only 1 second resolution) if it is not present.
您应该检查构建系统中是否存在st_mtim
in struct stat
,如果不存在,则准备回退到st_mtime
(具有 type time_t
,并且只有 1 秒的分辨率)。
回答by ZelluX
You may use stat() function, it will return struct stat which contains time of last modification of a file. Here is the man page http://linux.die.net/man/2/stat. As to precision, it depends on whether your file system supports sub-second timestamps or not.
您可以使用 stat() 函数,它会返回 struct stat ,其中包含最后修改文件的时间。这是手册页http://linux.die.net/man/2/stat。至于精度,这取决于您的文件系统是否支持亚秒级时间戳。
回答by Nikita Rybak
回答by Matt
JFS, XFS, ext4, and Btrfs support nanosecond timestamps.
JFS、XFS、ext4 和 Btrfs 支持纳秒时间戳。
The book "The Linux Programming Interface" by Michael Kerrisk has a good section on File attributes
Michael Kerrisk 所著的“Linux 编程接口”一书对文件属性有很好的介绍
回答by byte_array
There is a stat command, which you can use directly
有一个stat命令,你可以直接使用
http://www.thegeekstuff.com/2009/07/unix-stat-command-how-to-identify-file-attributes/
http://www.thegeekstuff.com/2009/07/unix-stat-command-how-to-identify-file-attributes/