From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111c93,a4058c37c8deaaa3 X-Google-Attributes: gid111c93,public From: Alex Rhomberg Subject: Re: How can I change year 2018 to 1998 Date: 1998/10/16 Message-ID: <36272192.D3D26328@ife.ee.ethz.ch>#1/1 X-Deja-AN: 401723704 References: <6vvjk0$t70$1@news.kjist.ac.kr> <706ugu$1sg23@id4.nus.edu.sg> To: Tan Wee Yeh , "G. Hugh SONG" Content-Type: multipart/mixed; boundary="------------0B5784ED250D8D654A7A57E9" X-Trace: 16 Oct 1998 12:37:14 +0100, 129.132.24.209 Organization: Electronics Lab, ETH Zurich, Switzerland MIME-Version: 1.0 Newsgroups: comp.os.linux.alpha This is a multi-part message in MIME format. --------------0B5784ED250D8D654A7A57E9 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Tan Wee Yeh wrote: > > Robert Harley 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 | \_________________________________________________________________/ --------------0B5784ED250D8D654A7A57E9 Content-Type: text/plain; charset=us-ascii; name="chyear" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="chyear" #!/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; } } --------------0B5784ED250D8D654A7A57E9--