21

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?

  1. 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)
  2. 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)
  3. 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.

Niko
  • 574
  • 5
  • 13
Andy
  • 2,770
  • 9
  • 35
  • 42
  • 7
    This is a good question, but I think it belongs on http://unix.stackexchange.com/ – Wayne Conrad Jan 06 '14 at 11:40
  • 1
    But 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 a `mv` of the specified kind" ;-) – Alfe Jan 06 '14 at 11:42
  • 1
    Sorry I did not know there is another site for UNIX questions. I will bear this in mind for the future. – Andy Jan 06 '14 at 17:02

1 Answers1

36

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).

Alfe
  • 56,346
  • 20
  • 107
  • 159
  • 3
    Thank 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. – Andy Jan 06 '14 at 12:25
  • 1
    Yes, 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 like `mv` 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 Jan 06 '14 at 14:08
  • 4
    Ah, 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. – Alfe Jan 06 '14 at 14:10
  • @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? – tommyTheHitMan Jan 28 '16 at 19:26
  • 3
    Execute 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. – Alfe Jan 29 '16 at 00:38
  • Hello, adding a comment about the case where A is a directory might be nice. 'w' permission is needed on A to update A/'..': https://stackoverflow.com/q/55133284/3936601 – Evan Benn Jul 18 '19 at 07:53
  • Whoa. 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 Aug 08 '19 at 15:26
  • (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. – Alfe Aug 08 '19 at 15:29