We have all heard the story of the developer and/or system administrator that has accidentally deleted the wrong file or directory ...or worse. What happens if all of the files within the $HOME directory or operating system (if operating as the root user) are deleted on accident? Yikes!! I can honestly say that this has never happened to me (..as of yet..and I don't intend for that to ever happen). ย With that said, admittedly, I am a mere mortal that exists within the vast time continuum of time and space. This means that, like every other mortal, I am bound to make mistakes.

Thanks in large part to the kind souls who have sounded the warning of the dangers of the rm -rf command over the course of my dev journey, I have taken note of the fact that to err is inevitable. The best defense (other than creating periodic backups of important data and utilizing a dependable version control system) is to be aware of the potential danger that lurks when deleting files and directories via the command line.

This is why I have been using trash-cli on my local machine for at least 4-5 years now (hat tip Wes Bos !). ย Instead of typing rm-rf <directory-name>, I have gotten into the habit of typing trash <directory-name> instead. ย  It is simply good practice to be mindful when deleting files and directories, especially when running many commands and processes simultaneously within several open terminal windows. Being on a Macbook Pro already provides me with the luxury of retrieving deleted files and directories directly from my trash can. When SSHing into an Ubuntu or CentOS VM or server instance, however, this luxury is not available by default. In the absence of a graphical interface, there is no trash can to retrieve lost data from!

Avoiding unintended deletions:

One way is to force bash to issue a warning that asks you if you really intended to delete that file &/or directory:

Let's create an example situation where I am about to delete 20 files recursively from a directory:

mkdir dir_with_many_files

sudo vim create_dir_with_many_files.sh

#!/bin/bash

init() {

  echo "Creating 20 files  ๐Ÿ‘๐Ÿ‘๐Ÿ‘๐Ÿ‘๐Ÿ‘๐Ÿ‘๐Ÿ‘๐Ÿ‘" 2> /dev/null

  cd dir_with_many_files

  for i in {1..20}
    do
      touch file${i}
    done

 }

init

cd ../
ls  dir_with_many_files/*

# sudo chmod +x create_dir_with_many_files.sh

..run the above script to create some throw away files:

Using -l will only create a prompt/warning confirmation when attempting to delete several files (in this case from within a directory) at once:

I don't know about you, but I'd prefer to also have the option to retrieve any data that may accidentally be lost.

Enter the Trash CLI:

Note: I am running Ubuntu 20.04.3 LTS (Focal Fossa). To install on CentOS run sudo yum install trash-cli.

Send a file to the trash can and then see it listed in the trash can:

Oh crud! I didn't mean to do that. Lucky for me, I can bring the file back:

Great! My file has been restored from the trash.

Let's repeat this process again by 'accidentally' deleting several files within a directory:

After having deleted an entire directory I was able to restore it in full.

When you really want empty the trash, just run trash-empty to permanently delete any unwanted files &/or directories.

That's it for now. ย I hope that this helps someone.

Cheers:-)

PS. Conducting regular, periodic backups (in at least 3 locations) of your data is also an important step when it comes to putting your mind at rest when it comes to any preventing data loss that can occur either due to human error or server downtime.