How to extract .tar.bz2 directly? (avoiding a folder creation)

4

3

I always extract this kind of files using on the route where I want those files:

# tar -xvf file.tar.bz2 

But it always creates a folder called "file" (for this example) and stores all inside it.

I've never had problem with that, but now I need to avoid that folder and simply extract everything in the route where I want all to be.

I've been reading the man tar (manual) .. but I still can't find an specific atribute that makes this.

How can I do that? (All in terminal)

AAlvz

Posted 2013-02-10T03:09:14.527

Reputation: 649

Answers

4

tar does not create a folder called file; the folder file is stored in the tarball. This is because the tarball was creating with the command

tar cf foo.tar.bz2 foo

rather than

cd foo
tar cf foo.tar.bz2 .

According to man tar, you can use the switch --strip-components (--strip-path in older versions) to override this.

To strip a single leading folder, use this command:

tar xvf file.tar.bz2 --strip-components 1

Dennis

Posted 2013-02-10T03:09:14.527

Reputation: 42 934

Great! It worked perfectly! .. If I have more than a single folder inside the .tar file and use the --strip-components 1 what does it do? .. or can I specify which folder to unpack? – AAlvz – 2013-02-10T03:26:38.167

1It removes the first component of every file, i.e., foo/bar/baz will get extracted to ./bar/baz. If that's not desired, you have to use several tar commands. Before unpacking, you can execute tar tf file.tar.bz2 to see exactly where the files would get extracted. – Dennis – 2013-02-10T03:36:56.210