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, we will do the same with a different program named rm-protection. Unlike chattr, the rm-protection utility 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 safe 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.
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
$ 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
The rm-protection utility 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 want to 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/
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.
The 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 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. As one of our reader mentioned in the comment section, this method does not stop a user from deleting files using the GUI. So, don't confuse this utility with security. It will not secure your files, instead it will keep the files from accidental deletion.
Another limitation of rm-protection utility is it 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 even if it is password-protected.