After spending a few hours on developing some scripts, I hastily fat-fingered a command and removed my entire scripts directory on my RHEL 7 server. My heart sank because I had no backup and hadn’t yet checked in my scripts into git. After frantically looking for tools to recover my scripts, I came across TestDisk/PhotoRec and photorec saved my bacon.
photorec is quite straightforward to use and recovers files based on your selected file types into a selected destination. Use lsblk to confirm the partition on which your lost files resided and execute sudo photorec <partition> (e.g. sudo photorec /dev/sda2)
The image below is a screenshot of the recovery process.
Upon checking the recovery destination, you will find directories and files (I was interested in *.sh files) as shown below:
After performing a few “find and grep” commands looking for my script content, I retrieved the latest version of all my scripts. You will find earlier versions of your script too. So, check carefully. Once retrieved, I performed my tests on the recovered scripts and they worked as before. Phew!! Immediately backed up my scripts! Thank you Christophe Grenier!