如何在Perl中获取文件的上次修改时间?

时间:2021-06-18 20:27:32

Suppose I have a filehandle $fh. I can check its existence with -e $fh or its file size with -s $fh or a slew of additional information about the file. How can I get its last modified time stamp?

假设我有一个文件句柄$ fh。我可以使用-e $ fh或-s $ fh的文件大小或关于该文件的大量附加信息来检查它的存在。如何获得最后修改的时间戳?

9 个解决方案

#1


92  

You can use the built-in module File::stat (included as of Perl 5.004).

您可以使用内置模块File :: stat(包含在Perl 5.004中)。

Calling stat($fh) returns an array with the following information about the file handle passed in (from the perlfunc man page for stat):

调用stat($ fh)返回一个数组,其中包含有关传入的文件句柄的以下信息(来自stat的perlfunc手册页):

  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

The 9th element in this array will give you the last modified time since the epoch (00:00 January 1, 1970 GMT). From that you can determine the local time:

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

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

To avoid the magic number 9 needed in the previous example, additionally use Time::localtime, another built-in module (also included as of Perl 5.004). This requires some (arguably) more legible code:

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

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

#2


22  

Use the builtin stat function. Or more specifically:

使用builtin stat函数。或者更具体地说:

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

#3


18  

my @array = stat($filehandle);

The modification time is stored in Unix format in $array[9].

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

Or explicitly:

或明确:

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

The epoch was at 00:00 January 1, 1970 GMT.

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

More information is in stat.

更多信息在stat。

#4


12  

You need the stat call, and the file name:

您需要stat调用和文件名:

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

Perl also has a different version:

Perl也有不同的版本:

my $last_mod_time = -M $file;

but that value is relative to when the program started. This is useful for things like sorting, but you probably want the first version.

但该值与程序启动时相关。这对于排序等内容非常有用,但您可能需要第一个版本。

#5


8  

If you're just comparing two files to see which is newer then -C should work:

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

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

There's also -M, but I don't think it's what you want. Luckily, it's almost impossible to search for documentation on these file operators via Google.

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

#6


3  

You could use stat() or the File::Stat module.

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

perldoc -f stat

#7


3  

I think you're looking for the stat function (perldoc -f stat)

我想你正在寻找stat函数(perldoc -f stat)

In particular, the 9th field (10th, index #9) of the returned list is the last modify time of the file in seconds since the epoch.

特别地,返回列表的第9个字段(第10个,索引#9)是自纪元以来以秒为单位的文件的最后修改时间。

So:

所以:

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

我的$ last_modified =(stat($ fh))[9];

#8


2  

On my FreeBSD system, stat just returns a bless.

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

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

You need to extract mtime like this:

你需要像这样提取mtime:

my @ABC = (stat($my_file));

print "-----------$ABC['File::stat'][9] ------------------------\n";

or

要么

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

#9


0  

This is very old thread, but I tried using the solution and could not get the information out of File::stat. (Perl 5.10.1)

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

I had to do the following:

我必须做以下事情:

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

Just thought I share in case anyone else had the same trouble.

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

#1


92  

You can use the built-in module File::stat (included as of Perl 5.004).

您可以使用内置模块File :: stat(包含在Perl 5.004中)。

Calling stat($fh) returns an array with the following information about the file handle passed in (from the perlfunc man page for stat):

调用stat($ fh)返回一个数组,其中包含有关传入的文件句柄的以下信息(来自stat的perlfunc手册页):

  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

The 9th element in this array will give you the last modified time since the epoch (00:00 January 1, 1970 GMT). From that you can determine the local time:

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

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

To avoid the magic number 9 needed in the previous example, additionally use Time::localtime, another built-in module (also included as of Perl 5.004). This requires some (arguably) more legible code:

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

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

#2


22  

Use the builtin stat function. Or more specifically:

使用builtin stat函数。或者更具体地说:

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

#3


18  

my @array = stat($filehandle);

The modification time is stored in Unix format in $array[9].

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

Or explicitly:

或明确:

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

The epoch was at 00:00 January 1, 1970 GMT.

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

More information is in stat.

更多信息在stat。

#4


12  

You need the stat call, and the file name:

您需要stat调用和文件名:

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

Perl also has a different version:

Perl也有不同的版本:

my $last_mod_time = -M $file;

but that value is relative to when the program started. This is useful for things like sorting, but you probably want the first version.

但该值与程序启动时相关。这对于排序等内容非常有用,但您可能需要第一个版本。

#5


8  

If you're just comparing two files to see which is newer then -C should work:

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

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

There's also -M, but I don't think it's what you want. Luckily, it's almost impossible to search for documentation on these file operators via Google.

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

#6


3  

You could use stat() or the File::Stat module.

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

perldoc -f stat

#7


3  

I think you're looking for the stat function (perldoc -f stat)

我想你正在寻找stat函数(perldoc -f stat)

In particular, the 9th field (10th, index #9) of the returned list is the last modify time of the file in seconds since the epoch.

特别地,返回列表的第9个字段(第10个,索引#9)是自纪元以来以秒为单位的文件的最后修改时间。

So:

所以:

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

我的$ last_modified =(stat($ fh))[9];

#8


2  

On my FreeBSD system, stat just returns a bless.

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

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

You need to extract mtime like this:

你需要像这样提取mtime:

my @ABC = (stat($my_file));

print "-----------$ABC['File::stat'][9] ------------------------\n";

or

要么

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

#9


0  

This is very old thread, but I tried using the solution and could not get the information out of File::stat. (Perl 5.10.1)

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

I had to do the following:

我必须做以下事情:

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

Just thought I share in case anyone else had the same trouble.

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