Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Changing file modification timestamp

by monktim (Friar)
on Jun 04, 2007 at 14:58 UTC ( #619139=perlquestion: print w/replies, xml ) Need Help??

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

I have a multi-gigabyte file. Is there an easy way to change the modification timestamp of a file without opening it, modifing it, and closing it again? I'd like to do it using standard Perl. I'm using v5.8. I'd like to do it without creating a second or temporary file too. I just want to change the modifiation time, not the contents. It would be great if there was a simple function call to do it. Thanks in advance.

Update
Thanks for all the great answers. I should have said I was using ActiveState Perl on Windows. I just wrote a little touch.pl script and stuck it in my utilities folder. It amounts to this:
use strict; use warnings; die "bad file name: $ARGV[0]" if !-e $ARGV[0]; my $atime = (stat($ARGV[0]))[8]; utime $atime, time(), $ARGV[0];

Replies are listed 'Best First'.
Re: Changing file modification timestamp
by Fletch (Chancellor) on Jun 04, 2007 at 15:03 UTC

    utime is probably what you want (a knowledge of the POSIX C api can be handy).

      That worked great! You got me, I'm a Windos guy. Thanks.
Re: Changing file modification timestamp
by Zaxo (Archbishop) on Jun 04, 2007 at 15:18 UTC

    A file's stats are stored in the parent directory, not in the file itself. That makes the utime call that Fletch and Util recommend very efficient. The file that holds the directory inodes is usually small.

    After Compline,
    Zaxo

      Close, depending on the file system. UFS, for example, doesn't store that information in the directory, but the directory entry points to an inode that contains that information. The reason for this can be seen by attepting to make a hard link if the information is stored only in the parent directory.

      --MidLifeXis

Re: Changing file modification timestamp
by Util (Priest) on Jun 04, 2007 at 15:06 UTC
    It would be great if there was a simple function call to do it.

    There *is* a simple function call to do it: utime

Re: Changing file modification timestamp
by doom (Deacon) on Jun 04, 2007 at 18:11 UTC
    In addition to "utime", there's also the unix command-line utility "touch". If you use "touch", with no other arguments, it changes the timestamp on a file to the current time.

      In particular, touch and utime match if the file already exists. The touch command is basically equivalent to:
      if utime() fails, if create() succeeds, close() it (zero bytes written) else, give an error message

      --
      [ e d @ h a l l e y . c c ]

Re: Changing file modification timestamp
by 13warrior (Acolyte) on Jun 05, 2007 at 07:56 UTC
    Actually that is too simple a job you are asking from perl :). If you are in unix you can simply issue a touch command . perl -e "touch filename";
Re: Changing file modification timestamp
by mattr (Curate) on Jun 05, 2007 at 12:18 UTC
    It was a while ago but I believe I've used touch on windows, from cygwin. You just need cygwin1.dll in the same folder as touch.exe, and call it from bash.exe.
      It was a while ago but I believe I've used touch on windows, from cygwin. You just need cygwin1.dll in the same folder as touch.exe, and call it from bash.exe.

      There are ports which do not require the cygwin emulation layer nor a *nix shell to launched from:

      OTOH, does anybody remember Borland's touch.com?

Re: Changing file modification timestamp
by TGI (Parson) on Jun 07, 2007 at 22:47 UTC

    You might want to check out Perl Power Tools--a reworking of basic unix commands in perl. It includes a touch implementation.

    You should also check out the GnuWin32 project. They provide win32 ports for many Unix utilities.


    TGI says moo

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://619139]
Approved by Joost
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2020-01-22 17:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?