Stat (Unix)

From Wikipedia, the free encyclopedia

stat() is a Unix system call that returns useful data about an inode. The semantics of stat() vary between operating systems. With Unix command ls, you can gather information about

  • mtime: time of last modification (ls -l),
  • ctime: time of last status change (ls -lc) and
  • atime: time of last access (ls -lu).

Note, that ctime is not the time of file creation. Overwriting a file changes mtime, ctime as well as atime. A change in file permissions or file ownership changes ctime and atime. Reading a file changes atime.

[edit] Synopsis

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int stat(const char *filename, struct stat *buf);
int lstat(const char *filename, struct stat *buf);
int fstat(int filedesc, struct stat *buf);

struct stat {
    dev_t         st_dev;      /* device */
    ino_t         st_ino;      /* inode */
    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 type (if inode device) */
    off_t         st_size;     /* total size, in bytes */
    blksize_t     st_blksize;  /* blocksize for filesystem I/O */
    blkcnt_t      st_blocks;   /* number of 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 */
};

[edit] In Perl

In the platform-independent scripting language Perl, stat() returns a thirteen-element array. From the Perl documentation:

Not all fields are supported on all filesystem types.  Here are the
meaning of the fields:

 0 dev      device number of filesystem
 1 ino      inode number
 2 mode     file mode  (type and permissions)
 3 nlink    number of (hard) links to the file
 4 uid      numeric user ID of file's owner
 5 gid      numeric group ID of file's owner
 6 rdev     the device identifier (special files only)
 7 size     total size of file, in bytes
 8 atime    last access time in Unix time format
 9 mtime    last modify time in Unix time format
10 ctime    inode change time (NOT creation time!) in Unix time format
11 blksize  preferred block size for file system I/O
12 blocks   actual number of blocks allocated

[edit] External links