Bootstrap FreeKB - Perl (Scripting) - File Statistics
Perl (Scripting) - File Statistics

Updated:   |  Perl (Scripting) articles

his assumes you have installed the File::Stat Perl module on your system.

stat can be used to return the stats of a file.

#!/usr/bin/perl
use File::stat;
use strict;
use warnings;

my $stats = stat("/path/to/example.txt");

my $dev     = $stats->dev;  # device ID
my $ino     = $stats->ino;  # inode number
my $mode    = $stats->mode; # permissions of the file
my $nlink   = $stats->nlink;
my $uid     = $stats->uid;  # the ID of the user that owns the file
my $gid     = $stats->gid;  # the ID of the group that owns the file
my $rdev    = $stats->rdev; 
my $size    = $stats->size; # the size of the file in Bytes
my $atime   = $stats->atime; # the date and time the file was last accessed represented in epoch
my $ctime   = $stats->ctime; # the date and time the file was last changed represented in epoch
my $mtime   = $stats->mtime; # the date and time the file was last modified represented in epoch
my $blksize = $stats->blksize; # the block size of the file
my $blocks  = $stats->blocks;  # the number of blocks

print "\$dev     = $dev \n";     # device ID
print "\$ino     = $ino \n";     # inode number
print "\$mode    = $mode \n";    # permissions of the file
print "\$nlink   = $nlink \n";
print "\$uid     = $uid \n";     # the ID of the user that owns the file
print "\$gid     = $gid \n";     # the ID of the group that owns the file
print "\$rdev    = $rdev \n";
print "\$size    = $size \n";    # the size of the file in Bytes
print "\$atime   = $atime \n";   # the date and time the file was last accessed represented in epoch
print "\$mtime   = $mtime \n";   # the date and time the file was last changed represented in epoch
print "\$ctime   = $ctime \n";   # the date and time the file was last modified represented in epoch
print "\$blksize = $blksize \n"; # the block size of the file
print "\$blocks  = $blocks \n";  # the number of blocks

 

Something like this should be returned.

$dev     = 64768
$ino     = 9350651
$mode    = 33188
$nlink   = 1
$uid     = 409
$gid     = 2001
$rdev    = 0
$size    = 0
$atime   = 1757556327
$mtime   = 1757556327
$ctime   = 1757556341
$blksize = 4096
$blocks  = 0

 

getpwuid can be used of you want the name of the user that owns the file.

#!/usr/bin/perl
use File::stat;
use strict;
use warnings;

my $stats = stat("/tmp/example.log");
my $owner = getpwuid($stats->uid);

print "\$owner = $owner \n";

 

getgrgid can be used of you want the name of the group that owns the file.

#!/usr/bin/perl
use File::stat;
use strict;
use warnings;

my $stats = stat("/tmp/example.log");
my $group_name = getgrgid($stats->gid);

print "\$group_name = $group_name \n";

 

 

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter c0388c in the box below so that we can be sure you are a human.