I want to clarify a few things I noticed regarding the file handling in linux (Ubuntu basically but I think this is applicable to every distribution).
I noticed that when files are created by root in home directory they are considered write-protected regular files and a message is displayed when trying to remove them the usual way:
$ sudo touch ~/test.txt
$ ls -l ~/test.txt
-rw-r--r-- 1 root root 0 Nov 24 10:27 /home/xxx/test.txt
$ rm ~/test.txt
rm: remove write-protected regular empty file '/home/gorfanidis/test.txt'? y
$ ls -l ~/test.txt
ls: cannot access '/home/gorfanidis/test.txt': No such file or directory
If I do not confirm with y
the file is not deleted also (I know I can use -f
for example to skip this message but I am not asking for this).
Anyway, I tried the same in another place where the folder owner is root (I guess this is the difference but not sure) as in /usr/
$ sudo touch /usr/test.txt
$ rm /usr/test.txt
rm: remove write-protected regular empty file '/usr/test.txt'? y
rm: cannot remove '/usr/test.txt': Permission denied
So, my questions are the following:
- What does exactly write-protected regular file mean? What file is non-regular for example? It's not clear to me. Write-protected in this case means
root
owner (privileges) as stated here for example. - What's the point of asking for permission for removing write-protected file when you cannot actually remove it (case of root file in root folder)?
- What's the mechanism for the linux system to decide if a user has actually privileges to remove a file: is it the folder permissions the file resides in? Is it something more elaborated?
cd <dir>; rm <file>
what you are actually doing is modifying the file<dir>
(yes, directories are just files... everything is a file in UNIX systems) by removing the link to<file>
. – Bakuriu Nov 24 '22 at 19:57