Jay Taylor's notes
back to listing indexlinux - How to save and restore file's created/modified dates? - Super User
[web search]I've copied a bunch of files from one server to the other, and now the files' dates are reset to current.
How to backup files' dates on old server and restore the them on the new one (without re-transferring all files)?
cp
(or scp
) has the -p
or the --preserve
option, you should have used it! Don't forget it next time...
– gniourf_gniourf
Dec 2 '12 at 21:31
Here are scripts to save and restore all {c,n,a}times of files and directories:
Save
find / -mount -print0 | perl -ne 'INIT{ $/ = "\0"; use File::stat;} chomp; my $s = stat($_); next unless $s; print $s->ctime . "/" . $s->mtime . "/" . $s->atime ."/$_\0"; ' > dates.dat
Restore: one-liner
cat dates.dat | perl -ne 'INIT{ $/ = "\0";} chomp; m!^([0-9]+)/([0-9]+)/([0-9]+)/(.*)!s or next; my ($ct, $mt, $at, $f) = ($1, $2, $3, $4); utime $at, $mt, $f;'
One important aspect to note is this solution doesn't save or restore the ctime (or "change time") inode attribute. The ctime inode attribute tracks the last time attributes on the file, such as permissions, were modified.
Expanded, readable form
For those interested in inspecting or learning the details of how this works.
Save
find / -mount -print0 \
| perl -ne '
INIT{ $/ = "\0"; use File::stat;}
chomp; my $s = stat($_);
next unless $s;
print $s->ctime . "/" . $s->mtime . "/" . $s->atime ."/$_\0";' \
> dates.dat
Restore
cat dates.dat \
| perl -ne '
INIT{ $/ = "\0";}
chomp;
m!^([0-9]+)/([0-9]+)/([0-9]+)/(.*)!s or next;
my ($ct, $mt, $at, $f) = ($1, $2, $3, $4);
utime $at, $mt, $f;'
I have a Python script for doing this at https://github.com/robertknight/mandrawer/blob/master/save-file-attrs.py
On the original server run:
save-file-attrs.py save
scp .saved-file-attrs <user>@<dest-server>:<path>
On the destination server run:
cd <path>
save-file-attrs.py restore
This will restore the file attributes.
You can use stat
to get the dates on the source and touch
to modify them on the target.
If file names are not too weird, and I only need to restore mtime, I use this quick & dirty solution:
find . -type f -exec stat -c 'touch --no-create -d "%y" "%n"' {} \;
This creates a script on the source, and that script can be run on the destination to restore the mtime timestamps.
find . -type f -printf 'touch --no-create -d "%t" "%p"\n'
because it doesn't fork. But it still needs some improvement (stable time format)
– Daniel Alder
Jul 9 '14 at 19:51
find . -type f -exec stat -c 'touch --no-create -d @%Y "%n"' {} \; >restore-timestamps
(timestamps instead of local date representation).
– rdesgroppes
Aug 23 '19 at 13:48
Your Answer
Not the answer you're looking for? Browse other questions tagged linux filesystems date-modified or ask your own question.
-
21
-
38
Related
Hot Network Questions
-
Aligning Equations with Cases
-
Why does the closest approach of star S2 to Sgr A* not appear to be near the focus of its elliptical orbit?
-
Is English simplifying its phonology? Or are there examples of new linguistic distinctions being created?
-
First digit of 3^2020
-
replace ground almonds with flour?
-
Why not asking about updates of search process
-
Chic Le (Freak - Freak Out!)
-
What exactly happens when power is reduced in trimmed straight and level flight
-
Did US prohibit the export of the mere description of a cryptographic algorithm?
-
One word translation for spiraling or ballooning
-
What happens if a player asks to roll an Ability Check?
-
Breath Marks (different symbols)
-
Why does a rubber band become a lighter color when stretched?
-
Understanding definition of NP and co-NP
-
what is this weird mechanical thing I found in the woods?
-
How to represent null or dashes in a cost matrix or incidence matrix in cplex?
-
Lowering a Character Level
-
Does the "Avoid using floating-point" rule of thumb apply to a microcontroller with a floating point unit (FPU)?
-
Different Slur/Tie interpretations
-
Why does the WHO seem to support allowing Chinese “wet markets” to reopen?
-
Why is untreated trypanosomiasis invariably fatal in humans?
-
Employer asking for lengthy handover which was already done
-
Should I convert my family pictures from JPEG to PNG?
-
What's the mechanism to maintain this shelf stable
site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2020.4.17.36630