LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-09-2012, 08:12 AM   #1
mmhs
Member
 
Registered: Oct 2010
Posts: 99

Rep: Reputation: 1
tar absolute and relative path


hi guys

i have a problem with tar command

i use absolute path to make a backup

tar cvf backup /home/user

and when i try to restore backup it doesn't destroy my files
tar xvf backup

it makes a directory with the name of home in my current directory although i've heard if we use absolute path to make a backup when we want to restore files they will extract in the same absolute path

for example:

if i make a backup with absolute path with this command

tar cvf backup /home/user

i think when i want to restore the backup
backup will restore exactly in /home/user and destroy all user files
however when i tested that it only makes a directory with the name of home in my current directory !!

is that right ??


another example :
according to this example

for backing up, you use
[/backups]# tar -cvf backup /home/user
& then you restore using
[/backups]# tar -xvf backup
the files will be restored to /home & not /backups,
but my files restored exactly in /backup a new directory with the name of home

Last edited by mmhs; 08-09-2012 at 08:18 AM.
 
Old 08-09-2012, 08:36 AM   #2
Mr. Alex
Senior Member
 
Registered: May 2010
Distribution: No more Linux. Done with it.
Posts: 1,238

Rep: Reputation: Disabled
Quote:
Originally Posted by mmhs View Post
i think when i want to restore the backup
backup will restore exactly in /home/user and destroy all user files
however when i tested that it only makes a directory with the name of home in my current directory !!

is that right ??
Yes. But here's how you specify output dir.
 
Old 08-09-2012, 09:13 AM   #3
mmhs
Member
 
Registered: Oct 2010
Posts: 99

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by Mr. Alex View Post
bu it did not happen when i restore a tar file which was made with absolute path it didn't destroy my files !
 
Old 08-09-2012, 09:19 AM   #4
deadeyes
Member
 
Registered: Aug 2006
Posts: 609

Rep: Reputation: 79
Quote:
Originally Posted by mmhs View Post
bu it did not happen when i restore a tar file which was made with absolute path it didn't destroy my files !
You probably need -C / when extracting.
That will extract any dirs/subdirs/content directly under /.

tar tvf filename.tar.gz

This should show entries like
your/path/backed/up

Without slash at the start of the line.
 
Old 08-09-2012, 09:38 AM   #5
mmhs
Member
 
Registered: Oct 2010
Posts: 99

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by deadeyes View Post
You probably need -C / when extracting.
That will extract any dirs/subdirs/content directly under /.

tar tvf filename.tar.gz

This should show entries like
your/path/backed/up

Without slash at the start of the line.
did you read my question ???

i want to know the meaning of absolute path in tar !!!

it was written in a book when i use absolute path to make a backup when i want to restore the backup all file will restore in the same absolute path

e.g
for backing up, i use
[/backups]# tar -cvf backup /home/user
& then i restore using
[/backups]# tar -xvf backup
the files will be restored to /home & not /backups,

bu it did not happen for me ! my question is why ???

when i restored backup with above command all files restored in /backup instead of /home/user !!!! why ??

Last edited by mmhs; 08-09-2012 at 09:45 AM.
 
Old 08-09-2012, 10:01 AM   #6
Mr. Alex
Senior Member
 
Registered: May 2010
Distribution: No more Linux. Done with it.
Posts: 1,238

Rep: Reputation: Disabled
Quote:
Originally Posted by mmhs View Post
bu it did not happen for me ! my question is why ???

when i restored backup with above command all files restored in /backup instead of /home/user !!!! why ??
Because GNUtar acts this way I guess... Extraction goes to current directory by default.
 
Old 08-09-2012, 10:05 AM   #7
Mr. Alex
Senior Member
 
Registered: May 2010
Distribution: No more Linux. Done with it.
Posts: 1,238

Rep: Reputation: Disabled
http://lowfatlinux.com/linux-tar.html
Quote:
tar xvf panda.tar Extract files from panda.tar.

This will copy all the files from the panda.tar file into the current directory. When a tar file is created, it can bundle up all the files in a directory, as well as any subdirectories and the files in them. So when you're extracting a tar file, keep in mind that you might end up with some new subdirectories in the current directory.
 
Old 08-09-2012, 10:10 AM   #8
deadeyes
Member
 
Registered: Aug 2006
Posts: 609

Rep: Reputation: 79
Quote:
Originally Posted by mmhs View Post
did you read my question ???

i want to know the meaning of absolute path in tar !!!

