7

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

  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
/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
...
2
  • 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. Aug 12, 2014 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, 2014 at 1:22

1 Answer 1

29

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.

3
  • 2
    Note: the second and third sentences of the second paragraph are key. Newcomers to *nix are frequently astonished and perplexed by the fact that they can rename and even delete files to which they don't have write access (or even to which they have no access); that all they need is write (and execute) access to the containing directory (or directories, if applicable, for mv). Aug 12, 2014 at 1:13
  • Thank you.. this helps clarify it and the analogy is excellent! Looking at the source directory, I have write and execute permissions.
    – Kent Pawar
    Aug 12, 2014 at 1:16
  • 1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .