Intro

Most Unix (Linux, MacOS, FreeBSD etc.) and open-source tools, apps and files are shared and distributed in .tgz, .tar.gz or .tar.xz format.

tar stands for tape archive and it simply puts multiple files together.

$ touch someFile{1,2,3}
$ ls
someFile1 someFile2 someFile3
$ tar -cf someFiles.tar someFile1 someFile2 someFile2
$ ls
someFiles.tar someFile1 someFile2 someFile3

In order to save space, tar files are compressed using (mostly) gzip tool, or xz utility. Therefore after compression we have .tar.gz, .tgz or .xz archives.

.tar.gz and .tgz archives are the same things; .tgz is just an abbreviation.

Extract .tar.gz and .tgz

To extract an .tar.gz archive to the current directory run:

$ tar -zxvf someFiles.tar.gz

You can run it without “-” flag indicator. It’s same argument above.

$ tar zxvf someFiles.tar.gz

If you want to extract/unpack them to a specific directory add -C flag:

$ tar -zxvf someFiles.tar.gz -C someDir

The arguments that we used for tar tool are:

  • -z : Uncompress the archive with gzip.
  • -x : Extract to disk from the archive.
  • -v : Produce verbose output i.e. show progress and file names while extracting files.
  • -f : Read the archive from the specified file called someFiles.tar.gz.
  • -C : Extract/Unpack files in someDir directory instead of the default current directory

Info

Some general flags can be found below. For more information you can check $ man tar.

  • -c : Creates an archive
  • -x : Extract the archive
  • -f : creates an archive with given filename
  • -t : displays or lists files in an archived file
  • -u : archives and adds to an existing archive file
  • -v : Displays verbose Information
  • -A : Concatenates the archive files
  • -z : Tells tar command that creates tar file using gzip
  • -j : Filter the archive tar file using tbzip
  • -W : Verify an archive file
  • -r : Update or add file or directory in already existed .tar file

List .tar.gz and .tgz contents without Extracting

You can list the content of the archive without extracting them:

$ tar -ztvf someFiles.tar.gz
-rw-r--r--  0 user user    2048 Apr 21 17:20 someFile1
-rw-r--r--  0 user user       0 Apr 21 17:20 someFile2
-rw-r--r--  0 user user       0 Apr 21 17:20 someFile3

Extract .tar.xz

xz compression tool is available through xz-utils package in most Linux distributions. Most of the time, you’ll already have the xz-utils installed by default in your distro.

On Debian or Ubuntu you can get it with the following command:

$ sudo apt install xz-utils

To unpack a .tar.xz archive run:

$ tar -xvf someFiles.tar.xz

Bonus: Automation

If you, like me, can’t remember or don’t want to remember all those flags arguments you can simply use below function.

Typing extract-it followed by a filename will extract most archives you come across — assuming you have the packages needed to extract that type of archive.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
extract-it() {
	if [ -f $1 ] ; then
		case $1 in
			*.tar)       tar xvf $1     ;;
			*.gz)        gunzip $1      ;;
			*.tar.gz)    tar zxvf $1    ;;
			*.tgz)       tar zxvf $1    ;;
			*.txz)       tar xvf $1     ;;
			*.xz)        tar xvf $1     ;;
			*.zip)       unzip $1       ;;
			*.7z)        7z x $1        ;;
			*.tar.bz2)   tar xvjf $1    ;;
			*.tbz2)      tar xvjf $1    ;;
			*.bz2)       bunzip2 $1     ;;
			*.rar)       unrar x $1     ;;
			*.Z)         uncompress $1  ;;
			*)           echo "Unable to extract <$1>'" ;;
		esac
	else
			echo "'$1' is not a valid file"
	fi
}

You can add it to your shell configuration file like .zshrc, .bashrc, config.fish etc. You can find the example in my dotfiles, in the functions file:

The credit goes to link.

All done!