How to mv a folder in Linux retaining its mtime?
POSIX mv
doesn't provide any option to ask for atime/mtime preservation,
but as the operation is local to a same volume, you can ask cp
to use hard-links
instead of copying data of the regular files using the -l
option:
cp -p -r -l source/date target/
rm -rf source/data
Since only directories and file references will be actually copied, it should go much faster:
For more informations on hard-links, you can consult the corresponding Wikipedia page
As for why subdirectories mtime is being reset with your current solution, it's because you only get and restore the parent directory mtime : touch is not a recursive command.
Another solution may be:
rsync -a --remove-source-files source/data target/