#!/usr/bin/perl -w # chyear # changes access or modification year of a file # based on a program of Robert Harley # Author: Alex Rhomberg #usage: first come the options (-a to modify access, -m to modify modify time) #then comes fromyear, then toyear, then the filenames #filenames can also be supplied through stdin #examples: #touch /tmp/now; find / -newer /tmp/now |chyear 2038 1998; rm /tmp/now #touch /tmp/now; chyear 2038 1998 `find / -newer /tmp/now`; rm /tmp/now my $usage = "Usage: $0 [-m] [-a] [ [ [...]]]\n". "\t-m modifies modify time, -a the access time\n". "if no option is given, both times are modified\n". "if no filename is given, they are taken from stdin\n"; use POSIX; # needed for mktime die $usage if $#ARGV < 1; my ($modacc, $modmod) = (0,0); while ($ARGV[0] =~ /^-(.*)$/) { shift; $modacc = 1 if $1 eq 'a'; $modmod = 1 if $1 eq 'm'; die $usage unless $1 =~ /^[am]$/; } $modacc = $modmod = 1 unless $modacc||$modmod; die $usage if $#ARGV < 1; my $fromyear = shift; my $toyear = shift; die "$0: years are strange\n" if (($fromyear < 1970) || ($fromyear > 2037) || ($toyear < 1970) || ($toyear > 2037)); if (@ARGV) { while(@ARGV) { $_ = shift; chyear($fromyear, $toyear, $_) unless -l $_; } } else { while (<>) { chomp; chyear($fromyear, $toyear, $_) unless -l $_; } } sub chyear { my $fy = shift() - 1900; my $ty = shift() - 1900; my $file = shift; my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = lstat($file); my @time = localtime ($atime); my $mod = 0; if($modacc) { if ($time[5] == $fy) { $time[5] = $ty; $atime = mktime(@time); $mod = 1; } } @time = localtime($mtime); if($modmod) { if ($time[5] == $fy) { $time[5] = $ty; $mtime = mktime(@time); $mod = 1; } } if ($mod) { print "changing $file\n"; utime $atime, $mtime, $file if $mod; } }