Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the access time in Unix

Tags:

linux

unix

I want to know what access time is. I searched in the web but got the same definition:

read - gets changed

I know with touch we can change it. Could anyone explain me more about it with an example how it is changed? And is there a way to get the creating date/time in unix?

like image 670
Arav Avatar asked Aug 02 '10 05:08

Arav


People also ask

What is access time in Linux?

Linux's files have 3 timestamps recorded by the computer: Access timestamp (atime): which indicates the last time a file was accessed. Modified timestamp (mtime): which is the last time a file's contents were modified.

How do I check access time?

To see the access time for a file with ls , append the -u option in your command. In this case, our access time is the same as the file's modified time, which is normal for files that have not been accessed since they were last saved. Yet another tool we can use is the date command.

What is time stamp in Linux?

Linux stores these in the Unix time format which measures seconds since the epoch. The three timestamps are commonly referred to as atime, ctime, and mtime. The mtime is the most common and often the most useful. It stands for modified time. It's the time at which the file's contents were last written to disk.

How do you find the timestamp of a file in Unix?

The command is called stat . If you want to adjust the format, refer to the man pages, since the output is OS-specific and varies under Linux/Unix. Generally, you can get the times through a normal directory listing as well: ls -l outputs last time the file content was modified, the mtime.


1 Answers

stat structure

The stat(2) structure keeps track of all the file date/times:

struct stat {
    dev_t     st_dev;     /* ID of device containing file */
    ino_t     st_ino;     /* inode number */
    mode_t    st_mode;    /* protection */
    nlink_t   st_nlink;   /* number of hard links */
    uid_t     st_uid;     /* user ID of owner */
    gid_t     st_gid;     /* group ID of owner */
    dev_t     st_rdev;    /* device ID (if special file) */
    off_t     st_size;    /* total size, in bytes */
    blksize_t st_blksize; /* blocksize for file system I/O */
    blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
    time_t    st_atime;   /* time of last access */
    time_t    st_mtime;   /* time of last modification */
    time_t    st_ctime;   /* time of last status change */
};

st_atime is the access time, updated on read(2) calls (and probably also when open(2) opens a file for reading) — it is NOT updated when files are read via mmap(2). (Which is why I assume open(2) will mark the access time.)

st_mtime is the data modification time, either via write(2) or truncate(2) or open(2) for writing. (Again, it is NOT updated when files are written via mmap(2).)

st_ctime is the metadata modification time: when any of the other data in the struct stat gets modified.

Changing timestamps

You can change the timestamps on files with utime(2):

struct utimbuf {
    time_t actime;       /* access time */
    time_t modtime;      /* modification time */
};

Note you can only change the access time and (data) modification time. You can set either of these to arbitrary times, but the ctime is going to be set to the current time — because you changed the metadata for the file.

stat structure does not have create time

There is no create time in this structure, so it's not possible to find out when a file was created directly from the system.

If you really need to know the create time, you can narrow it down to a range by looking at your backups — assuming the file you're interested in has been backed up, along with its metadata.

statx structure does have create time

The statx(2) syscall introduced a new structure that can report the creation time of a file. Not all filesystems support this feature.

struct statx {
    __u32 stx_mask;        /* Mask of bits indicating
                              filled fields */
    __u32 stx_blksize;     /* Block size for filesystem I/O */
    __u64 stx_attributes;  /* Extra file attribute indicators */
    __u32 stx_nlink;       /* Number of hard links */
    __u32 stx_uid;         /* User ID of owner */
    __u32 stx_gid;         /* Group ID of owner */
    __u16 stx_mode;        /* File type and mode */
    __u64 stx_ino;         /* Inode number */
    __u64 stx_size;        /* Total size in bytes */
    __u64 stx_blocks;      /* Number of 512B blocks allocated */
    __u64 stx_attributes_mask;
                           /* Mask to show what's supported
                              in stx_attributes */

    /* The following fields are file timestamps */
    struct statx_timestamp stx_atime;  /* Last access */
    struct statx_timestamp stx_btime;  /* Creation */
    struct statx_timestamp stx_ctime;  /* Last status change */
    struct statx_timestamp stx_mtime;  /* Last modification */

    /* If this file represents a device, then the next two
       fields contain the ID of the device */
    __u32 stx_rdev_major;  /* Major ID */
    __u32 stx_rdev_minor;  /* Minor ID */

    /* The next two fields contain the ID of the device
       containing the filesystem where the file resides */
    __u32 stx_dev_major;   /* Major ID */
    __u32 stx_dev_minor;   /* Minor ID */
};
like image 100
sarnold Avatar answered Sep 20 '22 15:09

sarnold