Recovering moved or deleted files in Linux

I was developing one C program which I spend 1 day writing with around  500 lines of code in Ubuntu 12.04 

for compiling in GCC, I used the below command

# gcc server.c -o server.c

instead of giving gcc server.c -o server 

now my whole source code was replaced with the binary file :( :(  , a simple mistake which caused my whole source code got deleted or replaced. I had a habit of pressing for auto completing the file names. 

After searching for some time googling, i got a solution which worked for me and saved my 9 hours of work.

I got the help form this below link

repeating what is said there and which i tried and worked for me

in the console with sudo permission
sudo grep -a -B[size before] -A[size after] 'filename or unique content' /dev/[your_partition] > file.txt

-i : Ignore case distinctions in both the PATTERN and the input files i.e. match both uppercase and lowercase character.
-a : Process a binary file as if it were text
-B Print number lines/size of leading context before matching lines.
-A: Print number lines/size of trailing context after matching lines.

my command was
sudo grep -a -B10 -A1000 'UNIQUE_SERVER_IP' /dev/sda1 > file.txt

UNIQUE_SERVER_IP was the #define i did it in my source code. After the command is over, open the file.txt with vi or any text editor and search for the string 'UNIQUE_SERVER_IP'. you should find your source code around the string, copy paste and save to the required file name

I got the my source code back in some time.


Popular posts from this blog

phpbb3 Forum not showing after creating