How to get file creation time in unix with perl?
I have this command which displays file's last modification time:
perl -MPOSIX -le 'print strftime "%d %b %Y %H:%M ".$_, localtime((lstat)[9]) for @ARGV' file.txt
How to get file creation time in unix with perl?
I have this command which displays file's last modification time:
perl -MPOSIX -le 'print strftime "%d %b %Y %H:%M ".$_, localtime((lstat)[9]) for @ARGV' file.txt
There is not normally file creation time in UNIX. A new xstat() interface promises support for it, but perl doesn't use it. It is possible to write an XS-based interface to gain access to it.
In Perl on UNIX,
ctime
stands for Inode Change Time, which has nothing to do with file creation.According to perlport,
ctime
will be creation time only on Windows, but you might want to use the Win32API::File::Time::GetFileTime() since it is more explicit (and will be obvious to porters) that your program depends on creation time instead of whateverctime
contains.You almost have it.
You just need to pass the return value from
localtime
instead of the filename tostrftime
:Outputs:
And even though it takes more code, I'd lean toward using
Time::Piece
andFile::stat
to make the code more modern and readable: