2

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

chepner
  • 497,756
  • 71
  • 530
  • 681
user9924807
  • 707
  • 5
  • 22
  • Edited the title and tags. You are already successfully decompressing a file to get an archive; your question is about extracting the contents of the archive. – chepner Aug 01 '18 at 15:24

4 Answers4

3

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
vishnu narayanan
  • 3,813
  • 2
  • 24
  • 28
3

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 -)
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
0

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

7 Reeds
  • 2,419
  • 3
  • 32
  • 64
0

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.

pitseeker
  • 2,535
  • 1
  • 27
  • 33
  • `gunzip < $tar | tar xf - -C /another/target/directory` returns the error: `tar: 2 file(s) not extracted` on Solaris 10 – user9924807 Aug 01 '18 at 15:11