Wednesday, January 28, 2009

How to untar in different directory?

The default behaviour of (un-)tar is to extract everything at its original location. Sometimes, it is required to extract/restore to some other location (eg. for comparision or analysis before putting data back to original location). I found that default tar binary provided with Solaris does not provide any option to restore/untar in different location. Hence I needed to use GNU Tar (gtar) for this purpose. In following demo, I will show how to restore in different directory:


Part 1: Creating a tar archive of a directory /tmp/A

# ls -la /tmp/A
total 6
drwxr-xr-x 2 root root 512 Jan 27 12:08 .
drwxrwxrwt 15 root sys 1536 Jan 27 12:12 ..
-rw-r--r-- 1 root root 0 Jan 27 12:08 a
-rw-r--r-- 1 root root 0 Jan 27 12:08 b
-rw-r--r-- 1 root root 0 Jan 27 12:08 c

# tar cvf /dev/rmt/14 /tmp/A
a /tmp/A/ 0 tape blocks
a /tmp/A/a 0 tape blocks
a /tmp/A/b 0 tape blocks
a /tmp/A/c 0 tape blocks

# tar tvf /dev/rmt/14
drwxr-xr-x 0/0 0 Jan 27 12:08 2009 /tmp/A/
-rw-r--r-- 0/0 0 Jan 27 12:08 2009 /tmp/A/a
-rw-r--r-- 0/0 0 Jan 27 12:08 2009 /tmp/A/b
-rw-r--r-- 0/0 0 Jan 27 12:08 2009 /tmp/A/c
===========================================================================
Part 2: Extracting tar archive - to original location (i.e. /tmp/A)

# tar xvf /dev/rmt/14
x /tmp/A, 0 bytes, 0 tape blocks
x /tmp/A/a, 0 bytes, 0 tape blocks
x /tmp/A/b, 0 bytes, 0 tape blocks
x /tmp/A/c, 0 bytes, 0 tape blocks


===========================================================================
Part 3: Extracting tar archive - to Different location (i.e. /tmp/B)
# gtar -C /tmp/B -xvf /dev/rmt/14
/tmp/A/
gtar: Removing leading `/' from member names
/tmp/A/a
/tmp/A/b
/tmp/A/c


# ls -latrR /tmp/B
/tmp/B:
total 8
drwxrwxrwt 14 root sys 1536 Jan 28 10:25 ..
drwxr-xr-x 3 root root 512 Jan 28 10:26 .
drwxr-xr-x 3 root root 512 Jan 28 10:26 tmp

/tmp/B/tmp:
total 6
drwxr-xr-x 2 root root 512 Jan 27 12:08 A
drwxr-xr-x 3 root root 512 Jan 28 10:26 ..
drwxr-xr-x 3 root root 512 Jan 28 10:26 .

/tmp/B/tmp/A:
total 4
-rw-r--r-- 1 root root 0 Jan 27 12:08 a
-rw-r--r-- 1 root root 0 Jan 27 12:08 b
-rw-r--r-- 1 root root 0 Jan 27 12:08 c
drwxr-xr-x 2 root root 512 Jan 27 12:08 .
drwxr-xr-x 3 root root 512 Jan 28 10:26 ..

Note: gtar is located at /usr/sfw/bin/gtar in Solaris 9 and Solaris 10.

8 comments:

ocp said...

Excellent job.

ocp said...

excellent job

chandu said...

its worked perfectly, very good job, thanks

Anonymous said...

Very good.. very useful

naga said...

Good Job Dude.It helped me a lot.

Clueless Forever said...

I was trying the same but gtar was not installed. So I had to use pax:

pax -r -s,/tmp,/home/me, -f tarfile.tar

This will change the base directory of extracted files from /tmp to /home/me i.e. a file which would have been extracted to /tmp/A/a would now be extracted to /home/me/A/a. I think /home/me should be pre-existing (haven't tried otherwise).

Anonymous said...

i am trying to untar file in diffrent dir but it wont work for me. no idea what i did wrong.

tar -C /sam -xvf fas.tar
tar: C: unknown function modifier
Usage: tar {c|r|t|u|x}[BDeEFhilmnopPqTvw@[0-7]][bfk][X...] [blocksize] [tarfile] [size] [exclude-file...] {file | -I include-file | -C directory file}...

Unix Geek said...

@Anonymous: I see you used tar while I mentioned "gtar" command to use for this purpose. Hope this helps.