What command do I need to unzip/extract a .tar.gz file?

3,197,100

Solution 1

Type man tar for more information, but this command should do the trick:

tar -xvzf community_images.tar.gz

To explain a little further, tar collected all the files into one package, community_images.tar. The gzip program applied compression, hence the gz extension. So the command does a couple things:

  • f: this must be the last flag of the command, and the tar file must be immediately after. It tells tar the name and path of the compressed file.
  • z: tells tar to decompress the archive using gzip
  • x: tar can collect files or extract them. x does the latter.
  • v: makes tar talk a lot. Verbose output shows you all the files being extracted.

To extract into a custom folder, add the -C option with a folder name of your choice:

tar -xvzf community_images.tar.gz -C some_custom_folder_name

Solution 2

If you want the files to be extracted to a particular destination you can add -C /destination/path/
Make sure you make the directory first, in this case: ~/Pictures/Community

Example:

mkdir ~/Pictures/Community
tar xf community_images.tar.gz -C /home/emmys/Pictures/Community/

You can easily memorize it if you consider telling tar to e X tract a F ile

gif of process done at terminal

Note: Remember you can search inside man pages with ?+term to look for, and then n and N to go to the next or previous instance of the term you are looking for.

Solution 3

At some point tar was upgraded to auto-decompress. All you need now is:

tar xf community_images.tar.gz

The same explanation applies:

  • f: this must be the last flag of the command, and the tar file must be immediately after. It tells tar the name and path of the compressed file.
  • x: extract the files.

