[linux] Tar error: Unexpected EOF in archive

I tar a directory full of JPEG images:

tar cvfz myarchive.tar.gz mydirectory

When I untar the archive:

tar xvfz myarchive.tar.gz

I get an error:

tar: Unexpected EOF in archive

Looking at the output, it fails in the middle of one particular JPEG image.

What am I doing wrong?

This question is related to linux sysadmin

The answer is


I had a similar error, but in my case the cause was file renaming. I was creating a gzipped file file1.tar.gz and repeatedly updating it in another tarfile with tar -uvf ./combined.tar ./file1.tar.gz. I got the unexpected EOF error when after untarring combined.tar and trying to untar file1.tar.gz.

I noticed there was a difference in the output of file before and after tarring:

$file file1.tar.gz
file1.tar.gz: gzip compressed data, was "file1.tar", last modified: Mon Jul 29 12:00:00 2019, from Unix
$tar xvf combined.tar
$file file1.tar.gz
file1.tar.gz: gzip compressed data, was "file_old.tar", last modified: Mon Jul 29 12:00:00 2019, from Unix

So, it appears that the file had a different name when I originally created combined.tar, and using the tar update function doesn't overwrite the metadata for the gzipped filename. The solution was to recreate combined.tar from scratch instead of updating it.

I still don't know exactly what happened, since changing the name of a gzipped file doesn't normally break it.


In my case, I had started untar before the uploading of the tar file was complete.


I had a similar problem with truncated tar files being produced by a cron job and redirecting standard out to a file fixed the issue.

From talking to a colleague, cron creates a pipe and limits the amount of output that can be sent to standard out. I fixed mine by removing -v from my tar command, making it much less verbose and keeping the error output in the same spot as the rest of my cron jobs. If you need the verbose tar output, you'll need to redirect to a file, though.


May be you have ftped the file in ascii mode instead of binary mode ? If not, this might help.

$ gunzip myarchive.tar.gz

And then untar the resulting tar file using

$ tar xvf myarchive.tar

Hope this helps.