What command do I need to unzip/extract a .tar.gz file?
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?
08 Answers
Type man tar for more information, but this command should do the trick:
tar -xvzf community_images.tar.gzTo 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 gzipx: tar can collect files or extract them.xdoes 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 7 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
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.
At some point tar was upgraded to auto-decompress. All you need now is:
tar xf community_images.tar.gzThe 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).
7Remembering all flags for tar can be tedious. Obligatory XKCD:
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.tarSee also similar script for unzipping zip archives.
4Quick Answer:
tar -xvf <.tar file> or <.tar.xz file>
tar -xzvf <.tar.gz file>
tar -xjvf <.tar.bz2 file>[NOTE]:
-vVerbosely list files processed.-zFilter the archive through gzip.-fUse archive file or device ARCHIVE.-xExtract files from an archive.-jbzip2.- tar
-xfarchive.tar # Extract all files fromarchive.tar.
In case you are not able to extract .tar.gz file using
tar -xvzf fileName.tar.gzTry extracting using
tar xf fileName.tar.gz 1 You could do this too in first step:
gunzip community_images.tar.gz
Then you have the file: community_images.tar
Second step would be:
tar -xvf community_images.tar
And the *.tar file would be extracted.
1tar xvf file.tar.gz
- x for extract
- v for verbose (list files)
- f for filename is next
Any remotely modern version of tar should auto-detect that the archive is gzipped, and add "z" for you.