使用Perl在Mac OS上读取实际的文件创建日期

时间:2013-07-26 23:41:44

标签: macos perl file

我想阅读Mac OS Finder上显示的文件的创建日期。这是我的代码的一部分:

require File::stat;
use Time::localtime;

($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($file_path);
print ctime($ctime) . "\n";

在Finder上,每个文件都有两个日期:创建和修改。我假设$ctime等于Finder中的“创建”日期,但它完全不同。做了一些研究,我发现大多数Unix操作系统都没有存储这种日期,但Mac OS确实如此。

有人知道从文件中读取此信息的方法吗?

2 个答案:

答案 0 :(得分:10)

只需阅读mdls命令的输出。您可以随时执行以下操作:

perl -e '@lines = qx(mdls filename.txt); print "@lines[3]";'

或类似。

请参阅man mdls

 mdls -- lists the metadata attributes for the specified file

您应该使用的创建日期:

mdls -raw -name kMDItemFSCreationDate filename 

Ps:模块MacOSX::File::Catalog是为此而开发的,但不幸的是它在Mountain Lion中被打破了。 (安装期间出错 - 至少在我的OS X上)

答案 1 :(得分:1)

mdls命令的问题是它的时间输出不在UNIX时间戳中。请尝试使用特定于Mac的功能stat命令:

stat -f %B _filename_

在perl:

$_="filename"
print scalar localtime `stat -f %B $_`