Modules

  • ABCDE
  • FGHIL
  • MNOPS
  • TUX

Tools

stat

Perl 5 version 8.8 documentation
Recently read

stat

  • stat FILEHANDLE

  • stat EXPR
  • stat

    Returns a 13-element list giving the status info for a file, either the file opened via FILEHANDLE, or named by EXPR. If EXPR is omitted, it stats $_ . Returns a null list if the stat fails. Typically used as follows:

    1. ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
    2. $atime,$mtime,$ctime,$blksize,$blocks)
    3. = stat($filename);

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

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

    (The epoch was at 00:00 January 1, 1970 GMT.)

    (*) Not all fields are supported on all filesystem types. Notably, the ctime field is non-portable. In particular, you cannot expect it to be a "creation time", see Files and Filesystems in perlport for details.

    If stat is passed the special filehandle consisting of an underline, no stat is done, but the current contents of the stat structure from the last stat, lstat, or filetest are returned. Example:

    1. if (-x $file && (($d) = stat(_)) && $d < 0) {
    2. print "$file is executable NFS file\n";
    3. }

    (This works on machines only for which the device number is negative under NFS.)

    Because the mode contains both the file type and its permissions, you should mask off the file type portion and (s)printf using a "%o" if you want to see the real permissions.

    1. $mode = (stat($filename))[2];
    2. printf "Permissions are %04o\n", $mode & 07777;

    In scalar context, stat returns a boolean value indicating success or failure, and, if successful, sets the information associated with the special filehandle _ .

    The File::stat module provides a convenient, by-name access mechanism:

    1. use File::stat;
    2. $sb = stat($filename);
    3. printf "File is %s, size is %s, perm %04o, mtime %s\n",
    4. $filename, $sb->size, $sb->mode & 07777,
    5. scalar localtime $sb->mtime;

    You can import symbolic mode constants (S_IF* ) and functions (S_IS* ) from the Fcntl module:

    1. use Fcntl ':mode';
    2. $mode = (stat($filename))[2];
    3. $user_rwx = ($mode & S_IRWXU) >> 6;
    4. $group_read = ($mode & S_IRGRP) >> 3;
    5. $other_execute = $mode & S_IXOTH;
    6. printf "Permissions are %04o\n", S_IMODE($mode), "\n";
    7. $is_setuid = $mode & S_ISUID;
    8. $is_setgid = S_ISDIR($mode);

    You could write the last two using the -u and -d operators. The commonly available S_IF* constants are

    1. # Permissions: read, write, execute, for user, group, others.
    2. S_IRWXU S_IRUSR S_IWUSR S_IXUSR
    3. S_IRWXG S_IRGRP S_IWGRP S_IXGRP
    4. S_IRWXO S_IROTH S_IWOTH S_IXOTH
    5. # Setuid/Setgid/Stickiness/SaveText.
    6. # Note that the exact meaning of these is system dependent.
    7. S_ISUID S_ISGID S_ISVTX S_ISTXT
    8. # File types. Not necessarily all are available on your system.
    9. S_IFREG S_IFDIR S_IFLNK S_IFBLK S_IFCHR S_IFIFO S_IFSOCK S_IFWHT S_ENFMT
    10. # The following are compatibility aliases for S_IRUSR, S_IWUSR, S_IXUSR.
    11. S_IREAD S_IWRITE S_IEXEC

    and the S_IF* functions are

    1. S_IMODE($mode) the part of $mode containing the permission bits
    2. and the setuid/setgid/sticky bits
    3. S_IFMT($mode) the part of $mode containing the file type
    4. which can be bit-anded with e.g. S_IFREG
    5. or with the following functions
    6. # The operators -f, -d, -l, -b, -c, -p, and -S.
    7. S_ISREG($mode) S_ISDIR($mode) S_ISLNK($mode)
    8. S_ISBLK($mode) S_ISCHR($mode) S_ISFIFO($mode) S_ISSOCK($mode)
    9. # No direct -X operator counterpart, but for the first one
    10. # the -g operator is often equivalent. The ENFMT stands for
    11. # record flocking enforcement, a platform-dependent feature.
    12. S_ISENFMT($mode) S_ISWHT($mode)

    See your native chmod(2) and stat(2) documentation for more details about the S_* constants. To get status info for a symbolic link instead of the target file behind the link, use the lstat function.