通过c程序更改文件的ctime和mtime

通过c程序更改文件的ctime和mtime与某个文件相同

源文件words.txt
[root@bogon ~]# ll words.txt ;ll words.txt -u
-rw-r--r-- 1 root root 740 7月  14 2007 words.txt
-rw-r--r-- 1 root root 740 6月  12 09:33 words.txt
新建测试文件text.txt
[root@bogon ~]# touch text.txt
[root@bogon ~]# ll text.txt;ll text.txt -u
-rw-r--r-- 1 root root 23 6月  21 09:20 text.txt
-rw-r--r-- 1 root root 23 6月  21 09:20 text.txt
创建c程序
[root@bogon ~]# vim atimemtime.c 

#include <sys/stat.h>
#include <fcntl.h>
#include <utime.h>
/*#include <quit.h>*/

int main(int argc,char **argv) {
   struct stat statbuf;
   struct utimbuf timebuf;

   lstat(argv[1],&statbuf);
   timebuf.actime=statbuf.st_atime;
   timebuf.modtime=statbuf.st_mtime;
   utime(argv[2],&timebuf);
   close(argv[2]);

}
将c文件编译,编译后的文件名为testc
[root@bogon ~]# gcc -o testc atimemtime.c 
修改测试文件text.txt的时间为words.txt的访问时间和修改时间
[root@bogon ~]# ./testc words.txt text.txt
[root@bogon ~]# ll text.txt;ll text.txt -u
-rw-r--r-- 1 root root 23 7月  14 2007 text.txt
-rw-r--r-- 1 root root 23 6月  12 09:33 text.txt
原文地址:https://www.cnblogs.com/su-han/p/5602518.html