Author Topic: How to get a last accessed time (i_atime) of a file  (Read 1876 times)

0 Members and 1 Guest are viewing this topic.

Offline winaiw

  • Jr. Member
  • **
  • Posts: 4
  • Karma: 0
  • I've just joined!
    • View Profile
    • Awards
How to get a last accessed time (i_atime) of a file
« on: December 22, 2013, 06:04:28 pm »
HI, 

when a Linux process P accesses a file F.  A 'struct file' structure represents file F as an opened file, which is opened by process P.
 Could you please let me know that if I want to know 'a last accessed time' (i_atime) of file F,  my question is that the last accessed time of file F will be in:

1-struct path     f_path;  or

2-struct inode   *f_inode;     

I have some reasons to write a  program to get the last accessed time of file F.

Best,

Winai

Offline musakilimanjaro

  • Jr. Member
  • **
  • Posts: 1
  • Karma: 1
  • Gender: Male
  • I've just joined!
    • View Profile
    • Love
    • Awards
Re: How to get a last accessed time (i_atime) of a file
« Reply #1 on: April 11, 2015, 07:12:20 am »
Found this excellent article in stackexchange on the subject, i think it best describes a solution for your problem

There are three timestamps normally recorded:
    mtime — updated when the file contents change. This is the "default" file time in most cases.
    ctime — updated when the file or its metadata (owner, permissions) change
    atime — updated when the file is read

So, generally, what you want to see is the atime of a file. You can get that with stat or with ls. You can use ls -lu to do this, although I prefer to use ls -l --time=atime (which should be supported in almost all modern Linux distributions) because I don't use it often, and when I do I can remember it better. And to sort by time, add the -t flag to ls. So there you go.

There is a big caveat, though. Updating the atime every time a file is read causes a lot of usually-unnecessary IO, slowing everything down. So, some Linux distributions now default to the noatime filesystem mount option, which basically kills atimes, or else relatime, which only updates atimes once a limit has passed (normally once per day) or if the file was actually modified since the previous read. You can find if these options are active by running the mount command.

Also, note that access times are by inode, not by filename, so if you have hardlinks, reading from one will update all names that refer to the same file.

And, be aware that c is not "creation"; creation isn't tracked by Unix/Linux filesystems, which seems strange but actually makes sense because the filesystem has no way of knowing if it is the original — maybe the file was created forty years ago and copied here. And, in fact, many file editors work by making copies over the original. If you need that information, it's best to use a version control system like git.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal