Lost source file after compilation with GCC

Hello.
Please help.
I've accidentally executed a wrong command and lost one of my .cpp source files (file1.cpp).
The command entered was the following:

gcc -g -o file1.cpp file2.cpp main.cpp

The compiler have probably deleted or overwritten the file.
Is it possible to recover the lost file (file1.cpp)? I've been working on it about two weeks and it was a very important file.
Last edited on
Unfortunately, I do not that that is something that can be recovered. I've lost source files a number of times and all I can do is offer preventative steps to make it less critical in the future. Version control is a programmers best friend. CVS and Subversion (SVN) are widely available for Linux. If version control is not available, backup often.
Of course, I will be more cautious afterwards. But isn't it possible to recover the file in some way...
It all depends on how the file system is implemented. Basically, Linux file systems have a table of inodes with file information and a data block. The inode has information such as the filename, permissions, and where the data block lies on the disk. The data block contains the contents of the file. If I had to guess, I'd think that the inode was updated to point elsewhere and that the data block may still be intact. The problem would then be that there is no way to find it.

Note that this is just a guess. If the file compiled, the executable would have been larger than your source file. In that case, it may have overwritten the data block (if it fits in what was previously allocated) or it may have allocated another block.

I would just get started rewriting it. If it took two weeks before it should take significantly less time this time around.
I know that there are some Linux applications ("Midnight Commander", for example) or tools (e.g. grep) that recover deleted text files. Could such tools help in this case?
Huh? Grep can recover deleted text files...??

But they could, you could try running them and see what happens.
Someone wrote a variant of grep that parses the superblock and tries to find fragments of text files or something. But from what I've read about the ext2fs, block pointers are zeroed after a file is deleted so unless you force the system to halt without syncing first, it's unlikely that it will work.
WOW. Grep did recover the file, though partially. Several fragments are not found!!!

http://www.cyberciti.biz/tips/linuxunix-recover-deleted-files.html
http://datarecoverypros.com/linux-deleted-files.html
Great!!! Grep really did a great job. The 400-row code was recovered completely, without any damage!!!!
Nice.
Thanks a lot, guys!!!!!!!!!!!
Hmm, not sure about all Linux flavours but in the Ubuntu I have, whenever I open any text file, let's say test.c, it will create another file namely test.c~.

I lost one of my files exactly how you did. When I typed in the Terminal I found a copy of my old file test.c~.
All I had to do was rename it to test.c and surprisingly it was the same file.

I don't know if this applies to every Linux distribution.
It depends on the text editor
Hmm, not sure about all Linux flavours but in the Ubuntu I have, whenever I open any text file, let's say test.c, it will create another file namely test.c~.


It depends on the text editor


Vim does that. Although I think it is configurable.
gedit also does that.
Hmm, not sure about all Linux flavours but in the Ubuntu I have, whenever I open any text file, let's say test.c, it will create another file namely test.c~.

I lost one of my files exactly how you did. When I typed in the Terminal I found a copy of my old file test.c~.
All I had to do was rename it to test.c and surprisingly it was the same file.

I don't know if this applies to every Linux distribution.


Before using grep, I tried a lot to find any backup files associated with the lost source file, but got no result. It wasn't also in the trash. After a few hours of useless attempts to find any copies or backups, eventually I tried grep and recovered the file in a few minutes.
The command I entered was the following:
sudo grep -a -B[number of rows before the text being searched] -A[number of rows after the text being searched] '[some unique text in the lost file]' /dev/sda3 > test.cpp

After a few minutes, a log file and a .cpp file were generated where I found my code by entering the command
'vim test.cpp'.
Topic archived. No new replies allowed.