Hi all!
Back in January, I had the misfortune of a "recommended" rm command wiping my desktop. While we all know about the rm -i
and rmdir -ri
commands, this didn't help here, as typing "n" or "N" upon prompt for some reason simply did nothing. I pulled the plug at the end, so recovery is going somewhat well, but I'm still down a working desktop at 400 bucks, 4 months later.
This is a level of fustration noone should go through ever, so in addition to my recovery attempts (possible, but costly), I've also done some deep digging into how to prevent this from happening again - the fruits of my labor, I share with you.
Please note: This is to prevent accidental deletions, not to promote overuse of the rm command - I completely stand by Stew's statement here. ONLY use rm commands as absolutely neccessary, and if needed, only ever use them with the file path!
With that in mind: how to keep your data safe, instead of yeeting it into the great server in the sky!
- First, install trash-cli, if you haven't already - this is a vital program that aids in easier data deletion And recovery!
sudo apt-get install trash-cli
- Then, backup your bashrc file
cp .bashrc .bashrc-backup
- Go time - access your bashrc file with the following command:
sudo nano .bashrc
At the bottom of the file, paste the following:
confirm() {
echo -n "Do you want to perform the given deletion? Double-check the name! Instead of permadeletion, it will be moved to the Trash via $*, just in case. [N/y] "
read -N 1 REPLY
echo
if test "$REPLY" = "y" -o "$REPLY" = "Y"; then
"$@"
else
echo "Cancelled by user"
fi
}
alias rm='confirm trash'
alias rmdir='confirm trash'
(You can make the echo neater with an /n command if you want, but it's late and I'm feeling lazy. Many thanks to Mikel for the base code.)
- We're almost done! Refresh your bashrc with the following command:
source ~/.bashrc
You should be set! Now whenever some command you run requires file or directory deletion, you will be both prompted for confirmation, and your listed target still sent to your Trash as a safeguard - here, it can be easily restored using Trash commands, if neccessary.
Just to be safe, set up a folder/directory with a blank file within to make sure it worked - if done right, the "rm"ed fil should end up in your trash, and the "rmdir"'d directory should do the same. If not, restart the computer, then try again (file refresh doesn't always work, for some reason.
Trash commands can be handled, as shown here: Trash-cli - A Trashcan Tool to Manage 'Trash' from Linux Command Line. Additionally, simply removing items from trash will have the same effect as the original "rm" command.
Is this extra work? Yes. Tedious for fully deleting data? Yes. Will this save you the headache of an accidental command line deletion?
Hell yes.