In light of new information I’ve rephrased the question. The old information is below in order to not invalidate answers and comments that were based on it.
I’ve got a tarball tarball.tar
I want to extract to destination
, a directory that is different from my current working directory, so I use tar’s -C
option, which does what I want.
However, if I try to specify files to be extracted from the tarball, the -C
option appears to be ignored. The files are extracted in my current working directory.
tar -xf tarball.tar -C destination
Tar’s version is
$ tar --version
tar (GNU tar) 1.28
Is this a bug or is my understanding of -C
wrong?
Minimal Working Example
Here’s a bash script to show the behaviour. Store (or execute) it in an empty directory
#!/bin/bash -x
set -e # Abort if any of these commands fails
touch file1 file2 file3 # Create files to be archived
tar -cf tarball.tar file1 file2 file3 # Create the archive
rm file1 file2 file3 # Remove source files
tar -tf tarball.tar # Should contain files 1–3
mkdir -p destination # Create destination directory
tar -xf tarball.tar file1 file2 -C destination # Extract two files from
#+ tarball into destination directory
ls . # Should contain only the script itself,
#+ tarball.tar, and destination
ls destination # Should contain file1 and file 2
If I execute the script, destination
is empty and ls .
returns
$ ls .
file1 file2 tarball.tar tar.sh
If I do not specify the files to be extracted (so
tar -xf tarball.tar file1 file2 -C destination
on line 9 becomes
tar -xf tarball -C destination
)
the behaviour is as expected. ls destination
shows file1 file2 file3
.
Old Question (ignore this)
I’ve got a tar archive /path/to/backup.tar.gz
that contains, among others, the directories home/bleistift2/stuff
and home/bleistift2/more_stuff
.
In order to extract these two directories to /home/bleistift2/tmp
(in the file system, not the archive), I issue the following command. My understanding is that -C
specifies the extraction location. The target directory exists.
tar -zxvf /path/to/backup.tar.gz \ # The archive to extract
home/bleistift2/stuff home/bleistift2/more_stuff \ # The contents to extract
--same-owner -C /home/bleistift2/tmp # The destination directory
However, the directories are stored as siblings to the archive, so I end up with /path/to/home/bleistift2/{stuff, more_stuff}
instead of /home/bleistift2/tmp/home/bleistift2/{stuff, more_stuff}
.
tar -xf tarball.tar -C destination file1 file2