29

This doesn't work:

tar xf /tmp/foo.tar.gz foo/bar
tar: foo/bar: Not found in archive

It's not obvious to me what would do this beyond extracting it in place and moving the files over.

Kit Sunde
  • 4,504
  • 10
  • 31
  • 34

7 Answers7

41

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)

sr_
  • 15,384
  • 4
    I reccommend to test this behaviour with non-GNU versions of tar. Solaris manual page only mentions this option in combination of add and replace operations. Being curious I've tested it with an extract operation. Solaris' tar did not issue any error or warning and extracted the archive in the current folder =:-/ – ktf Nov 01 '11 at 16:57
  • 1
    Also doesn't work on the AIX version of tar: 'File -C not present in the archive' - the '-C' does work for creating tarballs. –  Feb 13 '15 at 04:42
  • Is it possible to swap out the main directory with a new name? – tofutim Mar 09 '16 at 18:24
10

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.

ktf
  • 2,717
8

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.

jfg956
  • 6,336
5

The command:

tar -xzvf foo.tar.gz -C /home/user/bar/

will extract the input file "foo.tar.gz", into the directory /home/user/bar, while printing the processed files.

tzabal
  • 151
1

Change the directory where you want to extract

cd /u02/restore

if location of the extract file under /u01/backup.tar then

Extract as follows:

cd /u02/restore
tar -xvf /u01/backup.tar
Teja
  • 11
0
tar -xf ancd.tar.gz my/name/file

you can give file name with ./file after tar file.

tar -xf ancd.tar.gz ./my/name/file

if it is working means you have created a tar with ./. use less command to see tar content.

less ...tar.file  
Zelda
  • 6,262
  • 1
  • 23
  • 28
0

I ran into what seems to be a similar issue and have resolved it.

The issue was in the file creation rather than the created file.

When attempting to tar up and transfer a file in dir A, I provided the path to the original file in the tar command

tar -cvf MyFile.tar /foo/bar/dir/not/needed/path/*

What I was able to do to resolve is

cd /foo/bar/dir/not/needed/
tar -cvf /tmp/MyFile.tar path*

On transferring and extracting the tarball, the required subdirs are created.

tar -xvf MyFile.tar
Thomas
  • 6,362