How do I remove someone else's subdirectory from under my directory?

3

1

I was sharing files with a coworker on a Linux server, and he added some new files. Now we've moved the files somewhere else, so I'd like to remove the old files. However, when my coworker added his files, the software we used made new subdirectories owned by his account, and I can't find quite the right command to remove them.

$ ls -a
total 20
drwxrwxr-x 5 markpasc markpasc 4096 Sep 20 09:48 ./
drwxrwxr-x 3 markpasc markpasc 4096 Sep 20 09:48 ../
drwxr-xr-x 2 coworker coworker 4096 Sep 16 14:07 82/
drwxr-xr-x 2 coworker coworker 4096 Sep 16 14:07 c4/

I tried:

$ rm -rf 82
rm: cannot remove `82/b7fc78bc548537f3ea235026b7322fe3bea91f': Permission denied
$ rm -f 82/b7fc78bc548537f3ea235026b7322fe3bea91f
rm: cannot remove `82/b7fc78bc548537f3ea235026b7322fe3bea91f': Permission denied
$ rmdir 82/
rmdir: 82/: Directory not empty
$

I know that normally I can remove a file someone else owns from my directory:

$ ls -a
total 8
drwxrwxr-x  2 markpasc markpasc 4096 Sep 20 10:13 ./
drwxr-xr-x 24 markpasc markpasc 4096 Sep 20 10:13 ../
-rw-rw-r--  1 someuser someuser    0 Sep 20 10:13 file
$ rm file
rm: remove write-protected regular empty file `file'? y
$ ls file
ls: file: No such file or directory
$

Is there a way to remove these directories myself, or does my coworker (or root) have to remove them?

markpasc

Posted 2010-09-20T17:19:04.333

Reputation: 133

1you probably should ask the root – Nathan Fellman – 2010-09-20T17:31:03.327

Thanks! I asked my coworker and he helped me remove them, but I wondered if there was some other way I missed. – markpasc – 2010-09-20T17:48:43.057

No, only root may overcome account limitations. – harrymc – 2010-09-20T18:11:29.793

Answers

4

To remove a directory, you need two things: write access to the parent directory, and for that directory to be empty. So your coworker, or root, has to cooperate. This is a limitation of the unix permission model.

For better workflow when a directory tree is writable by more than one user, you can use access control lists (if supported) or setgid directories.

  • With ACLs, give write access to the directory to whoever should have it, and set the default ACL to also give write access, so that newly created files and subdirectories will inherit the desired permissions. Users can override those defaults, but as long as they cooperate passively, the permissions will be fine.

    setfacl -m user:coworker:rwx .
    setfacl -d -m user:coworker:rwx .
    
  • If you don't have ACLs, you can give write access to a group, and set the setgid bit on the directory. This causes newly created files and subdirectories to belong to the group in question, rather than the default group of the creating process. However it's still up to the users to give group write permission to the new files and directories (this happens automatically only if they have umask 002).

    chgrp mygroup .
    chmod g+ws .
    

You might be interested in this thread about why things work that way.

Gilles 'SO- stop being evil'

Posted 2010-09-20T17:19:04.333

Reputation: 58 319