Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading the actual file creation date on Mac OS with Perl

Tags:

file

macos

perl

I would like to read the creation date of a file as it is represented on the Mac OS Finder. This is part of my code:

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";

On the Finder, every file has two dates: created and modified. I assumed that $ctime would be equal to the "created" date from the Finder, but it is completely different. Doing some research, I found out that most Unix operating systems don't store this kind of date, however, Mac OS does.

Does anybody know a way to read this information from a file?

like image 401
calvillo Avatar asked Dec 06 '22 06:12

calvillo


1 Answers

Simply read the output from the mdls command. You can always do something like:

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

or similar.

see man mdls

 mdls -- lists the metadata attributes for the specified file

for creation date you should use:

mdls -raw -name kMDItemFSCreationDate filename 

Ps: the module MacOSX::File::Catalog is developed for this, but unfortunately it is broken in Mountain Lion. (error during install - at least on my OS X)

like image 194
jm666 Avatar answered Jan 19 '23 00:01

jm666