Sunday, July 17, 2011

To view the file information perl

To view the file information for a file in perl use the following statement:

Content of the Perl File:
use POSIX qw(strftime);
use File::stat;
my $sb = stat("test.txt");
print "Size: ".$sb->size."\n";
print "mtime in unreadable format:". $sb->mtime."\n";
$mtime=$sb->mtime;
my @systime =  localtime($mtime) ;
 my $str = strftime( "%a %b %e %H:%M:%S %Y", @systime );
print "mtime in readable format:".$str."\n";
 print "chmmod :".$sb->mode ."\n";
printf "Permissions are %04o\n", $sb->mode & 07777;
print "ctime :". $sb->ctime."\n";
 #ctime can also be converted into correct format similarly as did for mtime

We can access all the other file attributes given below by using $sb->needed file attribute as done for attribtes like mtime,ctime.

  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

This was reffered from http://perldoc.perl.org/functions/stat.html

Output :

G:\perl_programs>perl perl_size.pl
Size: 42
mtime in unreadable format:1310916921
mtime in readable format:Sun Jul  21:05:21 2011
chmmod :33206
Permissions are 0666
ctime :1310916921

G:\perl_programs>

No comments:

Post a Comment