How to Extract Tar Files in Linux
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.tarfile
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.
| |
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!