0

I have a directory in my home folder containing some build output, which the build process has chown to another user (for reasons unknown to me.) I want to delete the directory, but can't because it's not empty, and I can't delete the files it contains because they are not owned by me. Of course, I could recursively chown all the files, but getting all the hidden files is a pain. Is there a straight forward way?

  • If you can chmod, then chmod -R youruser DIR ought to do everything, including hidden files. – derobert Jun 22 '17 at 20:39
  • Do you mean chown? – Brent Jun 22 '17 at 20:43
  • Yes, sorry, I meant chown -R. Though actually you don't need to own files to delete them—you just need write permission on the parent directory. – derobert Jun 22 '17 at 20:45
2

If you have write access to the directory, you can remove the files in it, regardless of the files' owners. The sticky bit on the directory would prevent you from removing other users' files, but if you own the directory you can just unset the bit... Same for giving yourself write access.

However, a non-empty directory owned by some other user would be more of a problem.

In any case, if you can chown the files in the directory, you're probably superuser already, and should be able to just rm -r the whole tree. Though this is somewhat system specific, e.g. on Linux you could have the CAP_CHOWN capability allowing chown but not CAP_DAC_OVERRIDE which would allow bypassing the lack of write access.

-1

It's a workaround that doesn't immediately delete the files (so it won't help if you need the disk space right now), but: move the whole folder to /tmp and if /tmp is a tmpfs it will be gone after the next reboot.

  • 2
    if /tmp is a separate filesystem, mv will fail, because cross-filesystem move is copy & delete original. At least, it'll fail if rm would. – derobert Jun 22 '17 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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