Kindly Consider:
$ id # Me
uid=100(user1) gid=200(group1) groups=200(group1)
$ ls -l tnsnames.ora # So user1 has only read permission on below file.
-rw-rw-r-- 1 oracle dba 411024 Jul 28 2010 tnsnames.ora
$ cp tnsnames.ora tnsnames.ora_bak
$ cat tnsnames.ora_bak > tnsnames.ora # No write access
-bash: tnsnames.ora: Permission denied
$ cp -p tnsnames.ora tnsnames.ora_copy # Copy file and preserve mode, ownership, timestamps. Requires sudo to succeed completely.
$ ls -l tnsnames.ora_copy # Mode and timestamps preserved; ownership not preserved.
-rw-rw-r-- 1 user1 group1 411024 Jul 28 2010 tnsnames.ora_copy
$ mv tnsnames.ora tnsnames.ora_move
$ ls -l tnsnames.ora_move # Move preserves mode, ownership, timestamps.
-rw-rw-r-- 1 oracle dba 411024 Jul 28 2010 tnsnames.ora_move
$
Update
$ ls -l # As pointed out by @Gilles, the source (which is also the destination here) directory has r-x mode which allows mv to work.
drwxrwxrwx 3 oracle dba 4096 Aug 11 20:38 oracle
- How can the
mv
command move the file and preserve mode, ownership, timestamps while we cannot do the same withcp
...? - Also if you do not have write access to a file, how is it possible to change the location (metadata) of the file using
mv
; or we can look at it the other way... wheremv
will first make a copy of the file, then how does it get the rights to set the owner/group on the copied file and delete the source file it does not have write permissions on..
OS/Bash/Utility Version information:
$ echo $SHELL
/bin/bash
$ bash --version
GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
...
$ uname -a
Linux server1 2.6.18-371.8.1.el5 #1 SMP Fri Mar 28 05:53:58 EDT 2014 x86_64 x86_64 x86_64 GNU/Linux
$ cp --version
cp (GNU coreutils) 5.97
...
$ mv --version
mv (GNU coreutils) 5.97
...