The stat and lstat Functions
Though these file tests are fine for testing various attributes regarding a particular file or filehandle, they don’t tell the whole story. For example, there’s no file test that returns the number of links to a file or the owner’s user ID (uid). To get at the remaining information about a file, call the stat function, which returns pretty much everything that the stat Unix system call returns (and more than you want to know).[270] The operand to stat is a filehandle or an expression that evaluates to a filename. The return value is either the empty list indicating that the stat failed (usually because the file doesn’t exist), or a 13-element list of numbers, most easily described using the following list of scalar variables:
my($dev, $ino, $mode, $nlink, $uid, $gid, $rdev,
$size, $atime, $mtime, $ctime, $blksize, $blocks)
= stat($filename);The names here refer to the parts of the stat structure, described in detail in the stat(2) manpage. You should look there for the detailed descriptions. Here’s a quick summary of the important ones:
- $dev and $ino
The device number and inode number of the file. Together, they make up a “license plate” for the file. Even if it has more than one name (hard link), the combination of device and inode numbers will be unique.
- $mode
The set of permission bits for the file and some other bits. If you’ve ever used the Unix command
ls -lto get a detailed (long) file listing, you’ll see that each line of output starts with ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access