Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
add comment

4 Answers

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

share|improve this answer
add comment

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;
}
share|improve this answer
    
Wouldn't this corrupt the file, since you are writing undefined data to the beginning of it? –  Michael Kjörling Sep 28 '12 at 7:29
    
Yes. But it was the simplest example I could find. –  Bruce Ediger Sep 28 '12 at 13:04
add comment

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.

[ … ]

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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