Move preserves ownership of source files not allowing writes
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.
Related videos on Youtube
Comments
-
Kent Pawar almost 2 years
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 ...
-
Admin almost 10 yearsAbout 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.
-
Admin almost 10 years@G-Man - Oh yes, I see that now. Let me update the comment/s and specify the changes to ownership or file permissions.
- How can the
-
G-Man Says 'Reinstate Monica' almost 10 yearsNote: 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
). -
Kent Pawar almost 10 yearsThank you.. this helps clarify it and the analogy is excellent! Looking at the source directory, I have write and execute permissions.
-
G-Man Says 'Reinstate Monica' almost 10 years