Note the lack of hyphen for the command flags. This is because most versions of tar allow both gnu and bsd style options (simplistically, gnu requires a hyphen, bsd doesn't).

Solution 4

Remembering all flags for tar can be tedious. Obligatory XKCD:

enter image description here

Therefore I made my own little script in Python to do that. Quick, dirty, cp-like in usage:

#!/usr/bin/env python3
import tarfile,sys,os
tf = tarfile.open(name=sys.argv[1],mode='r:gz')
where = '.'
if len(sys.argv) == 3:
    where = sys.argv[2]
tf.extractall(path=where)
tf.close()

Use it as so:

myuntar.py archive.tar

See also similar script for unzipping zip archives.

Solution 5

Quick Answer:

tar -xvf  <.tar file> or <.tar.xz file>
tar -xzvf <.tar.gz file>
tar -xjvf <.tar.bz2 file>

[NOTE]:

  • -v Verbosely list files processed.
  • -z Filter the archive through gzip.
  • -f Use archive file or device ARCHIVE.
  • -x Extract files from an archive.
  • -j bzip2.
  • tar -xf archive.tar # Extract all files from archive.tar.
Share:
3,197,100

Related videos on Youtube

JohnB
Author by

JohnB

SOreadytohelp

Updated on September 17, 2022

Comments

  • JohnB
    JohnB over 1 year

    I received a huge .tar.gz file from a client that contains about 800 mb of image files (when uncompressed.) Our hosting company's ftp is seriously slow, so extracting all the files locally and sending them up via ftp isn't practical. I was able to ftp the .tar.gz file to our hosting site, but when I ssh into my directory and try using unzip, it gives me this error:

    [esthers@clients locations]$ unzip community_images.tar.gz
    Archive:  community_images.tar.gz
      End-of-central-directory signature not found.  Either this file is not a zipfile, or it constitutes one disk of a multi-part archive.  In the latter case the central directory and zipfile comment will be found on the last disk(s) of this archive.
    note:  community_images.tar.gz may be a plain executable, not an archive
    unzip:  cannot find zipfile directory in one of community_images.tar.gz or community_images.tar.gz.zip, and cannot find community_images.tar.gz.ZIP, period.
    

    What command do I need to use to extract all the files in a .tar.gz file?

  • djeikyb
    djeikyb over 9 years
    don't forget C for "change directory"! also, i feel it's better not to mix hyphen and non-hyphen options (especially confusing when using ps).
  • neurosnap
    neurosnap almost 9 years
    Is there any documentation on this? I've been searching all over for a reference to this, I never use the -z flag and everything gets gzipped so I've been worried if I haven't been compressing my tarballs at all, haha. Thanks.
  • djeikyb
    djeikyb almost 9 years
    @neurosnap see the gnu manual and of the bsd's manual
  • web.learner
    web.learner almost 8 years
    Probably would have been better to edit your original answer.
  • djeikyb
    djeikyb almost 8 years
    @Seth it feels/felt distinct enough to be a separate answer, especially in context of the destructive edit that provoked it. I'll rethink it after work though, might be a simpler way to merge than I originally thought.
  • Muhamed Huseinbašić
    Muhamed Huseinbašić over 7 years
    In which version?
  • Jan M.
    Jan M. about 7 years
    Why is it not -o like every other command ever?
  • djeikyb
    djeikyb about 7 years
    if it is a gzipped tar file, the first command will always work. if it does not work, you do not have a gzipped tar file. the latter command is a great recommendation for folks who don't care what compression algorithm was used on their tar file.
  • Enlico
    Enlico almost 7 years
    @djeikyb, does this simplification hold for tar.gz files only? If not, am I sure I'm not going to make a mess if use tar xf with any 'tar.*` file? BTW, the word should absolutely be spread! I'd also suggest @EmmyS to chose this as the accepted answer (after all @djeikyb, you gave both answers).
  • djeikyb
    djeikyb almost 7 years
    @EnricoMariaDeAngelis bsd tar since at least 2004 and gnu tar since at least 2010 have supported auto-selecting a decompressor. it's possible no "recent" (this century? nineties+?) version of tar requires manually selecting a decompressor.
  • mgarey
    mgarey over 6 years
    Reminds me of xckd's article about tar. I have to come back every time.
  • Nam G VU
    Nam G VU over 6 years
    I love your hint 'tar to e X tract a F ile'
  • Agargara
    Agargara about 6 years
    My mnemonic for this is: xtract ze v ***ing files.
  • ahnbizcad
    ahnbizcad almost 6 years
    would it matter if z came before x? Or would it inteligently apply the expanders in the appropriate order, regardless?
  • djeikyb
    djeikyb almost 6 years
    @ahnbizcad order does not matter for most of the flags. the -f flag accepts an argument. i prefer to put it last, but tar -f community_images.tar.gz -zxv works just fine.
  • Nick Steele
    Nick Steele over 5 years
    1+ for the animated gif
  • Videonauth
    Videonauth over 5 years
    :) only two tar flags to remember -caf for creating based on the filename and -xf for extracting (AFAIK works on every type of tar).
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy over 5 years
    @Videonauth I know, it's not that complex, but for some reason I never remember these options. Maybe because I don't deal with archives often enough.
  • Zen
    Zen over 5 years
    A tip, for BSD version of tar(OSX use it), -z flag is only required on the compress process, it will be ignored on extract process.
  • Patrick Dark
    Patrick Dark about 5 years
    The full form of this command—which may be easier to remember—is tar --extract --file="community_images.tar.gz" --gzip --verbose. The quotation marks are optional if the filename doesn’t contain spaces.
  • Patrick Dark
    Patrick Dark about 5 years
    One can also use --directory="/home/emmys/Pictures/Community/" (which I think is easier to remember than -C).
  • djeikyb
    djeikyb about 5 years
    will this only do gzipped tar files? api docs mention a mode for reading with transparent compression; would that behave more like tar xf <file>?
  • Vish
    Vish about 3 years
    This is the answer I was looking for....Everyone is talking about the tar.gz file...
  • starriet
    starriet about 2 years
    It's very important that the f flag must be the last flag. for example, -xfv assumes that the file name is 'v'. Also, we don't need to specify the z flag, because it's default.
  • Admin
    Admin almost 2 years
    +1'ed because of the xkcd