3

If you have a file called myfile and you do cat > myfile instead of cat myfile (to see it's contents) I understand that it is overwritten. Is there any possible way to recover its contents? I tried doing Ctrl+C, but the file is still overwritten.

  • 5
    Restore from your regularly updated backup. That's your best bet. (Set up a backup before going further, if you haven't already.) :) – Wildcard Nov 23 '16 at 08:01
  • 1
    http://unix.stackexchange.com/q/2677/135943 may help, although there are probably different ways to restore from a truncated file (as in this case) rather than a deleted file (as in the question I linked to). Still, there is no easy answer. – Wildcard Nov 23 '16 at 08:06
  • I did exactly the same thing with cat and was sad, but with the link from @Wildcard, specifically the accepted answer was able to recovered it. Remembered first some shorter words then a long hyperlink from the file and out of a massive file which was dredged up was able to find my accidentally overwritten text. If there is a next time, possibly better to not exit after pressing enter if you remember and recover from open file. – cardamom Nov 17 '20 at 18:37

3 Answers3

7

As soon as you redirect the stdout of any command to myfile with

any_command > myfile

the system creates myfile; if there was another file with the same name, it gets overwritten.

So your best bet is to restore from a backup.

dr_
  • 29,602
1

If the process is still holding on to the file you can goto /proc//fd/

then try to cat /proc/pid/fd/filedescriptor and do it.

Take a look at this

0

For ext3/ext4 file system maybe is extundelete.

But I never use it.

more explanation here

  • 1
    The file wasn't deleted, it was truncated. Are you sure an undelete utility can help? – Celada Nov 23 '16 at 22:44
  • apparently extundelete doesnt work anymore. "it appears that the extundelete utility has fallen out of maintenance and that it requires a version of e2fsprogs that is too old" https://www.unix.com/fedora/279812-segmentation-fault-while-trying-recover-file-extundelete.html – alchemy Jun 28 '23 at 19:42