Filip Kocina wrote: > I'd like to ask you why the owner and the group of a file is retained > while moving a file via mv.
Moving a file on the same filesystem does not copy the file. Moving a file from one directory to another on the same filesystem simply creates a new inode pointer in the new directory and deletes the inode pointer from the old. The data in the file itself is unchanged. Effectively the file did not move at all but only the directory entries pointing to it. It is only the directory that changes. dir1 --> file1 dir2 dir1 file1 <-- dir2 Moving a file from one filesystem to another does necessarily mean copying the file. In that case it is similar to a cp then rm. The permissions and ownership depend upon the priviledge of the user process doing the copy and remove. Root is the superuser and has full permission and the file will be as similar as possible to the previous move on the same filesystem. But non-root will be left with owning the file. Also think about the case where a file has many hardlinks to it. There is exactly one file and one inode and the perm:owner:group is stored in the inode. Moving a file on the same filesystem does not change this but just creates a new hardlink and deletes the old. > I was surprised when I made a file as root in home directory of > another user The root user is the superuser and has the power to do this. > and moved this file as this another user into /tmp. I mean the > preservation is rather logical, but if this another user renamed > this file to a not very smooth-tongued name, the user who owns this > file could be compromised. You did not explain the problem with a "not very smooth-tongued name, the user who owns this file could be compromised" and therefore I do not understand the problem you are worried about. As you might imagine this policy has been around for many decades and if there were a security problem with it then it would have been fixed years ago. Therefore it probably doesn't have a problem. With great power comes great responsibility. The root superuser must be careful. Root does have the power to create problems. It needs this in order to solve problems. There is no such problem for a non-root normal user. > I know there is an easy solution: not to move file into a directory > of another user, but still, this behavior of mv is little weird for > me. You may feel it is weird but it is the correct behavior. Also see this FAQ for related information. http://www.gnu.org/software/coreutils/faq/#Why-can-only-root-chown-files_003f Bob
