Tar Error – Unable to Untar File After Tar Error

archivedata-recoverytar

After unsuccessful attempt to make a tar.gz file I can't untar the file. It seems like I have lost it and yet this looks like trivial error one should be able to recover from. Is the content of the file really lost? I have tried all sorts of tar and unzip combinations without success.

OS environment and tar version:

$cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)

$tar --version |grep tar
tar (GNU tar) 1.26

Steps to reproduce:

[weshop@demo test]$ ls
file.txt
[filip@demo test]$ cat file.txt  
hello  

[filip@demo test]$ tar cvzf file.txt file.tar.gz  
tar: file.tar.gz: Cannot stat: No such file or directory  
tar: Exiting with failure status due to previous errors  

[filip@demo test]$ cat file.txt  
 ▒Om7▒▒7▒▒'([filip@demo test]$ 

[filip@demo test]$ tar xvzf file.txt  

[filip@demo test]$ cat file.txt  
 ▒Om7▒▒7▒▒'([filip@  

[filip@demo test]$ file file.txt  
file.txt: gzip compressed data, from Unix, last modified: Fri Jul 29 18:48:56 2016  
[filip@demo test]$  

Best Answer

On the line

$ tar cvzf file.txt file.tar.gz  

you have asked to create a compressed archive of the file file.tar.gz, and that the archive should be called file.txt. You have simply swapped the two filenames around.

The file file.txt will be overwritten by an empty compressed tar archive. This is why the subsequent commands displays weird things.

Yes, the original contents of file.txt is now lost. Sorry. Restore from backup.

See also this question.

Related Question