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:

  1. 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!

    ReplyDelete
  2. 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.

    ReplyDelete
  3. AnonymousJuly 14, 2011

    Great.. Simple is perfect.

    ReplyDelete
  4. Very useful command , thanks a lot

    ReplyDelete
  5. this command helped me a lot at right time

    ReplyDelete
  6. TheRainMakerJanuary 23, 2012

    HEY...SIMPLY GREAT...


    IT WORKS!!!!

    ReplyDelete
  7. 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 ./{}/

    ReplyDelete
  8. AnonymousMay 12, 2012

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

    ReplyDelete
  9. AnonymousMay 30, 2012

    Thank you

    ReplyDelete
  10. @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.

    ReplyDelete
  11. 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.

    ReplyDelete