0

Sorry if this question was asked already, I just could not find the answer. If I do:

touch file.txt
chomd 444 file.txt

in principle, everyone (except ROOT) can only read the file. However if I do:

rm file.txt

I still can remove the file, despite I am shown:

rm: remove write-protected regular empty file 'file.txt'? y

even if it is protected. Is there a way to really protect this file? I see that one can chattr +i file.txt but that only works if you can sudo; I am not an administrator (as 99% of the users out there).

Cheers.

John
  • 3
  • You can't do this by design. You can't even change the owner of the file to someone else, while you still own it. Having a way to make files you can't delete can fill your quota or the whole system with no way back in barring root coming to the rescue. – SHawarden Jan 16 '23 at 08:15
  • Re. others being able to delete it, see Do Linux directory permissions overrule file permissions? and links therein. As for protecting the file or directory from yourself, you can't, really. If you can change the permissions to remove access, you can change them back. – ilkkachu Jan 16 '23 at 08:18
  • @SHawarden @ilkkachu My problem is that I wanted to be able to remove any easy way to mess things up. If I do chmod 444 and then I try to remove, the only thing preventing me from the deletion is that line asking me if I really want to delete. If I am tired, I might just type y and enter.

    If I use ranger-fm, no such a comment appears and It just goes ahead with the deletion. Nowadays I mostly use ranger-fm.

    From the accepted answer, the way to do this is to protect the whole directory containing the files with chmod 555, at least that works for me.

    – John Jan 16 '23 at 08:33

1 Answers1

0

In Unix filesystems you don't really remove file, you "unlink" it: the file data are in an "inode" and directories have a reference to that inode (which is why you can have "hard links": these are multiple references to the same inode in multiple directories). When the last reference disappears, the inode is destroyed and the space recovered.

rm is just removing the reference in the parent directory and implies write privileges on that parent directory and not on the file. So, to protect the file reference you remove write privileges on the directory.

xenoid
  • 8,888
  • Perfect! Thanks, for other's reference, I am using chmod 555 directory to remove write permissions from everyone. Read and execute are allowed for everyone. – John Jan 16 '23 at 08:26