Thursday, February 28, 2008

Extracting tar into chosen directory

Extracting tar is a common thing to do and everybody familiar with unix and linux knows how to do it. Usually the files will be extracted to the current directory. There is although one small trick to do to send the files to the desired location. Use the below command:

# tar -xvzf filename.tar.gz -C /desired/path

This command will extract(-x) verbosely(-v) tar gz(-z) file(-f) to the desired location. Hope this will help. Cheers

15 comments:

NetworkNerd007 said...

Very well written! Your topic was easy to find and well said, meaning I understood it. Too many tutorials are poorly explained and you even explained the command variables xzvf.

Thanks for this!

Escapee said...

Thanks!

Chandan Benjaram said...

here major flag is '-C' which has following man page entry:

-C directory
In c and r mode, this changes the directory before adding the following files. In x mode, change directories after opening the archive but before extracting entries from the archive.

Showtim3 said...

yeah thanks help a lot

f1dz said...

Great.. Simple is perfect.

Anonymous said...

Very useful command , thanks a lot

Anonymous said...

this command helped me a lot at right time

TheRainMaker said...

HEY...SIMPLY GREAT...


IT WORKS!!!!

ArmHead said...

Perfect, thanks for the guidance. I was able to put together this script using xargs to extract tgz files to directories names after the files.

ls *.tgz |cut -f1 -d'.' |xargs -L1 mkdir
ls *.tgz |cut -f1 -d'.' |xargs -L1 -I{} tar xzfv {}.tgz -C ./{}/

Anonymous said...

Thanks for the explanation.
Thanks to Chandan for explaining the '-C' , too.

Anonymous said...

Thank you

Anonymous said...

Tnx!

Mikhail said...

@ArmHead, the following command does the same (and removes archives if tar succeeds) in an easier way. You can even type it on a single line.

for i in *.tgz; do
mkdir -p ${i%.tgz};
tar xzfv $i -C ${i%.tgz} && rm $i;
done

However, you might need to modify it if your *.tgz files contain a whitespace, a tab or a newline character in their names.

Carlos Jacon said...

Thanks!!!

Anup Dubbewar said...

How to extract specific folder from tar.gz file

I have tried below command:
# tar -xvzf test.tar.gz MyPersonal

MyPersonal is a folder name.