When extracting a tar file with the tar command, you may face errors that say “gzip: stdin: unexpected end of file
” and similar errors. In this guide, we will explore the potential causes of this error and how to fix it.
The error could look like this:

Other possible errors you could see when extracting tar archives:
tar: z: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
The Primary Suspect: A Corrupt File
A corrupt file can cause this issue as it was in my case. This error typically occurs when a .tar.gz
file has not been fully downloaded, resulting in an incomplete and unusable file.
When using cURL
, you may encounter this error if the file’s URL redirects to another location. By default, cURL does not follow redirects, which can cause a partial file download. To counter this, passing the -L
option is essential, as it compels cURL
to follow redirects until the entire file has been downloaded.
So if you downloaded your tar
file using cURL
, make sure you passed the correct options:
curl -LO https://example.com/download/my-downloaded-file.tar.gz
You can also check the size of the downloaded file and make sure it matches the file size on the remote site. You can check the size of a file with du
command in Linux:
du -sh my-downloaded-file.tar.gz

As you can see from the screenshot, the size of my downloaded file is 0.
Another way to check whether the correct file was downloaded properly or not is to check its integrity which is essential for security whenever you download files from the internet. See the below section on how to check the file integrity.
How to Verify a Downloaded File Integrity
Once the file is downloaded, confirming its integrity is an important step. Websites often provide a checksum, such as SHA256, allowing you to verify the authenticity and completeness of your file. The sha256sum
command can be used to generate a checksum for your file, which should match the one provided by the source.
You can simply pass the file name to sha256sum
command, and it will print out the hash of the file. You can then compare this with the hash mentioned in the download site.
sha256sum my-downloaded-file.tar.gz

Or even better way is to copy the hash from the download site and pass it to sha256sum
command to check:
echo "d3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 my-downloaded-file.tar.gz" | sha256sum --check
if the hash is correct, you will see an OK message:

If the download file hash doesn’t match the hash from the source website, you will see a “Failed” message:

If the check failed, it indicates the correct file was not downloaded. That could be due to a network error while downloading, a man-in-the-middle attack, or simply the file download was not complete for whatever reason.
Ensuring the Correct File Location
Yes, it could be that simple sometimes. An incorrect file path will result in an error. Ensure that the file is present in the directory from which you are attempting the extraction.
Check which directory you are in using pwd
command and execute ls
command to make sure the filename you passed to the tar command is the file in your directory.

Confirming the Correct File Extension
The file extension .tar.gz
indicates a gzip-compressed tar file archive. An error could occur if the incorrect decompression option is applied. Confirm that the file you are working with is actually a .tar.gz
file and that you are using the correct options with the tar
command.
Check User Permissions
Extraction errors can also come from limited user permissions to the file. You need to have the appropriate permissions to write to the directory where you are attempting to extract the files.
Besides, the file itself must be readable, which you can verify with ls -l
and modify if necessary with chmod
.
ls -l my-downloaded-file.tar.gz
Also, check the permissions on the directory you’re working with
ls -ld .

If you’re running commands as the root
user, the permissions may not be an issue.
Conclusion
Figuring out what went wrong when you see weird errors with tar
command can be frustrating. Often, the problem is just a bad file that didn’t download all the way. But there are a few other things you should check too. In this blog post, we went through different checks that you can perform to get the tar command working again.