user9924807
user9924807

Reputation: 787

Extract from tar file to different directory in Bash

I'm new to Bash and trying to unzip a tarball. Code so far:

#!/bin/bash
tar="/cdrom/java/jre1-8u181-x64tar.gz"

# Unpack tarball
gunzip < $tar | tar xf -

This extracts the archive in current directory. How can I specify a location?

Using Solaris 10, Bash 3.2.51

Upvotes: 2

Views: 4837

Answers (4)

7 Reeds
7 Reeds

Reputation: 2539

The -xf part of tar means to extract into the "f" file. try changing the tar command to something like

Edit

...| tar -xf - -C /path/to/your/desired/result/folder

sorry, @pitseeker is correct. The -C option tells tar to change directory then do the extract

Upvotes: 0

Mark Setchell
Mark Setchell

Reputation: 207365

This works pretty well everywhere - including Solaris, and as you only change directory in a sub-shell, it doesn't affect your location in the current session:

gunzip < $tar | ( cd /some/where/else && tar xf -)

Upvotes: 4

pitseeker
pitseeker

Reputation: 2543

As you wrote your command is unpacking in the current directory:

gunzip < $tar | tar xf -

Add the "-C" option to give it an alternate target directory:

gunzip < $tar | tar xf - -C /another/target/directory

Note that the Solaris tar does not understand the --directory option.
See the Solaris tar manpage.

Just for the sake of completeness if you have Gnu-Tar (which is available for Solaris too) you can use this simpler command (which unzips and unpacks in one go):

tar xzf $tar -C /another/target/directory

On a side note:
many people use a leading dash for the tar command parameters. That is redundant.
See the answers to this question if you are interested.

Upvotes: 0

vishnu narayanan
vishnu narayanan

Reputation: 3923

To extract the file to a specific directory

gunzip < $tar | tar -xf - --directory /path/to/extract/to

or

gunzip < $tar | tar -xf - -C /path/to/extract/to

Upvotes: 4

Related Questions