4

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

5

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.

[ … ]

4

I found a way. I used GNU stat (stat (GNU coreutils) 8.19) to look at "Access", "Modify" and "Change" timestamps of a file.

I could update the "Change" time by doing a chmod u+x on the file. "Modify" and "Access" timestamps remained the same.

I could update "Access" file by doing a cat on it. "Modify" and "Change" timestamps remained the same.

I wrote a small C program that just does an open(filename, O_WRONLY);, writes a single byte to the file descriptor, and then a close(filedes); on the resulting file descriptor. stat showed no change on the subject file's "Access" timestamp, but "Modify" and "Change" timestamps got updated.

This was all under Linux 3.5.4, a fairly recently update Arch Linux laptop, on an Ext4 filesystem.

The small C program:

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int
main(int ac, char **av)
{
        int fd = open(av[1], O_WRONLY);
        if (fd >= 0)
        {
                char buf[12];
                write(fd, buf, 1);
                if (close(fd) < 0)
                        fprintf(stderr, "Problem closing file: %s\n",
                                strerror(errno));
        } else {
                fprintf(stderr, "Problem opening \"%s\": %s\n",
                        av[1], strerror(errno));
        }
        return 0;
}
  • Wouldn't this corrupt the file, since you are writing undefined data to the beginning of it? – a CVn Sep 28 '12 at 7:29
  • Yes. But it was the simplest example I could find. – Bruce Ediger Sep 28 '12 at 13:04
2

Remount the FS with noatime attribute, change the file, and remount it back.

2

Include the -m argument to touch. By default, the touch command modifies both the access time and the modification time; if you pass either -a or -m then only the specified time is modified.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.