Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
you probably should ask the root – Nathan Fellman Sep 20 '10 at 17:31
Thanks! I asked my coworker and he helped me remove them, but I wondered if there was some other way I missed. – markpasc Sep 20 '10 at 17:48
No, only root may overcome account limitations. – harrymc Sep 20 '10 at 18:11
up vote 2 down vote accepted

To remove an empty 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.

share|improve this answer
Awesome answer. Thanks! – markpasc Oct 5 '10 at 0:23

You must log in to answer this question.

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