Home Command line utilities An Easy Way to Protect Files From Accidental Deletion In Linux
Protect Files From Accidental Deletion In Linux

An Easy Way to Protect Files From Accidental Deletion In Linux

By sk
256 Views

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.

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

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

Protect Files From Accidental Deletion Using rm-protection

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.

Protect Files From Accidental Deletion With rm-protection

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

Protect Files From Accidental Deletion With rm-protection utility

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.

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.

Resource:

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

1 comment

Red April 5, 2019 - 3:54 am

Unfortunately, this method does not stop a user from deleting files using the GUI.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More