I have a situation where I'm testing a new filesystem that has an issue with some metadata. All of the files are intact, however I ultimately have to effectively move every single file off to another volume and then immediately back in place.
Obviously mv
won't do the trick since it's not able to preserve all attributes (particularly all timestamps). I was thinking something more along the lines of a find command whilst exec'ing cp -p /original/path/to/file /tmp/location/file
, rm /original/path/to/file
, cp -p /tmp/location/file /original/path/to/file
, rm /tmp/location/file
. Perhaps all of these commands in a script that is passed to the find exec?
I'm not sure of the most efficient approach here, but would like some quick input in order to make sure I cover all of my bases and have a precise command that will not leave me mourning some kind of data attribute loss when it's all said and done. I have many hundreds of gigabytes to shift back and forth so I need to be both as careful and as efficient as possible.
Here's the solution I've come up with so far and would love some input:
Shell script safe_move.sh
:
#!/usr/bin/env bash
SRC_FILE_AND_PATH="$1"
SRC_BASENAME=`basename "$1"`
DESTINATION_PATH="/mnt/tmp"
cp -a ${SRC_FILE_AND_PATH} ${DESTINATION_PATH}/${SRC_BASENAME}
rm -f ${SRC_FILE_AND_PATH}
cp -a ${DESTINATION_PATH}/${SRC_BASENAME} ${SRC_FILE_AND_PATH}
rm -f ${DESTINATION_PATH}/${SRC_BASENAME}
And this will be called with:
find /path/to/move -type f -exec safe_move.sh {} \;
Any thoughts or corrections?
- I did find a problem where this will ALTER folder dates due to modification of the folder's children... hrm. Not sure how to overcome this one yet.
cp -a
with find is appearing more and more to be the likely answer in this scenario, however I have seen some valid points that perhaps tar may handle some scenarios as good or better than cp.