3

I have a regular file and I changed its permission to 444. I understand that as the file is write protected, we can't modify or remove the contents of file but when I try to remove this file using rm, it generates a warning stating whether I want to remove a write protected file or not. My doubt is that isn't that depends on the directory permissions that whether a file can be deleted or not ? Why rm is generating a warning even when directory is having a write and execute permission. Does it also depends on the file permission whether a file can be deleted or not ? or is it totally dependent on directory permissions only ?

1
  • Suggestion to improve the questions' title: Why rm gives warning when deleting a read-only file? Jan 16 '20 at 15:00
6

Because the standard requires it:

3. If file is not of type directory, the -f option is not specified, and either the permissions of file do not permit writing and the standard input is a terminal or the -i option is specified, rm shall write a prompt to the standard error and read a line from the standard input. If the response is not affirmative, rm shall do nothing more with the current file and go on to any remaining files.

So a) this is a matter specific to the rm utility (it doesn't say anything about how permissions work in general) and b) you can override it with either rm -f file or true | rm file

Also, this was rm's behaviour since quite a long time -- 46 years, or maybe even longer.

8
  • So it means deleting file is totally dependent on directory permissions only ?
    – LocalHost
    Jan 16 '20 at 14:03
  • See the edit -- it's just something specific to the rm utility. The unlink(2) system call rm is using to remove a file does only care about the permission of the containing directory, with some exceptions like when the directory has its sticky bit set.
    – mosvy
    Jan 16 '20 at 14:06
  • Can you explain more simply. I am not getting it properly.
    – LocalHost
    Jan 16 '20 at 14:10
  • What exactly do you have trouble with? If user "joe" creates a file inside /tmp (a directory with its sticky bit set), the user "jack" won't be able to delete it, despite having read, write and execute permission to the /tmp directory. It's an exception where the actual permissions of the file also matter, not just those of the directory.
    – mosvy
    Jan 16 '20 at 14:15
  • 2
    I love (some) standards. But when a question is "why?", any answer that refers solely to an arbitrary set of rules seems incomplete. Example: 'Why shalt thou not steal?' – 'Because of one of the Ten Commandments. Case closed.' But there is usually a rationale behind any rule, e.g. philosophers can debate over stealing. In this case I guess the rationale is: since the file is read-only, the owner probably does not want to lose any bit of data stored within, so it's better to ask before we remove the file as a whole. Jan 16 '20 at 14:21

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.