What Unix file permissions are required for mv path/to/src/file path/to/dest
?
Neither I nor ChatGPT nor any of the dozen programmers I asked could give a completely correct answer off the top of their heads.
If you'd like, take a moment to think about it for yourself. Otherwise, scroll down to see the answer.
+w
on src
and dest
+x
on every directory named in path/to/src
and path/to/dest
, including the implicit current directory for relative pathsfile
is a directory, +w
on file
src
has the sticky bit set, then you must be either the owner of file
or the owner of src
src
and dest
are on different filesystems, then +r
on file
Commentary:
f
itself, but – aside from the edge cases #3 and #5 – you do not, because renaming a file does not require either reading or altering its contents, only the contents of the source and target directories (hence #1).src
and dest
but also all the other directories in the path...
entry that points to its parent, which must be updated if a directory is moved./tmp
. The sticky bit is intended for shared directories so that a user cannot interfere with others' files – hence the ownership permissions required./tmp
, which is typically mounted as its own filesystem. The rename
syscall doesn't work across filesystems, so the mv
command emulates it by reading the source file, unlinking it from the source directory, and recreating it in the target directory. Because it has to read the contents of the file, +r
permissions are necessary.man 2 rename
covers the first three requirements under EACCES
in the "Errors" section.man 7 path_resolution
describes the Linux path resolution process./tmp
" covers the meaning of the sticky bit.rename
, respectively. ∎