21 C
Texas
angeloma
Senior Writer and partner

Protect your files from accidental deletion

It seems like a lie but if you work a long time with the Linux terminal, it is very likely that at some point you will have to delete several files. And that is where the problem comes in, you can make a mistake and delete some very important files or even an entire partition. So for that, today I will teach you how to protect your files from accidental deletion, with this you can work safer with your files safe.

To do it I present you two very simple methods to do it, one consists of using an external program for it. The other method is with a command that normally comes installed on all Linux distributions.

So, let us start.

1. Protect your files using rm-protection

rm-protection is a small program similar to an rm extension that protects your files from accidental deletion. The program can be installed via PIP so there should be no problem with it.

- Advertisement -

First, you need to install PIP.

If you use Debian, Ubuntu, Linux Mint or derivate:

$ sudo apt install python-pip
$ sudo apt-get install python-setuptools

1.- Install python-pip
1.- Install python-pip

In case you are using Arch Linux or derivates, you can install it with this command:

:~$ sudo pacman -S pyhton-pip

On CentOS 7 and RHEL:

:~$ su
:~# yum install epel-release
:~# yum install python-pip

Next, install rm-protection.

:~$ sudo -H pip install rm-protection

2.- install rm-protection
2.- install rm-protection

Next, use the command to protect your file. For example, I have one file called examplerm.txt.

:~$ protect examplerm.txt

3- protect your files using rm-protection
3- protect your files using rm-protection

Then, the tool will ask your to set a question and answer. If you want to eliminate it, it will ask you a question and you must answer it correctly.

Now, try to remove it.

:~$ rm-p example.txt

You will see this:

4.- The questions
4.- The questions

As you can see, you have to answer the question. If you don not answer the question correctly, the program will not allow you to remove the file.

5.- Wrong answer
5.- Wrong answer

However, if you type the answer, then you will remove it.

6.- Deleting a file
6.- Deleting a file

The script is very good, but not infallible. The root user can delete without knowing the answer to the question, just as if the user knows the answer as well. Use it more than anything to avoid accidents, not to give security to the system.

If you want more security, I recommend to you create a Alias and change rm for rm-p.

2.- Protect your files using the chattr command

The second form is somewhat more drastic than the first and is using the chattr command. It is a command available on almost all Linux distributions.

Its usage is like this:

:~$ chattr [-pRVf] [-+=aAcCdDeijPsStTu] [-v version] files

Do not worry, it is too easy to use it.

For example, I will protect one file called examplechattr.txt.

:~$ sudo chattr +i examplechattr.txt

 

7.- Protect a file using chattr
7.- Protect a file using chattr

I’ll explain briefly. Chattr has an operator like + that adds a feature or – that removes it. Option i indicates that the file is immutable, i.e. it cannot be deleted.

 

Let us try to remove it.

8.- The file is protected
8.- The file is protected

As you can see, even the root user is not allowed to remove the file.

If you want to remove this attribute, you can do so with this command:

:~$ sudo chattr -i examplechattr.txt

9.- Revoke the i attribute
9.- Revoke the i attribute

Now, you can delete it.

The same way you can protect entire folders. For example:

:~$ sudo chattr -R +i Example/

10.- Protecting a folder
10.- Protecting a folder

To revoke it

:~$ sudo chattr -R -i Example/

So, that’s it.

Conclusion

The protection of data and files is an essential task of a sysadmin, for it there are two very simple ways to do it.

Please share this post with your friends.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article