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.
-
5Restore from your regularly updated backup. That's your best bet. (Set up a backup before going further, if you haven't already.) :)– WildcardCommented Nov 23, 2016 at 8:01
-
1unix.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.– WildcardCommented 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.– cardamomCommented Nov 17, 2020 at 18:37
Add a comment
|
3 Answers
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.
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
-
1The file wasn't deleted, it was truncated. Are you sure an undelete utility can help?– CeladaCommented 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/…– alchemyCommented Jun 28, 2023 at 19:42