Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
$ touch testfile
$ chmod g+w testfile
$ sudo adduser user2 user1
$ stat -c'%a %A' testfile
664 -rw-rw-r--
$ su user2
Password: 
$ groups
user2 user1
$ rm testfile 
rm: cannot remove `testfile': Permission denied

What is missing?

share|improve this question
add comment

2 Answers

up vote 11 down vote accepted

Deleting a file means you are making changes to the directory it resides in, not the file itself. Your group needs rw on the directory to be able to remove a file. The permissions on a file are only for making changes to the file itself.

This might come off as confusing at first until you think about how the filesystem works. A file is just an inode, and the directory refers to the inode. By removing it, you're just removing a reference to that file's inode in the directory. So you're changing the directory, not the file. You could have a hard link to that file in another directory, and you'd still be able to remove it from the first directory without actually changing the file itself, it would still exist in the other directory.

share|improve this answer
    
I get 775 drwxrwxr-x for the directory. –  Tshepang Feb 15 '11 at 18:12
1  
What's the group ownership of the directory? Remember, the second 7 you mentioned is the permission the directory's group has, not your user's group. –  jsbillings Feb 15 '11 at 18:39
1  
ls -ld /path/to/directory or simply ls -ld . if you've already cd'd to that directory. –  jsbillings Feb 15 '11 at 19:16
1  
for whatever reason, stat can't look up group ID 1002. stat looks up the group ID in /etc/group, NIS, LDAP, etc, and it's getting an error, that's why you're seeing UNKNOWN. I'd try running getent group 1002 to see if it gives you a more verbose error. –  jsbillings Feb 15 '11 at 20:06
1  
Actually, I don't find this behavior confusing at all. It is identical to how an actual, "real-life" directory works, which is why it's called "directory", and not, for example, "folder", which would behave quite differently. If I want to delete someone from my phone directory, I don't go to her house and kill her, I simply take a pen and strike through her number. IOW: I need write access to the directory, and no access to her. Windows has folders, Unix has directories, and both of them behave like their real-life counterparts. Confusion only happens if you mix them up. –  Jörg W Mittag Feb 16 '11 at 4:12
show 6 more comments

Only the system can delete a file, and only if it has no references. A mere user can only unlink a file, that is, remove it from a directory. You need write access to a directory to unlink a file from it. Unlinking a file doesn't modify the file, so write access to the file is irrelevant.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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