From: Alex Rhomberg <rhomberg@ife.ee.ethz.ch>
To: Tan Wee Yeh <tanwy@comp.nus.edu.sg>, "G. Hugh SONG" <ghsong@kjist.ac.kr>
Subject: Re: How can I change year 2018 to 1998
Date: 1998/10/16 [thread overview]
Message-ID: <36272192.D3D26328@ife.ee.ethz.ch> (raw)
In-Reply-To: 706ugu$1sg23@id4.nus.edu.sg
[-- Attachment #1: Type: text/plain, Size: 1375 bytes --]
Tan Wee Yeh wrote:
>
> Robert Harley <harley@corton.inria.fr> wrote:
>
> > foreach file ( `find . -mtime -0 -print` )
> > chyear -m $file 2018 1998
> > end
>
> Correct me if I'm wrong but
>
> # find . -mtime -0 -exec chyear -m {} 2018 1998 \;
>
> would probably be more efficient :).
Both methods are very inefficient, because the chyear program has to be
started freshly for every file, resulting in high system cpu usage.
It's better to modify the program so it accepts any number of filenames
as arguments or reads them from stdin like the file I attached. This
way, the find results can be piped or backticked, resulting in only a
single call each of chyear and find.
I try to avoid -exec in find calls.
use
find . -mtime -0 |chyear 2018 1998
or
chyear 2018 1998 `find . -mtime -0 -mount`
- Alex
--
_________________________________________________________________
/ \
| Alex Rhomberg Tel: +41 1 632 49 18 |
| Institut fuer Elektronik Fax: +41 1 632 12 10 |
| ETH Zuerich Zentrale: +41 1 632 11 11 |
| Gloriastrasse 35 |
| 8092 Zuerich email: rhomberg@ife.ee.ethz.ch |
\_________________________________________________________________/
[-- Attachment #2: chyear --]
[-- Type: text/plain, Size: 2053 bytes --]
#!/usr/bin/perl -w
# chyear
# changes access or modification year of a file
# based on a program of Robert Harley
# Author: Alex Rhomberg <rhomberg@ife.ee.ethz.ch>
#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] <fromyear> <toyear> [<file1> [<file2> [...]]]\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;
}
}
next prev parent reply other threads:[~1998-10-16 4:00 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
1998-10-13 4:00 How can I change year 2018 to 1998 G. Hugh SONG
1998-10-15 4:00 ` Robert Harley
1998-10-16 4:00 ` Michal Jaegermann
1998-10-19 4:00 ` Robert Harley
1998-10-19 4:00 ` Michal Jaegermann
1998-10-16 4:00 ` Tan Wee Yeh
1998-10-16 4:00 ` Alex Rhomberg [this message]
1998-10-16 4:00 ` Joseph Sarkes
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox