Alien has asked for the wisdom of the Perl Monks concerning the following question:

Hi there!
How can the atime , mtime and ctime be changed from Perl? I would also like to know how the inodes info can be retrieved!
Thanks guys!

Replies are listed 'Best First'.
Re: Changing file stats
by shigetsu (Hermit) on Feb 04, 2007 at 16:12 UTC

    Excerpt from & copyright by Perl documentation:

  • stat FILEHANDLE
  • stat EXPR
  • stat

    Returns a 13-element list giving the status info for a file, either the file opened via FILEHANDLE, or named by EXPR. If EXPR is omitted, it stats $_ . Returns a null list if the stat fails. Typically used as follows:

    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat($filename)

    Not all fields are supported on all filesystem types. Here are the meanings of the fields:

    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

    This is by no means complete. See stat for further information.

Re: Changing file stats
by graff (Chancellor) on Feb 04, 2007 at 19:17 UTC
    How can the atime , mtime and ctime be changed from Perl?

    Well of course, you would change a file's atime (to "now") by opening it for read access and reading some data from it; to change the mtime (to "now") you'd have to open for write access and then actually modify its contents somehow (i.e. actually write something to it). The only way I can imagine to set ctime (to "now") is to delete the file and then (re)create it.

    There's a unix shell command called "touch" that allows a bit more flexibility: it can be used to set atime and/or mtime -- either to "now" or to a specified date/time (but setting things to a future date might be a bad idea). The man page for "touch" doesn't say anything about altering the ctime of a file.

Re: Changing file stats
by dave_the_m (Monsignor) on Feb 04, 2007 at 17:35 UTC
    Note that the ctime of a file can't be changed to anything other than the current time.

    Dave.

      Oops. I thought it said 'mtime' up there (rather than 'ctime', as I see now).

      I think that's OS-dependent, somewhat. On Mac OS X, I was able to change the mtime of a file. I wouldn't be surprised if some systems require administrative access (or just file ownership), but I haven't tested anywhere else.

      Update: I tried it on Debian GNU/Linux too, and it works the same way.

      $ touch testme $ ls -l testme -rw-r--r-- 1 kyle kyle 0 2007-02-04 13:12 testme $ perl -e 'utime time,1111111111,"testme";' $ ls -l testme -rw-r--r-- 1 kyle kyle 0 2005-03-17 19:58 testme $
Re: Changing file stats
by TOD (Friar) on Feb 04, 2007 at 16:13 UTC
    there is no perl builtin for these purposes. use the POSIX funtions instead.

    man 2 utime ;)

      there is no perl builtin for these purposes.

      Actually, utime is a Perl built-in.

        *flush*