Protecting Files & Folder on Linux

How to protect files and folders from deleting on Linux?

Protect Files

to prevent files from deletion on linux

Protect a file from deleting

$ sudo chattr +i file.txt

Revoke protection on file

$ sudo chattr -i file.txt

Explanation

this will change the permission on the file to :

$ lsattr file.txt
— — i — — — — -e — — file.txt
the remove {rm command) will give the following warning. 
rm: cannot remove ‘file.txt’: Operation not permitted

Protect Folders

this will protect the folder and its content from accidental deletion and/or modification in linux.

Protect folder for deletion

$ sudo chattr -R +i dirname

Revoke protection

$sudo chattr -R -i dirname

Explanation

this will protect the folder and its content from accidental deletion and/or modification in linux.

Protect Folders but allowing file modification

 to prevent files and folders from deletion, but allow the file for writing in append mode only

Allow writing on file which is protected from deletion

$ sudo chattr +a file.txt

Allow writing in append mode for folders/directoies.

$ sudo chattr -R +a dirname