11

I tried to use command ls --time=atime

it was working in one directory but wasn't working in another directory. I opened certain files in vim, evince and other applications but after the command file access times weren't changed.

When I type touch file.txt the access time is changed, but when I type vim file.txt the access time is not changed.

What is wrong here?

mattdm
  • 40,245
xralf
  • 15,415
  • What OS are you running? Many modern Linux distributions turn off atime; it's off by default in recent Linux kernels. – Gilles 'SO- stop being evil' Sep 01 '11 at 22:55
  • I'm running Ubuntu 10.04. – xralf Sep 02 '11 at 13:14
  • What do you do after running vim file.txt? Do you quit vim by :q or :wq? If you don't write, vim does not modify the access time of the file - this is by design. – rozcietrzewiacz Sep 13 '11 at 22:02
  • Now I have less files in the directory and the access times are changed with vim and I don't need to edit the file, but when I open file with .jpg extension it's not changed. When I open it in image viewer it's not changed too. I need the access time be changed every time the file was read by some application. – xralf Sep 14 '11 at 14:17

3 Answers3

14

With default mounts options, access times are handled in a special way on linux:

relatime – A filesystem mount with this option causes the access time to be updated if they are (before the update has occurred) earlier than the modification time. This significantly cuts down the writes caused by atime updates.

The relatime mount option was made the default option since linux kernel 2.6.30. That is the reason for ls --time=atime not listing what you expect.

Solution: To use strict (legacy) behavior for atime use the strictatime mount option (e.g. in /etc/fstab).

Note: To completely disable atime updates, one can use noatime.

  • This helped. Thank you. How much slower will the system be with strictatime? – xralf Sep 14 '11 at 14:34
  • atime uptades are quite costly (one scheduled disk access to update inodes each time their file contents are accessed). In particular, if a lot of small files are involved, atime updates might double the disc load. Most of the time, programs relying on access times only want to check whether atime > mtime or not. So, the new default option relatime preserves compatibility for those programs. In fact, very few programs still rely on this, and it's often safe to use noatime. – Stéphane Gimenez Sep 14 '11 at 15:46
  • I like the strictatime feature as I try it today, but if it will make the system slow, I'd like to set strictatime only in certain directories, if it will be possible. – xralf Sep 14 '11 at 16:25
  • 1
    Well in practice it's at most 1% or 2% slower, maybe up to 5% on a server with high disk load. Well, if you really care about performance, it's possible to restrict atime updates to a specific directory using a dedicated fs mounted with strictatime that would use this directory as mount point. – Stéphane Gimenez Sep 14 '11 at 16:59
2

You can turn off access time recording for some file systems (look for noatime in /etc/fstab). To test this, you could try to touch -a file; ls -l --time=atime file, then repeat touch -a file; ls -l --time=atime file to see if it changes.

The ls behavior is explained by man ls: ls -l --time=atime shows access time in the date/time column. To sort by this key you need to use --sort=time as well.

If you by "the time is not changed" mean that it shows the same result, that is either because atime is disabled for the file system (see /etc/fstab) or because the access time is really the same as the modification time (quite common).

Note that listing files does not change their access time - The files are not accessed by ls, only the inode cache.

ssb
  • 21
l0b0
  • 51,350
  • When I type ls -l --time=atime the time is not changed (I assume that the column with time is atime) and the sorting order is not changed, the files with last atime should be at the beginning or at the end of the list (if they were last accessed). – xralf Sep 01 '11 at 10:04
  • @xralf Did you repeat both touch and ls? I edited the answer to make it more evident. – rozcietrzewiacz Sep 01 '11 at 11:51
  • When I type touch file.txt the access time is changed, but when I type vim file.txt the access time is not changed. – xralf Sep 01 '11 at 12:17
1

It might also be useful when testing like this to add the --full-time flag to ls so that you get to see seconds and sub-seconds so that you can see the effect without being constrained by minute boundaries.

Kevin
  • 40,767
ssb
  • 21