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
[filip@demo test]$ cat file.txt  

[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  

[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]$  
  • 1
    tar cvzf file.tar.gz file.txt is what you wanted to do at the third step. – user4556274 Jul 29 '16 at 16:44
  • you are trying to create an archive called file.txt in that example. The archive name comes before the files you want to add. I am assuming that 'file.txt' has beem overwritten with a gzipped tarball. To test this, run gzip -d file.txt and then do file file.txt. Alternately if you dont want to replace the gzipped file, do something like zcat file.txt > file.decompressed and then check what it is with file file.decomprrssed. It is likely to be an empty tar archive. You have overwritten file.txt due to incorrect use of the tar command. – Wyatt8740 Jul 29 '16 at 17:11

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.