Hi Dude, sometime we want lock a file or folder from overwritten because any update or others. Simple method is setting the immutable flag on a file or folder will prevent any changes to it. For folders, this means that no files or foldes may be added to or removed from the immutable folder. However, file and folder in the immutable with chattr can’t be modified.
File or Folder Immutable
Making Files in Linux Immutable with the “chattr” Command. The chattr command in linux is used to change the attributes of files on linux to avoid incidents of wrong deletion, or prevent important files and directories on the system from being changed. Chattr itself also applies to all users, including users with root privileges though.
For linux user, simple method create file or folder immutable is using command "chattr"
with the “+i” flag.
chattr +i /path/to/file
With "chattr"
above, the file is immutable. If any update or anything the file is not change.
And then, how to vice versa? Can I make a file or directory no longer immutable?
No Longer Immutable
Here is simple way. To change a file or folder so that it is no longer immutable, use the “chattr” command with the “-i” flag. Here is an example:
chattr -i /PATH/TO/FILE/DIRECTORY
If we want other parameter, please read more about chattr can use manual command.
man chattr
lsattr to Show List Attribute
And then, what is different if the file set with chattr?
Here is example. Attribute file before receiving immutable.
[root@srv habibza]# lsattr remove.txt -------------e- remove.txt
After given command chattr +i
, there is a flag "i"
in lsattr.
[root@ns3 habibza]# chattr +i remove.txt [root@ns3 habibza]# lsattr remove.txt ----i--------e- remove.txt
That is short article about chattr. May be it’s helpful, please feel free to leave a comment if you have any questions and I’ll appreciate it.