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 Answer
Because the standard requires it:
3. If file is not of type directory, the
-foption is not specified, and either the permissions of file do not permit writing and the standard input is a terminal or the-ioption is specified,rmshall 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.
-
2So it means deleting file is totally dependent on directory permissions only ?LocalHost– LocalHost2020-01-16 14:03:17 +00:00Commented Jan 16, 2020 at 14:03
-
See the edit -- it's just something specific to the
rmutility. Theunlink(2)system callrmis 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.user313992– user3139922020-01-16 14:06:53 +00:00Commented Jan 16, 2020 at 14:06 -
Can you explain more simply. I am not getting it properly.LocalHost– LocalHost2020-01-16 14:10:28 +00:00Commented Jan 16, 2020 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/tmpdirectory. It's an exception where the actual permissions of the file also matter, not just those of the directory.user313992– user3139922020-01-16 14:15:58 +00:00Commented Jan 16, 2020 at 14:15 -
3I 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– Kamil Maciorowski2020-01-16 14:21:45 +00:00Commented Jan 16, 2020 at 14:21
Why rm gives warning when deleting a read-only file?