3

I've got a directory and a file in it, with the directory marked as read-only:

$ mkdir directoryname
$ touch directoryname/filename
$ chmod a-w directoryname

I cannot delete the file, even if pass the -f flag to rm:

$ rm -f directoryname/filename
rm: cannot remove `directoryname/filename': Permission denied

Is there a way to force rm to delete this file? Obviously, I could temporarily give directoryname write permissions, but I'm looking for a more convenient way.

I believe the underlying unlink syscall fails in the same way. Is there a way to get around that?

Flimm
  • 4,218
  • 6
    If it where possible why would we need permissions? you are revoking write permission to the directory which implies delete, therefore you are not allowed to write/delete. You may be looking for a chmod 755 which will allow you to write/delete but won't allow your group nor others. – YoMismo Aug 01 '14 at 11:31
  • It's possible to delete read-only files with rm -f, but not files in read-only directories. I'm just looking for a convenient one-line command, I know why it fails by default. – Flimm Aug 01 '14 at 12:21

1 Answers1

4

What about:

sudo rm directory/filename

or:

su -c "rm directory/filename"

depending on your distro and/or setup.

You are giving yourself a temporary root for the duration of the above commands and as root is almighty on Unix/Linux you are allowed to do anything. This contrasts with MS Windows where you can remove access to the administrator account (although there are ways around that).

SELinux can help as can various extended attributes tools (such as chattr) but in the end, they can be bypassed as root can alter the extended attributes and can configure (and even disable) SELinux.

garethTheRed
  • 33,957