tar is a UNIX utility program which can be used to package and unpackage files using the
.tar format. It is commonly used along with
gzip to produce compressed archives (usually with the
What About .tar.gz Files?
.tar.gz files are files which have been packaged by tar, and then compressed with
gzip. Fortunately, tar can both decompress and unpackage in one step.
To decompress a
$ tar -xvzf my_file.tar.gz
f must be the last option in the group as the filename must come after it.
To compress all the files in the current directory into a
$ tar -cvzf my_archive.tar.gz *
The above options are:
|v||Verbose output. This is not strictly needed, but is nice to see what files are being compressed/decompressed.|
|f||The file to decompress and unpackage. This must be the last argument in the group, and the file name must follow directly after.|
What If I'm Lazy?
tar's long history, it happens to support a variety of different option styles. You can infact provide old style options that do not contain a hyphen
- (if you want to read more about the history of the option styles, see here). Aside from no hyphen, they have the same meaning as the above short style options.
To decompress an archive in as little typing as possible:
$ tar xf my_archive.tar.gz
Note that lack of a hyphen in-front of
xf. We also drop of
z because they are not strictly needed.
To compress an archive in as little typing as possible:
$ tar cf my_archive.tar.gz *
Decompress To A Specific Folder
tar will decompress the files into the current working directory. Sometimes you will want to specify the folder manually. To do this with the GNU version for tar, you can use the
$ tar -xvzf my_archive.tar.gz -C destination_folder/
destination_folder has to already exist, tar will not create it for you.
--directory is the long name for
What About .tar.xz Files?
tar can also decompress and extract
.tar.xz files. The
-J flag can be used to specifically deal with
$ tar -xJf file.tar.xz
Also note that more recent versions of tar can automatically workout the compression format for you! Just use:
$ tar xf file.tar.xz