TAR is the GNU Tape ARchive utility. It is used to pack the contents of multiple files or directories in an archive file called a tarball. Tar can preserve directory organization including file ownership, permissions, links, as well as directory structure. To save space you can optionally enable compression with gzip, bzip2, or another external program.
For these examples I’m going to be using this sample directory which contains a few files.
/mydir /mydir/script.sh /mydir/picture.jpg /mydir/document.txt
Lets create a tarball from this directory. Run the following command.
$ tar -cvf mydir.tar mydir
Lets go over these options.
-c create tarball
-v verbose
-f output file
To extract this tarball.
$ tar -xvf mydir.tar
The new option ‘-x‘ instructs tar to extract the archive.
Add compression with gzip when creating the archive by including the -z option.
$ tar -cvzf mydir.tgz mydir
Extracting the archive is similar. Make sure to include the gzip option as well.
$ tar -xvzf mydir.tgz
You can also use the bzip2 utility to compress the archive, use the -j option.
$ tar -cvjf mydir.tbz mydir
And extract it.
$ tar -xvjf mydir.tbz
To preserve permissions use ‘-p‘ when creating as well as extracting.
$ tar -cvzpf mydir.tgz mydir
$ tar -xvzpf mydir.tgz
You may have noticed the file extensions used on the archives are different. Here is a quick rundown on the naming conventions. Understanding the extensions allows you to quickly figure out how to extract it.
*.tar (uncompressed tar archive)
*.tgz or *.tar.gz (gzip compressed tar archive)
*.tbz or *.tar.bz2 (bzip2 compressed tar archive)
You want to avoid including files in the current working directory when creating a tarball. When extracted these files will be dumped into the user’s current working directory instead of its own subdirectory, potentially overwriting files and creating a big mess.
Another helpful option to use is ‘-C
$ tar -xvzf mydir.tgz -C /tmp/testdir
A good way to check for any problems with extraction or to see exactly a tarball contains is to execute tar with the -t option. This will list the contents of a tarball and show the location they will be extracted.
$ tar -tf mydir.tgz
mydir/ mydir/script.sh mydir/picture.jpg mydir/document.txt
This will also help you avoid situations where a tarball might use an absolute path (path starting at root ‘/’) when extracting instead of a relative path (path that is relative to your current directory) like the previous examples. This is not necessarily a bad thing, but it can be dangerous if you don’t realize this before extracting, since an absolute path can install to anywhere on your system.