[Linux]: How to prevent accidental execution of “rm -rf” command

Updated on September 3, 2017
Delete files accidentally
Delete files accidentally

As humans we should accept that mistakes like : rm -rf * would happen ! As saying “Prevention is better than cure” we shall follow best practices during execution of such critical commands. Here we will be showing you how to avoid such accident deletions on your Linux machines. Still if you have deleted files and want to retrieve them, then follow : how you can retrieve deleted data !

1. Never ever use rm -rf ./* (Recommended)

As a matter of practice, instead of ./ at the beginning of a path just use *. The slashes really add no value to the command and will only cause confusion. The above command can be better re-written as :

rm -rf *

2. Use “–preserve-root” as an option to rm (Recommended)

The rm command includes a “–preserve-root” option to prevent the accidental deletion of the root directory, but that behavior is not the default in older machines. So simply add the following to /etc/bashrc and /etc/profile from now on:

alias rm=”rm -–preserve-root”

Note: If you still want to delete / you can use –no-preserve-root attribute

3. If you want to prevent any important directory deletion ? (Recommended)

Create a file named -i in that directory and to do that follow the below command :

touch -- -i or touch ./-i

Now try rm -rf *:

Mon Oct 21>$pwd
/tmp/demo
Mon Oct 21>$touch {1..4}
Mon Oct 21>$touch -- -i
Mon Oct 21>$ls
1  2  3  4  -i
Mon Oct 21>$rm -rf *
rm: remove regular empty file `1'? y
rm: remove regular empty file `2'? y
rm: remove regular empty file `3'? y
rm: remove regular empty file `4'? y

The above command translates itself to : rm -rf -i *, wherein -i is to delete files interactively. So it prompts for each file deletion !

4. Use safe-rm (Recommended)

Safe-rm is a safety tool intended to prevent the accidental deletion of important files by replacing /bin/rm with a wrapper, which checks the given arguments against a configurable blacklist of files and directories that should never be removed.

Users who attempt to delete one of these protected files or directories will not be able to do so and will be shown a warning message instead:

#rm -rf /etc
Skipping /etc

5. As in windows, you can create a trash directory and move all the deleted files to trash directory. (Not recommended)

This is not a good practice, as you keep deleting, these files/folders still gets accumulated in the trash directory and you need to spend time on deleting those too. Still if you would like to do it, here it is : Create a file del.sh with the following content under /usr/local/bin/.

#!/bin/bash
DIRECTORY="$HOME/.Trash";
if [ ! -d $DIRECTORY ] ; then
       echo "Directory $DIRECTORY doesn't exist";
       echo "Creating the .Trash directory.....";
       mkdir $DIRECTORY;
fi
mv "$@" $HOME/.Trash

create an alias for rm in your /etc/bashrc or /etc/profile

alias rm="/usr/local/bin/del.sh"

6. Create a bash script to warn you when you run rm command ? (Recommended)

Create a file del.sh with the following content under /usr/local/bin/.

#!/bin/bash
# Copyright (c) 13th Aug 2014 Focus4Infotech
#           All Rights Reserved
# Function of this script is to avoid unknowing deletion
# of files, which is hard to recover. This script
# doesn't backup, instead it just warns and upon user
# confirmation, it removes the files. 
# Authors : Focus4infotech.com
echo " __   ___ _     ___ ___ _ _  _  ___     ___ _ _     ___  ___     ";
echo "|  \ |___ |    |___  |  | |\ | | __    |___ | |    |___ [__      ";
echo "|__/ |___ |___ |___  |  | | \| |__]    |    | |___ |___ ___] ....";

echo "           __   ___     ___  __   ___  ___  ___ _  _ _    ";
echo "          |__] |___    |    |__| |__/ |___ |___ |  | |    ";
echo "          |__] |___    |___ |  | |  \ |___ |    |__| |___ ";

echo "       ___  __  _  _ ___ _ _  _ _  _  ___    _   _   _  _ ";
echo "      |    |  | |\ |  |  | |\ | |  | |___     \_/  / |\ | ";
echo "      |___ |__| | \|  |  | | \| |__| |___      |  /  | \| ";

read -p "" choice
case "$choice" in
  y|Y ) rm "$@";;
  n|N ) echo "Exiting without any change.....";exit 0;;
  * ) echo "Exiting without any change......";exit 0;;
esac

create an alias for rm in your /etc/bashrc or /etc/profile

alias rm="/usr/local/bin/del.sh"

Was this article helpful?

Related Articles

Leave a Comment