Linux security

How to Make File and Directory Undeletable in Linux

There may be situation that you want to make sure your file or directory cannot be DELETED by accident or intentionally.
In my case I like to protect this way my special directory containing my special and very important scripts. This is very easy to setup.

You need to obviously elevate your rights to do this task:

sudo chattr +i /home/Scripts/

Youc an view attributes of the file using this command:

lsattr /home/Scripts/

If you try to DELETE it, you will get error message

$ sudo rm /home/Scripts/
rm: cannot remove '/home/Scripts/': Operation not permitted

Now, it you want to revert and make the file again as before so you can delete, you just need to use this switch (-i) in the command

sudo chattr -i /home/Scripts/

The same you can do for DIRECTORY and here is the command:

sudo chattr +i -RV /home/Scripts
sudo chattr -i -RV /home/Scripts

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.