How to display file last modification time on linux
I want to write a C program to display the file last modification time 开发者_Python百科in microsecond or millisecond. How could I do? Could you give me a help?
Thanks very much.
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:
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.
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.
JFS, XFS, ext4, and Btrfs support nanosecond timestamps.
The book "The Linux Programming Interface" by Michael Kerrisk has a good section on File attributes
There is a stat command, which you can use directly
http://www.thegeekstuff.com/2009/07/unix-stat-command-how-to-identify-file-attributes/
To complete answers by Andrew and ZelluX.
The limitation is in file system. For Linux ext3 is commonly used, and you can see in wikipedia:
Date resolution 1s
精彩评论