【问题标题】:How do I get a file's last modified time in Perl?如何在 Perl 中获取文件的最后修改时间?
【发布时间】:2010-10-05 07:20:45
【问题描述】:

假设我有一个文件句柄$fh。我可以用-e $fh 检查它的存在,或者用-s $fha slew of additional information about the file 检查它的文件大小。如何获取其最后修改的时间戳?

【问题讨论】:

    标签: perl file-io


    【解决方案1】:

    调用内置函数stat($fh)返回一个数组,其中包含有关传入的文件句柄的以下信息(来自perlfunc man page for stat):

      0 dev      device number of filesystem
      1 ino      inode number
      2 mode     file mode  (type and permissions)
      3 nlink    number of (hard) links to the file
      4 uid      numeric user ID of file's owner
      5 gid      numeric group ID of file's owner
      6 rdev     the device identifier (special files only)
      7 size     total size of file, in bytes
      8 atime    last access time since the epoch
      9 mtime    last modify time since the epoch
     10 ctime    inode change time (NOT creation time!) since the epoch
     11 blksize  preferred block size for file system I/O
     12 blocks   actual number of blocks allocated
    

    此数组中的第 9 号元素将为您提供自纪元以来的最后修改时间(格林威治标准时间 1970 年 1 月 1 日 00:00)。从中您可以确定当地时间:

    my $epoch_timestamp = (stat($fh))[9];
    my $timestamp       = localtime($epoch_timestamp);
    

    或者,您可以使用内置模块 File::stat(自 Perl 5.004 起包含)以获得更面向对象的界面。

    为了避免前面示例中需要的幻数 9,另外使用Time::localtime,另一个内置模块(Perl 5.004 也包括在内)。这些共同导致了一些(可以说)更清晰的代码:

    use File::stat;
    use Time::localtime;
    my $timestamp = ctime(stat($fh)->mtime);
    

    【讨论】:

    • localtime 是否应该更正时区更改?即:在 PST 中创建文件,然后从 CET 读取 mtime。我得到了令人困惑的结果。
    • 提及File::stat 的使用具有误导性,因为这将提供一种面向对象的方式来访问统计信息,并且直接访问数组元素是行不通的。无需使用File::stat 来访问例如(stat($fh))[9]
    【解决方案2】:

    使用内置的stat 函数。或者更具体地说:

    my $modtime = (stat($fh))[9]
    

    【讨论】:

      【解决方案3】:
      my @array = stat($filehandle);
      

      修改时间以Unix格式存储在$array[9]中。

      或明确:

      my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
          $atime, $mtime, $ctime, $blksize, $blocks) = stat($filepath);
      
        0 dev      Device number of filesystem
        1 ino      inode number
        2 mode     File mode  (type and permissions)
        3 nlink    Number of (hard) links to the file
        4 uid      Numeric user ID of file's owner
        5 gid      Numeric group ID of file's owner
        6 rdev     The device identifier (special files only)
        7 size     Total size of file, in bytes
        8 atime    Last access time in seconds since the epoch
        9 mtime    Last modify time in seconds since the epoch
       10 ctime    inode change time in seconds since the epoch
       11 blksize  Preferred block size for file system I/O
       12 blocks   Actual number of blocks allocated
      

      时代是格林威治标准时间 1970 年 1 月 1 日 00:00。

      更多信息在stat

      【讨论】:

        【解决方案4】:

        您需要 stat 调用和文件名:

        my $last_mod_time = (stat ($file))[9];
        

        Perl 也有不同的版本:

        my $last_mod_time = -M $file;
        

        但该值与程序启动的时间有关。这对于排序之类的事情很有用,但您可能想要第一个版本。

        【讨论】:

          【解决方案5】:

          如果您只是比较两个文件以查看哪个更新,那么 -C 应该可以工作:

          if (-C "file1.txt" > -C "file2.txt") {
          {
              /* Update */
          }
          

          还有-M,但我认为这不是你想要的。幸运的是,几乎不可能通过 Google 搜索有关这些文件运算符的文档。

          【讨论】:

          • 到谷歌-M,添加引号“-M”,因为-X 删除具有X 的结果...顺便说一句,'-M' 是 OP 想要的。
          • 使用““-C” 返回文件的年龄,不是(创建)日期)
          • -M(内容修改)和-C(inode更改)区别说明:unix.stackexchange.com/questions/132660/…
          【解决方案6】:

          您可以使用 stat() 或 File::Stat 模块。

          perldoc -f stat
          

          【讨论】:

            【解决方案7】:

            我认为您正在寻找 stat 函数 (perldoc -f stat)

            特别是,在返回列表的索引 9 处找到的项目(即第 10 个字段)是文件的最后修改时间,以 epoch 以来的秒数为单位。

            所以:

            my $last_modified = (stat($fh))[9];

            【讨论】:

              【解决方案8】:

              在我的FreeBSD 系统上,stat 只是返回一个祝福。

              $VAR1 = bless( [
                               102,
                               8,
                               33188,
                               1,
                               0,
                               0,
                               661,
                               276,
                               1372816636,
                               1372755222,
                               1372755233,
                               32768,
                               8
                             ], 'File::stat' );
              

              你需要像这样提取mtime

              my @ABC = (stat($my_file));
              
              print "-----------$ABC['File::stat'][9] ------------------------\n";
              

              print "-----------$ABC[0][9] ------------------------\n";
              

              【讨论】:

                【解决方案9】:

                这是一个非常古老的线程,但我尝试使用该解决方案,但无法从 File::stat 中获取信息。 (Perl 5.10.1)

                我必须做到以下几点:

                my $f_stats = stat($fh);
                my $timestamp_mod = localtime($f_stats->mtime);
                print "MOD_TIME = $timestamp_mod \n";
                

                只是想我分享一下,以防其他人遇到同样的麻烦。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-05-13
                  • 1970-01-01
                  • 2016-07-30
                  • 2010-09-27
                  • 1970-01-01
                  相关资源
                  最近更新 更多