12
4
I am using CentOS 5.5 and would like to move a large amount of folders within one volume, retaining their mtime
.
The best solution I could find is like this:
cp -p -r source/data target/
rm -rf source/data
With over 1TB of data on a NFS share, the copying takes forever. I do not want to copy. I want instantaneous move.
When I move a folder using mv source/data target/
, the mtime
of the folder (not the files) gets set to current time. This is because the contents of folder I am moving get modified by this operation (the ..
entry is pointing to a different inode).
I came up with a following shell script I called mv_preserve_mtime.sh
:
#!/bin/bash
# Moves source folder to target folder.
# You are responsible for making sure the target does not exist, otherwise this blows up
export timestamp=`stat -c %y $1`
mv "$1" "$2"
touch --date="${timestamp}" $2
Well, that did not work either. The folder's mtime
is restored, but all folders within the folder I move (only the ones 1 level deep) get their mtime
reset for reasons I do not understand.
Does anyone have a proper, efficient and correct solution?
I wonder why your attempt with
touch
didn't work. Is it themv
step or thetouch
step that changes the mtime of the subdirectories? What OS is on the NFS server, and (if you know) what filesystem type? – Gilles 'SO- stop being evil' – 2010-12-10T20:22:33.587@Gilles: I do not know why is it happening. It is the
mv
step that causes trouble. The NFS server is actually a NetApp storage, I know virtually nothing about its internals. – Roman Zenka – 2010-12-10T22:15:04.5071Thanks. I suspect it's a NetApp oddity. Otherwise
touch
should have worked. By the way a more portable way would betouch -r "$1" reference.tmp; mv -- "$1" "$2"; touch -r reference.tmp -- "$2"; rm reference.tmp
. – Gilles 'SO- stop being evil' – 2010-12-10T22:29:29.450@Gilles: Very interesting, did not realize
stat
was not portable. – Roman Zenka – 2010-12-11T17:09:36.530