Reputation: 875
I want to preserve the timestamp of the files I'm editing in a for loop
for files in $DIR/MDC*/$FILE
do
# Need to get date here!
dos2unix $files
if grep -q $TYPE $files; then
echo 'done'
else
sed -i "1s/^/$TYPE\n/" $files
fi
$DIR/crccalc/LDDR16.py $files
# Use Date variable here to change it back
done
The issue is I need to get a formatted date string from the file so that I can do touch -r
to revert the files date back once the loop has completed.
stat
is not giving the me the format I need.
Required Format:
YYMMDDhhmm
Upvotes: 1
Views: 1875
Reputation: 111
Another attempt that can be run straight from the terminal.
FIRST do step one, then start working with your files.
Preserve old timestamps.
It operates from the current directory, excludes all hidden files, and saves it to the temporary file in /tmp/files
. You can always change parameters, but better stay with -printf '"%t" "%p"\n'
since the later touch
command utilizes that.
find . ! -iname ".*" -printf '"%t" "%p"\n' -type f > /tmp/files
Modify your files as much as you like
now create a file that helps you restoring the timestamps:
while read line; do echo touch -a -d $line >> job.sh; done < /tmp/times
And finally apply the old dates to the modified files
sh job.sh
Caveat: works for files with name spacing, special characters, but for instance no files with a $
sign and ones with a double space in it.
Upvotes: 1
Reputation: 289555
There is a nice trick for this: using touch -r reference_file
. That is, touch the file using the timestamp of another file as a reference.
From man touch
:
-r, --reference=FILE
use this file's times instead of current time
And you may be asking: and how can this help you? Well, because you can create a dummy file dummy
to work with:
dummy
with the timestamp of the file original_file
you want to modify.original_file
.original_file
using the timestamp of the dummy
one.All together:
for files in $DIR/MDC*/$FILE
do
# copy its timestamp to `dummy_file`
touch -r "$files" "dummy_file"
# ...things...
# Use Date variable here to change it back
touch -r "dummy_file" "$files"
done
Upvotes: 6