The Perl Script That May Save Your Life

I had a major “oh !@#$” moment tonight. While playing around with Maven, M2Eclipse and moving some project folders around I hastily hammered out a “sudo rm -R” and realized seconds later that I had blown away some code that I wrote last night that wasn’t in version control. All deleted. Not cool.

Fortunately I stumbled on this simple yet life saving article + perl script that greps the nothing of sda* for a particular string that was in your file and prints the contents around it:

#!/usr/local/bin/perl
open(DEV, '/dev/sda1') or die "Can't open: $!\n";
while (read(DEV, $buf, 4096)) {
  print tell(DEV), "\n", $buf, "\n"
    if $buf =~ /textToSearchFor/;
}

A quick run and a few minutes later I had my code back in one beautiful piece again. Mad props to chipmunk!

  1. David

    Bookmarked! I had no clue you could that.

  2. blah

    This just saved my ass.

  3. Andrew

    Wow dude, thanks a bunch for this. Just saved me two day’s worth of work.

Leave a Reply