An Easy Way to Protect Files From Accidental Deletion In Linux

Protect Files From Accidental Deletion In Linux

A while ago, we have published an article that described how to use chattr command to prevent files and folders from accidental deletion in Unix-like operating systems. Today, in this guide, we will do the same with different method using rm-protection utility. Unlike chattr, rm-protection will ask you to answer a question in order to delete the protected files. If you failed to provide the correct answer to the question, then you can’t delete the files. It is a safest alternative to ‘rm’ command with minimum difference.

In this brief guide, I will explain how to install and use rm-protection to protect files from accidental deletion in Linux.

Install rm-protection

The official way to install rm-protection is to use pip, the python package manager.

To install PIP on Arch Linux and its derivatives, run:

sudo pacman -S python-pip

On RHEL, Fedora, CentOS:

sudo yum install epel-release
sudo yum install python-pip

Or,

sudo dnf install epel-release
sudo dnf install python-pip

On Debian, Ubuntu, Linux Mint:

sudo apt-get install python-pip

Once PIP installed, run the following command to install ‘rm-protection’.

sudo pip install rm-protection

Protect Files From Accidental Deletion In Linux  using rm-protection

rm-protection works exactly like ‘rm’ command. The only difference is it will ask you to answer a question. It consists of two utilities namely rm-p and protect. Here, ‘rm-p’ will remove the files and ‘protect’ utility will protect your files from the accidental or intentional deletion. Allow me to explain with some examples.

First, make an alias for ‘rm-p’ and ‘protect’ utilities for easy convenience. This is optional. If you don’t create alias, you need to type rm-p each time when you want to delete a file.

alias rm="rm-p"
alias protect=protect

Let us say, we have an important file called ostechnix.txt.

To protect this file, run:

protect ostechnix.txt

You will be asked a question and its answer to protect the above file.

Question for /home/sk/ostechnix.txt: Do you love Linux?
Answer: Yes I do

Now, the above file is protected. The question and answer for this file is saved in .ostechnix.txt.rm-protection file in the current directory. You’re going to need the answer when you want to delete the file.

Now, let us try to delete this file.

rm ostechnix.txt

You will be asked to enter the answer for the question. If you failed to answer, the rm-protection will refuse to delete the file.

rm-p: /home/sk/ostechnix.txt: Do you love Linux?
Answer: No
rm-p: Wrong answer! /home/sk/ostechnix.txt will not be removed
rm-p: The answer is stored in /home/sk/.ostechnix.txt.rm-protection
rm: missing operand
Try 'rm --help' for more information.

If you provide the correct answer, then it will delete the file.

rm-p: /home/sk/ostechnix.txt: Do you love Linux?
Answer: Yes I do

It is simple as that.

Also, you can protect all files inside a folder by using -R flag.

Say for example, we can protect all files inside a directory called test as shown below.

protect -R test/

Sample output:

Question for /home/sk/test: Do you love Linux?
Answer: yes

Now, you can’t delete the files or the parent folder without providing the correct answer.

Wait, there are some limitations/problems too

Every applications have some limitations and disadvantages.

rm-protection is a case sensitive utility. So, you should use the correct case (upper or lower case) while providing the answer. Say for example, if your answer is “Yes I do”, and you enter “yes i do”, rm-protection will not delete the file. You need to enter correct answer with correct case, just like “Yes I do” as you provide while protecting the file in the first time. If there is any comma in your answer, you need to provide it too.

Look at the following example.

rm ostechnix.txt
rm-p: /home/sk/ostechnix.txt: Do you love Linux?
Answer: yes i do
rm-p: Wrong answer! /home/sk/ostechnix.txt will not be removed
rm-p: The answer is stored in /home/sk/.ostechnix.txt.rm-protection
rm: missing operand
Try 'rm --help' for more information.

As you noticed in the above example, even though I have provided the correct answer, rm-protection refused to delete the file. It is because of lower case letters in the answer.

Also, You must be very careful while using utility to protect the files. Be mindful that rm-protection is not a security program. If someone knows the answer or has the access to the file (.ostechnix.txt.rm-protection), s/he will know the answer and easily delete the file. So, don’t confuse this utility with security. It will not secure your files, instead it will keep the files from accidental deletion.

rm-protection will protect only the normal user’s files/folders. The root user can still able to delete the protected files. If someone knows the root password, your files will be deleted.

And, that’s all. I will be soon here with another useful guide. Until then, stay tuned with OSTechNix. If you find this guide useful, please share it on your social networks and support us.

Cheers!

Resource:

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You may also like...