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.

3
  • 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
    Commented Nov 23, 2016 at 8:01
  • 1
    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
    Commented Nov 23, 2016 at 8: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
    Commented Nov 17, 2020 at 18:37

3 Answers 3

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.

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

2
  • 1
    The file wasn't deleted, it was truncated. Are you sure an undelete utility can help?
    – Celada
    Commented Nov 23, 2016 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" unix.com/fedora/…
    – alchemy
    Commented Jun 28, 2023 at 19:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .