Using chattr (Change Attribute) command in Linux, you can prevent accidental deletion and modification of files and folders even as root user by changing file attributes . It comes pre-installed in Ubuntu Linux operating system.
Prevent Files Folders From Deletion Or Modification using Chattr
Now select any file from the Home directory (for example: mytextfile.txt file in my Home folder) or create a new file. Now run below command to makes the file (mytextfile.txt) immutable. You can replace “mytextfile.txt” with your file name.
sudo chattr +i mytextfile.txt
Now you can’t modify or delete that file. Just try to delete and modify that file. You will get below errors.
You can also try it with sudo command.
sudo rm mytextfile.txt
sudo echo 'CONNECTwww.com' >> mytextfile.txt
How to Revoke attributes using Chattr
In terminal app run below command.
sudo chattr -i mytextfile.txt
Now you can remove or modify that file.
In this same way you can make any folder and its files immutable using below command.
sudo chattr -R +i directory
Here you can replace “directory” with your real directory name in your Home folder.
To revoke it just run below command.
sudo chattr -R -i directory
In this way you can files and folders undeletable in your Ubuntu Linux system.
For the complete usage details run below command.
man chattr