On Thu, 2 Dec 2010, Brad wrote:

> On Tue Nov 16, 2010 at 10:22:13AM -0600, gregwm wrote:
>
>> can anyone show me a bash test to put before mv that will tell whether 
>> mv will do a simple rename or a copy and delete?  for example for mv 
>> into a --bind mount, df reports both locations as within the same fs, 
>> yet mv will copy and delete.  the best test I can think of would create 
>> a file, mv and see what happens.  can anyone concoct an accurate test 
>> that doesn't need to create a test file?  (or perhaps reveal an mv 
>> alternative..)
>
> you can use "stat -c %d FILENAME" -- compare for both files. i just 
> tested and it works for me showing the real device under a bind mount.


Suppose you are moving FILENAME to DIR.  I think you want to do both:

stat -c %d FILENAME
stat -c %d DIR/

Note the slash at the end of DIR/.  If DIR is a symlink to a mounted 
device then these will give different answers and you only want the second 
of the two answers:

stat -c %d DIR
stat -c %d DIR/

These give the same answer:

stat -c %d DIR/
stat -c %d DIR//

Thus, you don't have to worry about using ${DIR}/ in a script where ${DIR} 
might already include a slash at the end.

So I think this is the kind of thing you want to do:

FILENAME="$1"
DIRNAME="$2"

if [ $(stat -c %d "$FILENAME") == $(stat -c %d "${DIRNAME}/") ] ; then
    echo RENAME
else
    echo COPY/DELETE
fi


Of course you probably want to do something other than echo those words, 
but you get the idea.  If you come up with something useful, I hope you'll 
share it here.

Best,
Mike