How do I unzip a tar gz archive to a specific destination?

242,687

You have two choices:

cd /root/Desktop/folder
tar xf /root/Documents/file.tar.gz

or

tar xf file.tar.gz -C /root/Desktop/folder
Share:
242,687

Related videos on Youtube

Omar Malas
Author by

Omar Malas

Updated on September 18, 2022

Comments

  • Omar Malas
    Omar Malas over 1 year

    I want to unpack a .tar.gz file to a specific directory.

    The archive file is in /root/Documents. I want to unzip it into /root/Desktop/folder. The folder structure in zipped file should be preserved in the destination directory.

  • Omar Malas
    Omar Malas over 12 years
    thanks, how about gzip -dc archive.tar.gz | tar -xf - -C /destination ?
  • AlikElzin-kilaka
    AlikElzin-kilaka over 9 years
    what -C stands for?
  • tachomi
    tachomi over 9 years
    @AlikElzin-kilaka From man tar -C, --directory DIR change to directory DIR
  • vladkras
    vladkras over 8 years
    tar -xvjf foo.tar.bz2 for those who are looking for bz2 like me
  • Patches
    Patches over 7 years
    You actually don't have to specify the compression format anymore with modern tar. I edited the answer so it will work with bz2, xz, and whatever else you want to throw at it.
  • Zoe stands with Ukraine
    Zoe stands with Ukraine over 2 years
    Both of these seem to require manually creating a directory first though :/ (Not that it's hard, it's just an annoying extra command I don't wanna have to do). oh well
  • Patches
    Patches over 2 years
    @Zoe tar -xf foobar-1.2.tgz -C /opt --one-top-level=foobar unix.stackexchange.com/a/478341/5164