Why rm gives warning when deleting a write protected file?

7,054

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.

Share:
7,054

Related videos on Youtube

LocalHost
Author by

LocalHost

Just a beginner in Linux...

Updated on September 18, 2022

Comments

  • LocalHost
    LocalHost over 1 year

    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 ?

    • Paulo Tomé
      Paulo Tomé over 4 years
      Suggestion to improve the questions' title: Why rm gives warning when deleting a read-only file?
  • LocalHost
    LocalHost over 4 years
    So it means deleting file is totally dependent on directory permissions only ?
  • mosvy
    mosvy over 4 years
    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.
  • LocalHost
    LocalHost over 4 years
    Can you explain more simply. I am not getting it properly.
  • mosvy
    mosvy over 4 years
    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.
  • Kamil Maciorowski
    Kamil Maciorowski over 4 years
    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.
  • mosvy
    mosvy over 4 years
    @KamilMaciorowski They better leave it to the real thieves to debate about stealing.
  • mosvy
    mosvy over 4 years
    @JdeBP It's in the 1st link. I don't feel like copy-pasting it here. And the rationale is not the post-hoc rationalization people are probably expecting.
  • Steven J Owens
    Steven J Owens over 2 years
    The linked rationale doesn't shed any more light on this that I can see. My guess is that while a user's authority is sufficient to override the chmod write-protection, rm double-checks with you before doing so (unless you use -f) as a precaution. Even though you have full authority to delete your own file even with write protection perms, it's taken as a sign that you might have meant to prevent yourself from doing so by accident.
  • mosvy
    mosvy over 2 years
    @StevenJOwens As I mentioned, it's a rationale, not a rationalization where you try to "explain" historical accidents as they were parts of a greater plan. As you do in your comment ;-)
  • Steven J Owens
    Steven J Owens over 2 years
    I'll rephrase to make my point more clear: Your answer is the technical version of: "Doctor, my arm hurts when I do this." "Well then don't do that!" Unfortunately, the linked standard, even the "rationale" section, also boils down to the same thing, i.e. "we do it this way because historically it's been done this way, but we don't know why."
  • Steven J Owens
    Steven J Owens over 2 years
    Also, my comment isn't a "rationalization", it's a "guess" :-).