1820644 Members
1972 Online
109626 Solutions
New Discussion юеВ

Question about tar

 
SOLVED
Go to solution
xujun
Advisor

Question about tar

Sometimes when I extracted a tar file,they were created under current directory,but sometimes they were created under orginal directory.

For example: tar xvf usr1.tar,the current directory is /test, the extracted files are created under /usr1, not under /test/usr1.

why?
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Question about tar

That is a function of how the tar was created.
For example:

tar cvf /dev/rmt/0m /u01

All the files will be tar'ed with an absolute path /u01/* and restoring to another directory is difficult.

If instead you first cd to /u01 and then do a
tar xcf /dev/rmt/0m . then all the files will be tar'ed as ./* and can be easily restored anywhere.

Whenever possible, avoid the absolute pathnames when making tars.
If it ain't broke, I can fix that.
steven Burgess_2
Honored Contributor

Re: Question about tar

Hi

As far as I am aware, if you create the tar file (cvf) in a directory, ie /usr1 when you extract (xvf) the file will be extracted in the location that it was tarred, not the directory you reside when perform the xvf

If this ok?

Steve
take your time and think things through
Steven Sim Kok Leong
Honored Contributor

Re: Question about tar

Hi,

This is from the tar man page:

/quote/

There is no way to restore an absolute path name to a relative position.

/unquote/

1) If you want absolute path:

Creating the archive:

# tar cvf usr1.tar /usr1

Extracting the archive:

# tar xvf usr1.tar

2) If you want relative path:

Creating the archive:

# cd /usr1
# tar cvf usr1.tar *

Extracting the archive:

# mkdir -p /test/usr1
# cd /test/usr1
# tar xvf /usr1/usr1.tar

Hope this helps. Regards.

Steven Sim Kok Leong
Sanjay_6
Honored Contributor

Re: Question about tar

Hi,

It depends on how the file was backed up. If it was backed up using absolute path name. It will be extracted using absolute path no matter where you are trying to extract upto. If this was backed up without using absolute path, they can be extracted to the current directory.

Hope this helps.

Regds
Darrell Allen
Honored Contributor

Re: Question about tar

Hi,

Before extracting a tar file, you should verify how files were added to it. Use tar tvf and look to see if absolute pathnames were used to create the tar file.

If you created the tar file using relative pathnames for the input files, tar will extract the files to locations relative to your current directory.

If you created the tar file using absolute pathnames for the input files, tar will extract the files to that absolute path.

An alternative for changing the extract destination for a tar file created with absolute pathnames is to use pax. An example is:

tar cvf /tmp/users.tar /home/users

tar xvf /tmp/users.tar
will extract files back to their original locations because the input file/dir was specified with an absolute pathname (/home/users)

pax -r -s,/home/users,/home/users2, -f /tmp/users.tar
will extract files changing /home/users to /home/users2

See the man page for more info.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
xujun
Advisor

Re: Question about tar

Thanks for Sanjay and all guys.