Harshu
Harshu

Reputation: 91

Untarring a file with limited directory hierarchy

I have created a tar file in terminal. Let's say I am currently in a directory test7. I am creating a tar in my current directory of a file which is inside another directory (test8).

tar -czvf example.tgz ../test8/a/b

 

output:

example.tgz

Now, I untar this file using the following command :

tar -xzvf example.tgz

 

I get the result and a directory named as test8 is produced.

$) cd test8
$) ls
-> a 
$) cd a
$) ls
-> b

Now I can go inside the directory b and see my files.

 

I want the output to be only the directory a and inside of which b will be present i.e.

Output after untarring that I want should be of this hierarchy : a/b

But not: test8/a/b

 

Can anyone please help me out with this ?

I have been through the man page of tar but couldn't get much help from it.

Upvotes: 1

Views: 69

Answers (1)

Mazhar MIK
Mazhar MIK

Reputation: 1172

You can use -C flag while tarring. It is mentioned in the man page of tar.

-C, --directory=DIR
change to directory DIR

So, you can use this as follows :

tar -czvf example.tgz -C  ../test8  a/b

Now, if you untar example.tgz. It will have hierarchy as a/b .

Hope that helps.

Upvotes: 2

Related Questions