How do I extract with tar to a different directory?
From man tar
:
-C directory
In c and r mode, this changes the directory before adding the
following files. In x mode, change directories after opening the
archive but before extracting entries from the archive.
i.e, tar xC /foo/bar -f /tmp/foo.tar.gz
should do the job.
(on FreeBSD, but GNU tar is basically the same in this respect, see "Changing the Working Directory" in its manual)
if you want to extract an tar archive elsewhere just cd to the destination directory and untar it there:
mkdir -p foo/bar
cd foo/bar
tar xzvf /tmp/foo.tar.gz
The command you've used would search the file foo/bar
in the archive and extract it.
Doing:
(cd foo/bar ; tar xf /tmp/foo.tar.gz )
would do the job.
Basically, what is does is spawning a new shell (the parentheses), in this subshell, change directory to foo/bar
and then untar the file.
You can change the ;
by a &&
to be sure the cd
works fine.