M BUZZ CRAZE NEWS
// general

Untarring a file with limited hierarchy

By David Jones

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/bBut 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.

1 Answer

You can use the -C option to effectively change to the target directory

Ex. instead of

$ tar cvf example.tar ../Apple/Banana/Papaya
tar: Removing leading `../' from member names
../Apple/Banana/Papaya/
../Apple/Banana/Papaya/Papaya.md

use

$ tar cvf example.tar -C ../Apple Banana/Papaya
Banana/Papaya/
Banana/Papaya/Papaya.md

Alternatively you can remove leading directories upon extraction using the --strip-components option:

tar --strip-components=1 -xzvf example.tgz
3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy