How to Protect Files and Directories from Deleting in Linux

LinuxFile SystemOperating System

Are you working as a Linux Admin? Do you generally defend script documents or any important files? You might be aware that, Chattr (change Attribute) is a command line Linux utility that is used to set/unset exact attributes to a file in Linux process to comfortable unintended deletion or change of major files and folders, although you’re logged in as a root user.

This article helps you to look after your records or folders that may support in disallowing an unauthorized user to delete your touchy contents.

Protecting File

In the below example abc.txt is the file name

Protecting a file

To protect file, use the following command –

$ sudo chattr +i abc.txt

Now try to remove file, using rm command as shown below –

$ rm abc.txt

The sample output should be like this –

rm: remove write-protected regular file 'abc.txt'?

Removing the protection to file

To remove the protection to file, use the following command –

$ sudo chattr -i abc.txt

To verify the above command, use the following command as shown below-

$ rm abc.txt

The above command removes the file without giving information.

Protecting Directories

In the below example abc is a directory name

Protecting a directory

To protect directory, use the following command –

$ sudo chattr -R +i abc

To verify the above command, use the following command as shown below –

$ rm -r abc

The sample output should be like this –

rm: descend into write-protected directory 'abc'?

Removing protection to the directory

To remove protection to the directory, use the following command-

$ sudo chattr -R -i abc

To verify the above command, use the following command as shown below –

$ rm -r abc

The above command remove the file without giving information.

In the above article, we have learnt about – Learn how to protect files & directories from delete. In our next articles, we will come up with more Linux based tricks and tips. Keep reading!

raja
Published on 21-Jan-2020 10:20:28
Advertisements