Friday, June 17, 2011

Change file Modification Date from Perl

Today I had a task to change the modification date of file. In Linux it could be done using command:

touch -m -d "date" filename

But 'touch' command under Mac OS X has different parameters and after using the same command we will get error:

touch: illegal option -- d
usage: touch [-acfm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file



So to have this task done we had to write check for Mac OS and Linux platforms and pass different parameters to 'touch' command depending on OS.
To resolve this issue simple Perl script was written:

#!/usr/bin/perl

if (@ARGV[0] eq '' || @ARGV[1] eq '') {
  print "Change file modification date.\n";
  print "Usage: perl_touch.pl timestamp filename\n";
  exit;
}

utime undef, @ARGV[0], @ARGV[1];


First param - unix timestamp, second - filename. Scripts works under both - Mac OS X and Linux.

No comments:

Post a Comment