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



$ 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

  1. How can the mv command move the file and preserve mode, ownership, timestamps while we cannot do the same with cp ...?
  2. 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... where mv 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
$ 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
  • 1
    About two thirds of the way down your first block you say “Permissions were not preserved.” That’s an ambiguous statement. The file mode (commonly called the permission bits) was preserved. True, the ownership is different, so the new file has different access than the original. – G-Man Says 'Reinstate Monica' Aug 12 '14 at 1:16
  • @G-Man - Oh yes, I see that now. Let me update the comment/s and specify the changes to ownership or file permissions. – Kent Pawar Aug 12 '14 at 1:22

mv does not make a copy of the file and remove the original, unless you're moving the file between different filesystems. mv moves the file.

In order to move a file, you need to have permission to detach it from the directory where it was before, and to attach it to the directory where you're putting it. In other words, you need write (and execute) permission to both the source directory and the destination directory. You do not need to have any particular permission on the file, since you aren't modifying or accessing the file itself. (Analogy: you can move a locked box around even if you don't have the key do open the box.)

cp -p can and does preserve the permissions of the file, but it cannot preserve the file's ownership in general. Since you aren't running cp as root, it cannot create files that don't belong to you, or that don't belong to a group that you belong to.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.