Permissions required to move file to different directory in Unix/Linux
Actually, moving a file is either a rename of a file on a single file system or creating a copy and deleting the original (typically only done if the move is a migration from one file system to another).
In either case you need execute and of course write permissions to the target directory and the source directory. However, for a mere rename (and moving from one directory to another can be just that) on a single file system you do not need any permissions on the file itself. It can be cleared of all permissions and still you can move (rename) it (as long as you have write and execute permissions for the directories).
For a real copy (as it is needed when you move the file to a different file system), you need read permissions on the file itself. No write permissions on the original are necessary, as deletion of a file is not writing to it (but to the directory it is in).
Related videos on Youtube
Andy
Updated on January 28, 2020Comments
-
Andy over 4 years
I would like clarification on the permissions required, in order to move a file A from directory B to directory C (the command would be "mv B/A C/A", I think), with name unchanged.
Am I correct to think that the following are required?
- The user/group doing the move must have write permission for directory B (or B must have permission flag set to allow all users/groups to write it)
- The user/group doing the move must have write permission for directory C (or C must have permission flag set to allow all users/groups to write it)
- The user/group doing the move must have write permission for file A (or A must have permission flag set to allow all users/groups to write it)
Thank you.
-
Wayne Conrad over 10 yearsThis is a good question, but I think it belongs on unix.stackexchange.com
-
Alfe over 10 yearsBut here we also have a tag
file-permissions
, so read it as "what conditions do I have to check before I let my script perform amv
of the specified kind" ;-) -
Andy over 10 yearsSorry I did not know there is another site for UNIX questions. I will bear this in mind for the future.
-
Andy over 10 yearsThank you. Just to make sure that I understood you correctly: to move the file (with/without renames) within the same file system, no permission needed on the file; to move the file (with/without renames) to a different file system, r permission needed on the file; in both cases, w and x permissions on the directories are required.
-
Alfe over 10 yearsYes, you summarized correctly. Just one thing: One one file system renaming and moving is exactly the same thing; "moving" is just renaming the directory-entry's full path (i. e. the part stating its parent directories) instead of just the directory-entry's base name (the part after the last slash). It is done via the system call
rename(2)
; tools likemv
test whether using this is possible and only if it is not (different file systems or unsupported by the file system) fall back to copy-and-delete. -
Alfe over 10 yearsAh, and another implicitness I forgot to mention: You also need to have execute permissions on all parent directories of the source and the target directory. Maybe you want to have a look at the system call
access(2)
which can be used for checks on such things. -
tommyTheHitMan over 8 years@Alfe can you explain why write / execute are needed on the source directory "for a real copy"? In other words, if I can read the target, can't I already "copy" it's contents into memory?
-
Alfe over 8 yearsExecute and write permissions are needed on the target directory for creating a new directory entry there. They are needed on the source directory for removing a directory entry there (the topic here is moving a file and on the directory level this is what moving means). For creating a copy alone, only read permissions on the source file and execute permissions on all its (parent) directories are needed. But read my answer carefully: moving a file on one file system does not necessarily require reading it because no copy is made.
-
Evan Benn almost 5 yearsHello, adding a comment about the case where A is a directory might be nice. 'w' permission is needed on A to update A/'..': stackoverflow.com/q/55133284/3936601
-
Alfe almost 5 yearsWhoa. The question luckily says explicit that A is a file. I say "luckily" because if A is a directory, that's a whole different case, so I would rather give a completely specialized answer for this case. The answer I gave here will probably a part of that other answer then. But in short: Moving a directory on the same file system is like moving a file on the same file system (and needs the same permissions). Moving a directory across file systems is creating it on the target, copying the complete contents (recursively) from the source to the target, then removing the original.
-
Alfe almost 5 years(continued) But this copying over can be rather complex, e. g. if the directory itself doesn't allow writing into it. Then you have to create it first with set write permissions and revoke these after you recreated everything which is inside it, etc. As I said, that will be a rather complex thing I won't put into my existing answer or into a comment.