该utime
/utimes
系统调用,您可以随意设置访问和修改时间。因此,您可以使用stat
该文件,然后utime
仅更改其中之一。从手册页:
姓名
utime, utimes - 更改文件上次访问和修改时间
概要
Run Code Online (Sandbox Code Playgroud)#include <sys/types.h> #include <utime.h> int utime(const char *filename, const struct utimbuf *times); #include <sys/time.h> int utimes(const char *filename, const struct timeval times[2]);
描述
utime() 系统调用将 filename 指定的 inode 的访问和修改时间分别更改为时间的 actime 和 modtime 字段。
如果时间为 NULL,则文件的访问和修改时间设置为当前时间。
在以下情况下允许更改时间戳:ate 权限,或者有效用户 ID 等于文件的用户 ID,或者时间为 NULL 并且进程对该文件具有写权限。
[……]