The command below makes /backups/passwd file immutable (or undeletable). This implies that the file can’t be modified in any way: it can’t be deleted or renamed. You can’t even create a link to it and no data can be written to the file as well.

Note that you need superuser privileges to set or remove this attribute, using the sudo command:

$ sudo chattr +i /backups/passwd
$ sudo chattr +i -V /backups/passwd

To view attributes of a file, use the lsattr command as shown.

$ lsattr /backups/passwd 

Now try to remove the immutable file, both as a normal user and as a root.

$ rm /backups/passwd
$ sudo rm /backups/passwd

How to Recursively Make Directory Undeletable in Linux

Using the -R flag, you can recursively change attributes of directories and their contents as follows.

$ sudo chattr +i -RV /backups/  

To make a file mutable again, use -i sign to remove the above attribute, as follows.

$ sudo chattr -i /backups/ passwd

Was this answer helpful? 0 Users Found This Useful (1 Votes)