Back from the Future - or - Fixing file dates retroactively

n1vux on 2006-01-02T03:59:37

I had a problem ... when we rebuilt my Linux server a year ago, the hardware Real Time Clock (RTC) got set to the wrong decade, probably due to a order-of-date issue, maybe a typo. So each time it rebooted, it reset the year to 2025, but otherwise correct. I usually fixed it fairly quickly, but took me quite a while to figure out that the RTC was the problem and how to re-write the RTC from commandline.

I looked at Date::Calc and DateTime modules, but neither made it easy to subtract 20 years from a file timestamp from (stat)[9] and reapply with utime. And DateTime.pm required 3 prerequisites, one of which required Module::Build. Eventually I'll hook up the CPAN<->Debian magic, but not until after I upgrade the OS, so that was out.

So, old trick -- separate the problem into easy steps.

perl -MPOSIX=strftime -MFile::Find 
  -le 'find( { wanted=>\&foo , follow=>1}, "/");
  sub foo {return if -M $_ > 0; 
   my $ts=strftime ("%Y%m%d%H%M.%S", 
        localtime ((stat($_))[9])); 
   return unless $ts=~s/\b(202[45])/$1-20/e; 
   print "touch -t $ts $File::Find::name";}' \
  | tee touch-2025
$ (set -x; . ./touch-2025)
+ . ./touch-2025
++ touch -t 200502052006.08 /
++ touch -t 200412192026.04 /homex
++ touch -t 200412192133.13 /homex/wdr
++ touch -t 200412192026.04 /homex/wdr/.bashrc
++ touch -t 200412192026.04 /homex/wdr/.bash_profile
...

Of course, I tested it as an unprivileged user before running it as root from / (via sudo bash).


Setting the hardware clock

tirwhan on 2006-01-02T08:44:42

Doesn't hwclock --systohc work?

Define "not easy"

autarch on 2006-01-02T17:23:11

This seems easy enough to me:

  my $dt = DateTime->from_epoch( epoch => (stat $file)[9] );
  $dt->subtract( years => 20 );
  utime $dt->epoch, $dt->epoch, $file;

Prereqs are another issue, but I don't really care if people complain about that so much.

Maybe by "not easy" you meant not in the docs as such, or not in the FAQ.