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

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

tar -xvzf community_images.tar.gz

Also, to extract in a specific directory

for eg. to extract the archive into a custom my_images directory .

tar -xvzf community_images.tar.gz -C my_images

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.
  • C: means change to directory DIR. In our example, DIR is my_images.

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.


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).