4

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 ?

LocalHost
  • 469
  • 3
  • 10
  • 20

1 Answers1

7

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.

  • 2
    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. –  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](https://en.wikipedia.org/wiki/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. –  Jan 16 '20 at 14:15
  • 3
    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. – Kamil Maciorowski Jan 16 '20 at 14:21
  • @KamilMaciorowski They better leave it to the real thieves to debate about stealing. –  Jan 16 '20 at 14:39
  • Or you can read the actual rationale. _This_ standard _has_ a rationale. – JdeBP Jan 16 '20 at 15:05
  • @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. –  Jan 16 '20 at 15:11
  • 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. – Steven J Owens Sep 30 '21 at 06:12
  • @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 ;-) –  Oct 16 '21 at 09:03
  • 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 Oct 18 '21 at 16:45
  • Also, my comment isn't a "rationalization", it's a "guess" :-). – Steven J Owens Oct 18 '21 at 16:46