Expand archive into directories 

Date:          Thurs, Oct 23 2003 8:43 pm
Groups:        comp.os.linux.misc
>> I found that the 'tar' command has the following limitation:
>> Say my tar archive contains the following directory structure:
>> dir1/dir11
>> dir1/dir12
>> dir1/dir13
>> And, the dir1/dir12 is very big. Bigger than what the current volume can
>> hold. As a way to workaround, I created the above directory structure
>> manually, and the dir1/dir12 is actually a symbol link into a bigger
>> volume that can hold the content.
>> However, when I expend my tar archive into my created directory
>> structure, the symbol link dir1/dir12 get overwritten, and recreated as
>> a directory.
>> Is there any way for tar, or any other commands (e.g., cpio, rsync) to
>> preserve my created directory structure, expand/copy files into it, not
>> trying to recreate every directory even it is there?
>> This is only a simple example, for illustration purpose only. If tar can
>> do so, it would be greatly enhance its versatility.
> Are you preserving permissions when you pack and unpack the tar file?
> tar cpvf  filename.tar directory
> tar xpvf  filename.tar

hmm, no I didn't. I thought tar preserves permissions by default. Anyway, I gave it a try, but preserving permissions doesn't help:

$ find
./dir1
./dir1/dir11
./dir1/dir12
./dir1/dir12/aa
./dir1/dir13
$ tar cpvf test.tar dir1
dir1/
dir1/dir11/
dir1/dir12/
dir1/dir12/aa
dir1/dir13/

Now recreate dir1/dir12 as a symbol link!

rm -rf ./dir1/dir12
mkdir dirnew
ln -s ../dirnew ./dir1/dir12
$ ls -l ./dir1/dir12
lrwxrwxrwx    1 tong     tong   9 Oct 24 09:55 ./dir1/dir12 -> ../dirnew

Hmmm, yeah, it is there. Now, expand the tar.

$ tar xpvf test.tar
dir1/
dir1/dir11/
dir1/dir12/
dir1/dir12/aa
dir1/dir13/
$ ls -ld ./dir1/dir12
drwxrwx--x    2 tong     tong         4096 Oct 24 09:54 ./dir1/dir12

See, the previously manually created directory dir1/dir12 (a symbol link) is gone. Instead, it has changed back to a normal directory, which defeats what I was trying to do.

Any other suggestion? Thanks

Expand archive into directories 

NB, problem solved as of GNU tar 1.14:

$ mkdir -p dir1/dir11 dir1/dir12 dir1/dir13
$ touch dir1/dir12/aa
$ tar cvf test.tar dir1
dir1/
dir1/dir11/
dir1/dir12/
dir1/dir12/aa
dir1/dir13/
$ rm -rf ./dir1/dir12
$ mkdir dirnew
$ ln -s ../dirnew ./dir1/dir12
$ dir !$
lrwxrwxrwx  1 tong tong 9 03-26 16:10 ./dir1/dir12 -> ../dirnew/
$ tar xpvf test.tar
dir1/
dir1/dir11/
dir1/dir12/
dir1/dir12/aa
dir1/dir13/
$ ls -ld ./dir1/dir12
lrwxrwxrwx  1 tong tong 9 03-26 16:10 ./dir1/dir12 -> ../dirnew
# Now, dir link is preserved!
$ tar --version
tar (GNU tar) 1.14

Preserve file links 

$ echo abc > dir1/dir11/forg
$ ln -s ../dir11/forg dir1/dir13/flink
$ dir dir1/dir13/flink
lrwxrwxrwx  1 tong tong 13 03-26 16:27 dir1/dir13/flink -> ../dir11/forg
$ cat dir1/dir13/flink
abc
# Follow symbolic links
$ tar -h -cf t2.tar dir1/dir13
$ tar -tvf t2.tar
drwxrwx--x tong/tong         0 2006-03-26 16:27:14 dir1/dir13/
-rw-rw---- tong/tong         4 2006-03-26 16:26:30 dir1/dir13/flink
$ echo new > dir1/dir11/forg
$ tar xvf test.tar
dir1/dir13/
dir1/dir13/flink
$ dir dir1/dir13/flink
-rw-rw----  1 tong tong 4 03-26 16:26 dir1/dir13/flink
# file link is not preserved.

Preserve file links 

>> See, the previously manually created directory dir1/dir12 (a symbol link)
>> is gone. Instead, it has changed back to a normal directory, which
>> defeats what I was trying to do.
>> Any other suggestion? Thanks
> It works for me. You might try something more like this:
> Which would shown something on the order of this.

Ahh, David, based on what you showed me, I have an impression that you did not fully understand what I requested. I have no problem preserving symbol links in tar archives. I want to preserve previously manually created symbol link directory from overwriting by tar.

I am thinking that if you follow my previous post step by step, you'll get the same result. That's why I posted each step clearly. Try it, you'll understand what I meant.

If you still think it work for you by then, please post your steps clearly, your distro, and your version of tar.

Thanks.

PS. Also thanks to people who pointed out the -k switch. It is not exactly what I am looking for. It keep existing files. I do want to overwrite files, just need to preserve the directory structure.

Well, I think that it, tar can't do it. FYI, I've also invested in cpio, rsync, none of them can do what I wanted.

Preserve file links 

> If you still think it work for you by then, please post your steps
> clearly, your distro, and your version of tar.

I see what your doing. Your making a tarfile then changing the original directory structure. Then when you unpack the tarfile it is replacing the new directory structure with original that is in the tarfile. You want it to untar the archive into the new directory structure without making changes to the new directory structure.

That is something I've never tried to do.

David @netscape.net