In this article, we are going to see How to Make File and Directory Undeletable, Even By Root in Linux. So in order to make a file or directory undeletable by any user, we need to make the file or directory unmodifiable using the chattr command. This command changes the file attributes in our system.
So in order to make it Undeletable Syntax is:
Note: We need superuser user privileges to set or remove the attribute sudo Command
sudo chattr +i - v "file name"
sudo chattr +i -V manager
The Above command, created an undeletable file that is inside an example directory.You can see the below image.
Suppose if you want to view the attributes of a file, use the lsattr command as shown below
You can see in the below image the attributes that are set/
Now if we try to remove the immutable file which we have made some changes above using the command like in below
rm manager sudo rm manager
We can see the below image after trying to remove it as both normal and root user
Next, if we want to make the Directory undeletable, then we should use -R Command to change the attributes and their files present inside.
sudo chattr +i -RV example1
To make a file mutable again we should use –i sign to remove all attributes as given below. Now you can see in the below image the example1 directory is now deletable because we have made it mutable using -i.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.