1. Perl
  2. builtin functions
  3. here

stat function - get detailed information about the file

Use the stat function to get detailed information about the file.

my @infos = stat $file;

The following is the information that can be obtained with stat.

0 File system device number
1 inode number
2 File mode
3 Number of hard links to this file
Four File owner user ID (number)
Five File owner group ID (number)
6 Device identifier
7 File size (bytes)
8 The number of seconds since the last access since the epoch
9 The number of seconds since the last update since the epoch
Ten The number of seconds since the epoch that has passed since the inode was changed
11 Block size considered appropriate for reading and writing files
12 Actual number of blocks allocated

Example

This is an example to get detailed information of a file using the stat function.

use strict;
use warnings;

# Get detailed information about the file stat

my $file = 'a.txt';
my @stat_inf = stat $file;

# Creating a stat commentary
my @stat_exp = (
  '0 dev filesystem device number',
  '1 ino i node number',
  '2 mode file mode',
  '3 nlink Number of hard links to this file',
  '4 uid file owner's user ID (number)',
  '5 gid file owner's group ID (number)',
  '6 rdev device identifier',
  '7 size File size (bytes)',
  '8 atime The number of seconds since the epoch that has passed since the last access',
  '9 mtime The number of seconds since the last update since the epoch',
  '10 ctime The number of seconds since the epoch that has passed since the inode was changed',
  '11 blksize Block size considered appropriate for reading and writing files',
  '12 blocks Actual number of blocks allocated',
);

if (@stat_inf) {
  for my $i (0 .. 12) {
    # Output by combining the information of stat and the explanation.
    printf("%-15s%s\n", $stat_inf[$i], $stat_exp[$i]);
  }
}
else {print "stat $file failed.\n"}

Simple supplementary rule

  • Device number: The file system is built in association with the device (hard disk). A number that identifies the hard disk (probably). → File system
  • inode: In Unix-like OS, it is like an identifier for managing files. → inode
  • File mode: file type and permissions. →
  • Hard link: On Unix-like operating systems, files can be aliased. The number of hard links is the total number of aliases that point to this file. → Hard link

Related Informatrion