Modification and Access time of a file

filestimestamps

Is it possible to change the file modification time without changing the file acess time?

Best Answer

The utime/utimes syscall lets you set the access and modification time arbitrarily. So you can stat the file, then use utime to change only one of them. From the man page:

NAME

utime, utimes - change file last access and modification times

SYNOPSIS

   #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]);

DESCRIPTION

The utime() system call changes the access and modification times of the inode specified by filename to the actime and modtime fields of times respectively.

If times is NULL, then the access and modification times of the file are set to the current time.

Changing timestamps is permitted when: either the process has appropri‐ ate privileges, or the effective user ID equals the user ID of the file, or times is NULL and the process has write permission for the file.

[ … ]

Related Question