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.

PS/Warning: Yes, the description in the passage above is factually wrong. Don't tell me that or "correct" the post. I just quoted it here to illustrate how much half-knowledge about this issue is floating around.

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

2 Answers 2

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

0

Might it be because moving the directory into target directory changes the .. entry inside directory and since you're not allowed to write in directory, it is not permitted?

I experienced the same (and was puzzled similarly as OP) with a directory that was mine, but the owner-write permission wasn't set. Simply setting chmod u+w on the directory made me allow the move (on the same partition, no copying and deleting). After reading this thread, it made me realize the above situation. It fully explains why with files there is no such problem.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .