I've looked around at multiple sources for this question and I know how to extract the file, but neither source told me how to state where to put the folder once it has been extracted.

I tried this:

tar -xvf tarball.tar.gz my/folder/im/extracting

When I did this it seemed to extract it as it listed out it's contents, but also followed with the error:

gzip: stdin: unexpected end of file tar: Unexpected EOF in archive tar: Error is not recoverable: exiting now

I checked the current directory but I didn't see the folder.

How can this be done or is the error preventing the creation of it in the current directory?

  • you need the z option if your file is gzipped: tar -xzvf ....
    – meuh
    Apr 2, 2016 at 14:25
  • 1
    @meuh Given the error message, Brett is using a tar implementation such as GNU tar which invokes gzip automatically when the name of the archive ends in .gz. Apr 2, 2016 at 20:36

2 Answers 2

tar -xvf tarball.tar.gz my/folder/im/extracting

This extracts the archive member my/folder/im/extracting at the location my/folder/im/extracting. If the archive member is a directory, its contents are extracted (including subdirectories, recursively).

If you want to extract to a different directory, with GNU or FreeBSD tar (so on non-embedded Linux, Cygwin, FreeBSD and OSX), you can use

tar -xvf tarball.tar.gz --transform 's!my/folder/im/extracting!somewhere/else!' my/folder/im/extracting

If you just want to put my under a different (existing) directory, you can use

tar -xvf tarball.tar.gz -C different/directory my/folder/im/extracting

The error “gzip: stdin: unexpected end of file” has nothing to do with the way you're using tar. “Unexpected end of file” means that gzip reached the end of the file but the file format indicates that there should have been more data. In other words, the file was truncated, e.g. because your download was interrupted.

  • You say that "This extracts the archive member my/folder/im/extracting at the location my/folder/im/extracting." - so does this mean if you are currently in the directory foo when you run the command it will get extracted to foo/my/folder/im/extracting ? As in the current directory? ...or just my/folder/im/extracting?
    – Brett
    Apr 7, 2016 at 16:56
  • @Brett I don't understand your question: the two alternatives are the same. If you are in the directory /path/to/foo, then the command extracts my/folder/im/extracting at the location my/folder/im/extracting, which is the same as ``/path/to/foo/my/folder/im/extracting` (relative path and absolute path). Apr 7, 2016 at 17:11
  • No, sorry I meant. Say you are in the absolute path /foo/ when you do the extract, does the folder you extract then end up in /foo/my/folder/im/extracting OR /my/folder/im/extracting?
    – Brett
    Apr 8, 2016 at 14:06
  • @Brett /foo/my/folder/im/extracting. If you wanted to extract to /my/…, you'd need to specify an absolute path: /my/…, not my/…. Apr 8, 2016 at 19:32

You can use the -C option of tar to specify a target directory for the extracted files. But that target directory must be created beforehand. tar won't create it for you.

So your commands should look like...

mkdir -p my/folder/im/extracting
tar -xvf tarball.tar.gz -C my/folder/im/extracting
  • 1
    I seen the -C option mentioned elsewhere but it was only used in the context of extracting the whole archive, so I didn't think it was of use. As for your example, it doesn't make sense. You're only noting the created directory in the command but not the folder we want to extract?
    – Brett
    Apr 2, 2016 at 14:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .