Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a file's last modified time in Perl?

Tags:

file-io

perl

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?

like image 413
cowgod Avatar asked Feb 04 '09 00:02

cowgod


People also ask

How do I find the last modified date of a file?

The lastModified() method of the File class returns the last modified time of the file/directory represented by the current File object. You can get the last modified time of a particular file using this method.

How do you check file modification time in Linux?

The syntax is pretty simple; just run the stat command followed by the file's name whose last modification date you want to know, as shown in the example below. As you can see, the output shows more information than previous commands. It is important to differentiate the modification and change dates.

How do I print a timestamp in perl script?

Timestamp can be created by creating a DateTime object and then calling the now constructor. my $datetime = DateTime->now; print "$datetime\n" ; A DateTime object can also be created by providing all the details part wise like date, hour, minute, second, etc.

Which of the command given in options will display the last modification time of a file?

date command with -r option followed by the name of file will display the last modified date and time of the file.


2 Answers

Calling the built-in function stat($fh) returns an array with the following information about the file handle passed in (from the 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 

Element number 9 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:

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

Alternatively, you can use the built-in module File::stat (included as of Perl 5.004) for a more object-oriented interface.

And 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). Together these lead to some (arguably) more legible code:

use File::stat; use Time::localtime; my $timestamp = ctime(stat($fh)->mtime); 
like image 180
cowgod Avatar answered Nov 08 '22 20:11

cowgod


Use the builtin stat function. Or more specifically:

my $modtime = (stat($fh))[9] 
like image 40
Michael Carman Avatar answered Nov 08 '22 21:11

Michael Carman