Ubuntu – Corrupt file after power failure

data-corruptionencodinglatex

I just had a power failure while I was working on a text file (specifically a latex .tex file). I had been saving regularly – so wasn't too worried about lost work. But now I can't actually use the file at all – if I open it in gedit, it's all gobbledigook, with the following error message:

There was a problem opening the file "[…].tex”.
The file you opened has some invalid characters. If you continue editing this file you could corrupt this document. You can also choose another character encoding and try again.

(It was on UTF-8 – tried the other option to no avail)

Is there anyway to recover the file? Not the work I didn't save – but the entire file as it was before saving?

Best Answer

You have NUL values in your file (due to the power failure).

tr < {original_file} -d '\000' > {backupfile}

will remove the NUL values from {original_file} while creating a {backupfile}.

Check {backupfile} and confirm it is correct before deleting {original_file}.


If you do

file {original_file}

it probably will tell you it is a "binary file" and not a "text file".

Related Question