it was written in a book when i use absolute path to make a backup when i want to restore the backup all file will restore in the same absolute path

e.g
for backing up, i use
[/backups]# tar -cvf backup /home/user
& then i restore using
[/backups]# tar -xvf backup
the files will be restored to /home & not /backups,

bu it did not happen for me ! my question is why ???

when i restored backup with above command all files restored in /backup instead of /home/user !!!! why ??
tar tvf filename.tar.gz will show you what path components are in the path.

-C specifies the directory to start with. So each entry in the tar tvf ... listing will be added to the path specified with -C.

Copy from man page(this way you can remove part of the path taking into the tar tvf output):
Code:
       --strip-components NUMBER, --strip-path NUMBER
	      strip NUMBER  of	leading	 components  from  file	 names	before
	      extraction

	      (1) tar-1.14 uses --strip-path, tar-1.14.90+ uses --strip-compo-
	      nents
Please post tar tvf output (only a few lines is ok).
 
Old 08-09-2012, 10:21 AM   #9
Mr. Alex
Senior Member
 
Registered: May 2010
Distribution: No more Linux. Done with it.
Posts: 1,238

Rep: Reputation: Disabled
Quote:
Originally Posted by deadeyes View Post
Please post tar tvf output (only a few lines is ok).
It's just that his source absolute path becomes relative in tar archive (for extraction), that's pretty much covers it.
 
Old 08-09-2012, 10:50 AM   #10
mmhs
Member
 
Registered: Oct 2010
Posts: 99

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by Mr. Alex View Post
http://docstore.mik.ua/orelly/unix/upt/ch20_10.htm

see here

One problem with most versions of tar : it can't change a file's pathname when restoring. Let's say that you put your home directory in an archive (tape or otherwise) with a command like this:

%

tar c /home/mike

What will these files be named when you restore them, either on your own system or on some other system? They will have exactly the same pathnames that they had originally. So if /home/mike already exists, it will be destroyed


Tarfiles should not normally be created with absolute pathnames, only
with relative pathnames. Do not type "tar c /path/name" to create a tar
archive, type "(cd /path/name; tar c .)" instead.

Last edited by mmhs; 08-09-2012 at 11:02 AM.
 
Old 08-09-2012, 11:08 AM   #11
Mr. Alex
Senior Member
 
Registered: May 2010
Distribution: No more Linux. Done with it.
Posts: 1,238

Rep: Reputation: Disabled
The book's name is "UNIX power tools". As you probably know, there are several versions of TAR. UNIX and GNU is not the same.

Plus,
By Jerry Peek, Tim O'Reilly & Mike Loukides; ISBN 1-56592-260-3, 1120 pages.
Second Edition Edition, August 1997.


Plus,
your URL also says:
Use GNU tar (on the CD-ROM). It can ignore the leading / as it extracts files.
You use GNU tar, don't you? If "/" in the beginning is removed, path becomes relative. It is then extracted into current dir by default and path in tar archive concatenates with current dir's path. Deal with it.

Last edited by Mr. Alex; 08-09-2012 at 11:11 AM.
 
1 members found this post helpful.
Old 08-09-2012, 11:13 AM   #12
Mr. Alex
Senior Member
 
Registered: May 2010
Distribution: No more Linux. Done with it.
Posts: 1,238

Rep: Reputation: Disabled
Quote:
Originally Posted by deadeyes View Post
You probably need -C / when extracting.
That will extract any dirs/subdirs/content directly under /.

tar tvf filename.tar.gz
You don't need "v" option here. Just "tf".
 
Old 08-09-2012, 01:20 PM   #13
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,967

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
In the manpage for tar, see the "-P" (--absolute-names) option. You would need to have used that when creating the archive.
 
Old 08-09-2012, 09:09 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
As above:
by default, gnu tar drops the leading '/' when creating an archive.
You can use tar tf ... to see this.
You then have the option of (default) restore into current dir, OR use a switch to specify where to anchor the restore.
Your choice.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change Absolute path to Relative path sahil.jammu Linux - Newbie 2 05-31-2011 05:57 AM
java applet not loading image with relative path but loads image with absolute path amolgupta Programming 2 07-20-2009 02:58 PM
relative to absolute path vishalbutte Programming 4 01-14-2006 04:17 PM
TAR Archive file - need relative path cmd0a0 Linux - Software 2 10-19-2005 03:57 PM


All times are GMT -5. The time now is 11:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration