This question already has an answer here:

Very Long story short. In essence this is the problem.

I had some of my project files (new.cc) which I was working on for quite sometime. I created one backup of these files two months ago(old.cc), and basically what I did was "> cp old.cc new.cc". And I have lost all the changes made in last two months.

Is there any way to recover these files. Any help is appritiated.

marked as duplicate by Scott Stensland, user535733, karel, Kulfy, Eric Carvalho May 26 at 23:01

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    the cp command makes a copy. It doesn't remove anything. If you ran the command cp old.cc new.cc then both files should still exist and they will be identical to old.cc – Nmath May 26 at 1:41
  • 4
    You have learned a painful lesson. Backing up every 2 months for something that's actively changed is not adequate. – Organic Marble May 26 at 1:50
  • 3
    all software developers do this error ... only once ... daily backups to multiple places ... use a remote source code repository like github.com ... make your edits on a dir which lives on dropbox so current computer can meltdown with no loss of precious files – Scott Stensland May 26 at 1:50
  • 1
    @OrganicMarble Thanks, that makes more sense. OP I'm not aware of any methods to restore (parts of) a file that has been overwritten apart from using forensic tools to inspect the drive bit for bit; which is nothing short of impossible and exhaustive and may not even pay off. – Nmath May 26 at 1:56
  • 2
    If you wish to recover a deleted file immediately stop using that computer NOW ... over time deleted files will become unrecoverable however if you perform forensics immediately after file got deleted your chances of success increase – Scott Stensland May 26 at 2:02

In short, unfortunately, there is no way to recover the newer versions of your files, unless you still have a copy elsewhere. Your copy command has overwritten your new versions with the old versions.

The copy command copies files from the source (old.cc in your example) to the destination (new.cc in your example). If files with the same name exist in the destination (in your case the newer versions), they will be overwritten. By default, the command cp will not ask if you want the file to be overwritten unless you supply the -i (--interactive) option, as in `cp -i .

The mistake was that you swapped source and destination. You really wanted to execute cp new.cc old.cc ("copy new to old") rather than cp old.cc new.cc.

Data recovery - As outlined here, there are ways to recover deleted information. As a matter of fact, the copy operation does not physically overwrite your information on the disk because of the way cp works on an ext formatted file system. That means that the bits and bytes of your new versions may still be somewhere on your hard disk. However, if you attempt do it yourself, it will be a huge challenge to recover these bytes to files that will have a random name, then manually inspect all of your possibly thousand reconstructed files to determine for each file if the file is still usable and what it represents. If you give it to a specialized data recovery service, it will cost a lot.

  • Instead of cp new.cc old.cc, I suggest using a numeric or date-based system for file names so you have access to other revisions in the past. This is just best practice. Naming your files by YMD date also gives the added benefit or ability to have them arranged in chronological order in nearly every situation. – Nmath May 26 at 2:03
  • 4
    @nmath Just use a proper source control system, like git. No need to re-invent wheels. – PerlDuck May 26 at 8:07
  • 3
    @Nmath hand-versioning like that is very much not best practice. Using version control software is. – marcelm May 26 at 10:07
  • Version control software does just that. Automation of course makes things much easier. The practice however, is fundamentally the same – Nmath May 26 at 16:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.