Earlier this afternoon, I did something really really really really really^(infinity) careless and stupid.
I was working on a bash script containing various commands to automate some things that had taken me days and days to learn and get my head round. The script was outputting its results in a directory, which i was continuously deleting, as I repeatedly (re)ran the script to debug and improve stuff.
Cue moment of complete madness:
instead of typing
rm -rf [output_dir]
rm -rf [script_filename]
…and then it was gone.
Oh… I forgot to back it up or take a copy before I started editing in improvements didn’t I
big sigh…. this is unix… no easy undelete available (at least not that I know of)
now what… ? This would take me days and days to reproduce.
some frantic googling later… this linuxforums thread came to the rescue!
Now I didn’t actually unmount the partition because I had an active session with lots of other stuff i needed not the best idea… always unmount while doing this
- STOP everything and DO NOT edit anything more (to prevent the diskspace being overwritten)
- Unmount the partition
- run the following command:
grep --binary-files=text -500 «search_string» /dev/sda4 > output.txt
– 500 is how many lines before and after the seatch_string to display
– search_string is a string contained in the file to be recovered
– /dev/sda4 is the partition from which the file was deleted
This reads through the whole partition, prints the number of lines before/after the string pattern into the file output.txt
Then you can search through the output.txt file and hopefully find a block of text that corresponds to the content of the file…. ready for copying/pasting/saving into a new file and immediate backing up onto every partition on the machine…. onto every USB memory stick you can find lying around your desk and in your pockets…. via ssh to every remote machine you own or have an account on… … … etc.
There are also various tools available like magicrescue (on Ubuntu
sudo apt-get install magicrescue), but in this instance it didnt work for me – probably coz I didn’t create the recipe file correctly or didn’t know how to run it properly.
But good old grep was enough.