20
$ 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?

3 Answers 3

34

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.

11
  • I get 775 drwxrwxr-x for the directory.
    – tshepang
    Commented Feb 15, 2011 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
    Commented Feb 15, 2011 at 18:39
  • 1
    ls -ld /path/to/directory or simply ls -ld . if you've already cd'd to that directory.
    – jsbillings
    Commented Feb 15, 2011 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
    Commented Feb 15, 2011 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. Commented Feb 16, 2011 at 4:12
1

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.

0

It seems like in order to delete files from the directory you need user write permissions. Group write and all write are not enough, despite what the accepted answer says.

I tested this on two machines on two filesystems:

$ mkdir testdir
$ touch testdir/foo
$ chmod u-w testdir
$ chmod o+w testdir
$ ls -ld testdir
dr-xr-xrwx 2 tim all 17 Nov 25 23:08 testdir
$ rm testdir/foo
rm: cannot remove 'testdir/foo': Permission denied
$ chmod g+w testdir
$ rm testdir/foo
rm: cannot remove 'testdir/foo': Permission denied
$ chmod u+w testdir
$ rm testdir/foo

You must log in to answer this question.

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