1

I'm a bit puzzled about the following behavior (Linux, Debian 8.3, ext2 filesystem)

test@linux:~$ ls -ld directory/ file
drwxr-xr-x 2 root root 4096 Mar 10 18:51 directory/
-rw-r--r-- 1 root root    8 Mar 10 18:51 file
test@linux:~$ ls -ld target_directory/
drwxr-xr-x 2 test test 4096 Mar 10 18:47 target_directory/
test@linux:~$ mv file target_directory/
test@linux:~$ mv directory/ target_directory/
mv: cannot move ‘directory/’ to ‘target_directory/directory’: Permission denied

And that's even the case if directory/ is empty. Funny: rmdir directory/ works in that case.
Why? It would be nice to know why mv behaves so differently for a file and a directory, that both don't have write permissions.


Actually in the vein of "give a man a fish...", don't hesitate to point me to a book which explains this and the permissions system correctly because (mini-rant) I constantly see stuff like that (from a well-known Linux book):

$ ls -ld ch3 test
-rw-rw-r-- 1 joe sales 4983 Jan 18 22:13 ch3
drwxr-xr-x 2 joe sales 1024 Jan 24 13:47 test

The first line shows that the ch3 file has read and write permission for the owner and the group. All other users have read permission, which means they can view the file but cannot change its contents or remove it.

  • I suggest that you review how permissions work with directories as they act quite different than files. – mdpc Mar 10 '16 at 19:28
  • @mdpc: 'reviewing' ... I'm still learning. Any suggestion where I can read about it? – wolf-revo-cats Mar 10 '16 at 19:38
1

Removing a directory or a file has only to do with permissions on the directory which contains the file/dir to be removed. Permissions on the file/dir itself are irrelevant. Hence why you can remove directory using rmdir even though it's owned by root: you didn't show it in your output, but you clearly have read, write and execute permissions on the directory which contains directory.

I've written more elsewhere on what exactly each individual permission on a directory lets you accomplish.

As for why you can't move directory into target_directory, I'm not sure. I'm unable to reproduce this on my box. There are a host of reasons that could be causing this, ranging from extended attributes being set on directory, to the possibility that target_directory could be a mount point for a separate filesystem than that which contains directory. Permissions alone aren't the reason, however.

(P.S.: Next time pick better example names; using the names "directory" and "file" make it really annoying to try to describe what's happening.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.