Extract single file from tar archive

      No Comments on Extract single file from tar archive

Sometimes, extracting a whole tar archive can be a waste of time (and temporarily disk space).

To extract just a single file, you can use

tar -x file -zf archive.tar.gz -C /tmp/

This extracts the file file to /tmp/

Of course, most of the time the relative path to the file is not as simple as in the example above:

tar -x path/to/file -zf archive.tar.gz -C /tmp/

This creates the file /tmp/path/to/file.

To avoid the creation of the file’s relative path (or parts of it) in the target directory, use the switch --strip-components

tar -x path/to/file -zf archive.tar.gz -C /tmp/ --strip-components=2

This creates the file /tmp/file

And finally, you can use stdout redirection to achieve the same result

tar -x path/to/file -zf archive.tar.gz -O >/tmp/file

or even shorter

tar xfz archive.tar.gz path/to/file -O >/tmp/file

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.