Search code examples
linuxperlfileunixstat

Perl: Get the user name and group name from stat commad


Here is the code snippet I am using and I am unable to get the user name and group name for the required file.

#!/usr/bin/perl
use strict;
use Data::Dumper;
use File::stat;
$somedir = '/var/vob/icc_shantesh/';
$file = $somedir."power.log";

my $gid = stat($file1)->gid;
my $uid = stat($file1)->uid;

print "User Id : $uid Group Id : $gid";

ouptut: User id : 23489 Group Id : 29023

How can I get user name and group name as well ?


Solution

  • getgrgid($gid);
    getpwuid($uid);
    

    might be useful (get group added, it was wrong)

    Update

    I changed your code to this and it worked

     #!/usr/bin/perl
     use strict;
     use Data::Dumper;
     use File::stat;
     $somedir = '/var/vob/icc_shantesh/';
     $file = $somedir."power.log";
    
     my ($gid) = getgrgid(stat($file)->gid);
     my ($uid) = getpwuid(stat($file)->uid);
    
     print "User Id : $uid Group Id : $gid";