Operating System - HP-UX
1833461 Members
2987 Online
110052 Solutions
New Discussion

Re: How to extract files into a perticular directory from a tar file

 
yatin
Frequent Advisor

How to extract files into a perticular directory from a tar file

How to extract files into a perticular directory from a tar file
14 REPLIES 14
Sanjay Kumar Suri
Honored Contributor

Re: How to extract files into a perticular directory from a tar file

cd /dir1
tar xvf backup.tar

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Shaikh Imran
Honored Contributor

Re: How to extract files into a perticular directory from a tar file

hi,

Go to the directory where you want to extract
the tar file &
use:
#tar -xvf file.tar
where file.tar is present in that directory.

Regards,
I'll sleep when i am dead.
yatin
Frequent Advisor

Re: How to extract files into a perticular directory from a tar file

Cant i do that with a single command like tar -C(in Linux)
Ravi_8
Honored Contributor

Re: How to extract files into a perticular directory from a tar file

Hi,

assume that your tar file is /tmp/a.tar

you need to extract that in directory /usr/ravi

#mkdir /usr/ravi
#cd /usr/ravi
#tar xvf /tmp/a.tar .

never give up
Sanjay Kumar Suri
Honored Contributor

Re: How to extract files into a perticular directory from a tar file

Although there is option -C in man tar which states:

-C directory causes tar to perform a chdir() to directory (see chdir(2)). Subsequent file and -C directory arguments are relative to directory. This allows multiple
directories not related by a close or common parent to be archived using short relative path names.

However I couldn't succeed in using this. I am trying.

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Muthukumar_5
Honored Contributor

Re: How to extract files into a perticular directory from a tar file

Hai,

tar with -C option is used to do operation on a file located in another directory and make tar operations on that directory with going to that.

pwd /
tar file is located over /home/test/test.tar
then,

[/] tar -C /home/test -xvf /home/test/test.tar

It will archieve files over at /home/test/test directory

This operation is same to hp-ux and linux too.

Regards
Muthu
Easy to suggest when don't know about the problem!
yatin
Frequent Advisor

Re: How to extract files into a perticular directory from a tar file

ta -C doesnt work as it works in Linux
Mei Jiao
Respected Contributor

Re: How to extract files into a perticular directory from a tar file

I tried the tar with -C as well, it doesn't work as what you want it to be.

Please cd into the directory and do the 'tar -xvf ' then.
Muthukumar_5
Honored Contributor

Re: How to extract files into a perticular directory from a tar file

This is the error (all) will get when you try tar -C on linux as,

tar: You must specify one of the `-Acdtrux' options
Try `tar --help' for more information.

-C is used to change the directory, as
-C, --directory=DIR change to directory DIR

So it is used to reduce to change directory to operate the tar.
It is same as,
cd /home/test;tar xvf test.tar
tar -C /home/test -xvf /home/test/test.tar

Regards
Muthukumar.


Easy to suggest when don't know about the problem!
ranganath ramachandra
Esteemed Contributor

Re: How to extract files into a perticular directory from a tar file

i guess on hpux, files can only be restored to the absolute paths recorded in the archive (maybe the vendor doesnt want you to use the archive this way?).

the hpux tar man page describes the -C option as useful only during creation of the tar file. but beware, it does not guarantee that relative pathnames will be used.

i dont know if you could use the chroot(1m) command (/usr/sbin/chroot) to achieve this. i tried and failed, but maybe i made some mistake.

for your case, a workaround is to untar with -C on a linux box and re-archive with *relative pathnames*.

the long-term solution would be to
- make sure you (or the vendor) create the archive with relative pathnames.
- file an enhancement request on hpux tar
 
--
ranga
hp-ux 11i v3[i work for hpe]

Accept or Kudo

Mike Stroyan
Honored Contributor

Re: How to extract files into a perticular directory from a tar file

You can use the pax command to extract files from a tar file and use the -s option to make substitutions in the file paths.

% tar cvf group.tar /etc/group
a /etc/group 1 blocks
% pax -s@/@./@ -vrf group.tar
USTAR format archive
./etc/group

Bill Hassell
Honored Contributor

Re: How to extract files into a perticular directory from a tar file

tar on HP-UX cannot remove the leading / from archives on tape. It always restores files and directories exactly as they were saved. This is true for some other flavors of Unit too. That's why a tar interchange backup should never specify / as part of the path (ie, cd to the directory of interest, then specify the files/diectories to backup or just a *). This is the most portable way to interchange data with tar. However, in HP-UX, you can use pax to read tar tapes and extract the files using -s.


Bill Hassell, sysadmin
Cesare Salvioni
Trusted Contributor

Re: How to extract files into a perticular directory from a tar file

hi
if the files tared in tar file start are absolute path you can use the chroot following these steps

mkdir /tmp/test_tar
cp /sbin/tar /tmp/test_tar
cp tarfile.tar /tmp/test_tar
chroot /tmp/test_tar ./tar xvf tarfile.tar

If you copy the /usr/bin/tar instead of the /sbin/tar remember this file is dynamically linked, so you will need also shared library (.sl). In this case last command above would generate an error like
cannot find /usr/lib/dl.sl
copy the missing file to /tmp/test_tar/usr/lib and rerun, then copy the new file missing until u get all the required files

The idea is to recreate a minimum structure down the directory /tmp/test_tar

hope it helps
Alzhy
Honored Contributor

Re: How to extract files into a perticular directory from a tar file

Aside from the "chroot technique", you can also try using GNU tar. See man pages for options.
Hakuna Matata.