I have a bunch of historical files that need to be modified, as the file source announced there is an error in them which should be corrected.
I am planning to use sed to do the mass update, but I would like to know if there is a way to preserve files last modified date/time, so later ls -ltr lists will show me when files were obtained, not when I updated them.
Any idea?
One way:
filetime()
{
perl -e '
$mtime = (stat("$ARGV[0]"))[9];
# time structure into variables
($sec,$min,$hr,$day,$mon,$yr,$wday,@dntcare) = localtime($mtime);
$yr = ($yr>=70) ? $yr+1900 : $yr+2000;
printf ("%d%02d%02d%02d%02d", $yr,$mon,$day,$hr,$min); ' "$1"
}
for fname in *.dat
do
# get file time in YYYYMMddhhmm format
oldtime=$(filetime $fname)
ls -l $fname
# sed your file here
# sed 's/ / /g' $fname > tmp; mv tmp $fname
# put filetime back to where it was
touch -t $oldtime $fname
ls -l $fname
done
Purfect, thanks a million!