Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

n1vux (1492)

n1vux
  (email not shown publicly)
http://boston.pm ... x.cgi?BillRicker
AOL IM: n1vux (Add Buddy, Send Message)
Yahoo! ID: n1vux (Add User, Send Message)

Only started with Perl4 and Perl5 in 1995. I was doing AWK etc for 12 years before that, and resisted switching. I've been doing OO since before C++ hit bigtime, with Objective-C and SmallTalk, so I really like the (no longer new) Perl5 OO style; and the Lispish Map style is also an old friend. What do I hack with Perl? All data that passes my way; systems monitoring scripts at $DayJob, weather data at night, and I cheat on NPR word puzzles. Member: Boston.pm.org [pm.org] BLU.org [blu.org] /. LinkedIn [linkedin.com]

N1VUX is my FCC-issued ham radio callsign.

Journal of n1vux (1492)

Sunday January 01, 2006
10:59 PM

Back from the Future - or - Fixing file dates retroactively

[ #28197 ]
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 CPANDebian 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).

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Doesn't hwclock --systohc work?
  • 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.