Perl来获取Linux文件的3个时间

利用Perl来获取Linux文件的3个时间


#!/usr/bin/perl

($atime,$mtime,$ctime) = &getFileAMCTime($_); 传进来一个文件名
print "Atime:$atime Mtime:$mtime Ctime:$ctime\n";

#获取文件的atime,mtime,ctime
sub getFileAMCTime {
     $filename = shift @_;
     my ($atime,$mtime ,$ctime) = (stat ($filename))[8,9,10];
     $atime = &getTime($atime);
     $mtime = &getTime($mtime);
     $ctime = &getTime($ctime);

     #将日期时间格式转换为比较友好的格式
     sub getTime {
          my $time = shift @_;
          my($sec,$min,$hour,$day,$mon,$year) = (localtime $time)[0..5];
          $time = sprintf "%4d-%02d-%02d %2d:%02d:%02d",$year + 1900,$mon + 1,$day,$hour,$min,$sec;
          return $time;
     }
     return $atime,$mtime,$ctime;
}

标签:linux, perl, 文件, 时间

添加新评论