The tar command is a software utility for combining multiple files into one archive file. These files are often referred to as tarballs in the Linux / Unix community. The name originally stood for tape archive, which was it's original purpose. It has a long history and has been around since the late 1970's.

In this article we will be discussing many uses for tar with examples on how to execute those uses. A table of context has been provided for your convenience.

Basic Syntax of tar Command

The basic syntax of tar in slightly counter intuitive to most Linux beginners. This is because it specifies the destination (archive) before the source (files to be archived).

tar [OPTIONS] [ARCHIVE] [FILENAMES]

Creating a tar Archive

The most basic way to create a tar archive is using the -cf options (create, file). Here we will create a tar archive file called definitions.tar from all the files in the /home/savona/words/ folder.

$ tar cf definitions.tar /home/savona/words/
tar: Removing leading `/' from member names

The tar output is warning us that it is removing the forward slash from the filenames. This is not really an error. This is the correct behavior. If tar did not remove the leading '/' from the filenames it would extract to absolute paths. Which most of the time, is not what you want.

If you really want to create an archive with absolute paths, use the -P option.

$ tar cfP definitions.tar /home/savona/words/
$

Although this did exactly what we wanted it to, it would be nice to see the names of the files being added to the archive. To see the files being added to the archive we just need to add the -v (verbose) option.

$ tar cvf definitions.tar /home/savona/words/
tar: Removing leading `/' from member names
/home/savona/words/
/home/savona/words/b/
/home/savona/words/b/bachelor-at-arms
/home/savona/words/b/babyhouse
/home/savona/words/b/bacitracin
/home/savona/words/b/babiroussa
/home/savona/words/b/babies'-breath
/home/savona/words/b/babbitting
/home/savona/words/b/bacchants
/home/savona/words/b/bachelordom
/home/savona/words/b/baccharis
/home/savona/words/b/babylonish
/home/savona/words/b/backbiter
...OUTPUT TRUNCATED...

The way you create a tar archive will dictate the way it extracts. To clarify, if you create an archive by providing a path to files, the file directory structure will be recreated in the location the extraction is taking place.

Add Files to a tar Archive

You can add (or append) a file or directory to a tar archive after it has been created. Let's add /usr/share/dict/linux.words to our tar archive using the -r option.

$ tar rvf definitions.tar /usr/share/dict/linux.words 
/usr/share/dict/linux.words

Now let's add the the /usr/share/doc/dictd directory to definitions.tar.

$ tar rvf definitions.tar /usr/share/doc/dictd
/usr/share/doc/dictd/
/usr/share/doc/dictd/COPYING
/usr/share/doc/dictd/rfc2229.txt
/usr/share/doc/dictd/README
/usr/share/doc/dictd/security.doc
...OUTPUT TRUNCATED...

Compressing tar Archives

By default tar does not compress any data, it simply collects all the files and produces a single file. If you are creating a tar archive of a lot of small files, you may see a reduction of space depending on your file systems block size (a whole different article). There are several methods for compressing a tar archive.

Create a Compressed Archive with gzip (GNU zip)

Compressing a tar archive requires a separate utility. Here we use gzip to compress the tar archive and see big space savings. The gzip utility is a fast and efficient means of compression.

To compress a tar archive using gzip, add the -z option.

$ tar czvf definitions.tar.gz /home/savona/words/
tar: Removing leading `/' from member names
/home/savona/words/
/home/savona/words/b/
/home/savona/words/b/bachelor-at-arms
/home/savona/words/b/babyhouse
/home/savona/words/b/bacitracin
/home/savona/words/b/babiroussa
...OUTPUT TRUNCATED...

Now let's take a look at the differing file sizes between a regular tar archive and a gzipped archive.

$ ls -lrth definition*
-rw-rw-r--. 1 savona savona 1.9M Feb 18 15:56 definitions.tar
-rw-rw-r--. 1 savona savona 309K Feb 18 15:56 definitions.tar.gz

As you can see using gzip reduced the amount of disk space by over 80%.

Create a Compressed Archive with bz2 (bzip2)

The bzip2 utility is similar to gzip but it is uses a different set of algorithms. It is widely accepted that bzip2 has better compression than gzip but with the drawback of being slower.

To compress a tar archive using bz2, add the -j option.

$ tar cjvf definitions.tar.bz2 /home/savona/words/
tar: Removing leading `/' from member names
/home/savona/words/
/home/savona/words/b/
/home/savona/words/b/bachelor-at-arms
/home/savona/words/b/babyhouse
...OUTPUT TRUNCATED...

Let's take a look at the size difference between tar, gzip and bz2.

$ ls -lrth definition*
-rw-rw-r--. 1 savona savona 1.9M Feb 18 15:56 definitions.tar
-rw-rw-r--. 1 savona savona 309K Feb 18 15:56 definitions.tar.gz
-rw-rw-r--. 1 savona savona 228K Feb 18 16:08 definitions.tar.bz2

As you can see bz2 compressed the files even more. The savings here were not very noticeable.

Adding Files to Compressed Archive

The tar command does not have the functionality of adding files if the archive is compressed. This is because both gzip and bz2 are separate utilities and tar is just utilizing them. However, you can create a tar archive, add files to it, then compress it using gzip or bzip2 after the fact.

[savona@putor ~]$ tar cf def.tar /home/savona/words/a/
[savona@putor ~]$ tar rf def.tar /home/savona/words/b/
[savona@putor ~]$ tar rf def.tar /home/savona/words/f/
[savona@putor ~]$ tar rf def.tar /home/savona/words/k/
[savona@putor ~]$ ls -lh def.tar
-rw-rw-r--. 1 savona savona 440K Feb 18 16:45 def.tar
[savona@putor ~]$ gzip -k def.tar
[savona@putor ~]$ bzip2 -k def.tar
[savona@putor ~]$ ls -lh def.tar*
-rw-rw-r--. 1 savona savona 440K Feb 18 16:45 def.tar
-rw-rw-r--. 1 savona savona 74K Feb 18 16:45 def.tar.bz2
-rw-rw-r--. 1 savona savona 89K Feb 18 16:45 def.tar.gz

List Contents of an Archive

You can list the contents of an archive using the -t (--list) option.

NOTE: The -t option works with tar archives, or compressed (gzip, bz2) tar archives.

[savona@putor new]$ tar tf def.tar 
home/savona/words/a/
home/savona/words/a/abalienation
home/savona/words/a/abbreviate
home/savona/words/a/abbreviators
home/savona/words/a/abbreviature
home/savona/words/a/abdominous
...OUTPUT TRUNCATED...
Adding the -v (verbose) option will give us a long listing of the contents.
[savona@putor new]$ tar tvf def.tar 
drwxrwxr-x savona/savona 0 2019-02-18 14:06 home/savona/words/a/
-rw-rw-r-- savona/savona 290 2019-02-18 14:05 home/savona/words/a/abalienation
-rw-rw-r-- savona/savona 2750 2019-02-18 14:06 home/savona/words/a/abbreviate
-rw-rw-r-- savona/savona 278 2019-02-18 14:06 home/savona/words/a/abbreviators
-rw-rw-r-- savona/savona 453 2019-02-18 14:06 home/savona/words/a/abbreviature
-rw-rw-r-- savona/savona 469 2019-02-18 14:06 home/savona/words/a/abdominous
...OUTPUT TRUNCATED...

Extracting tar Archives

Extracting a tar archive is as simple as using the -xf (extract, file) option.

NOTE: The x option works with tar, tar.gz, and tar.bz2 files.

$ tar xf def.tar

Again, to see the files that are being extracted, add the -v (verbose) option.

$ tar xvf def.tar 
home/savona/words/a/
home/savona/words/a/abalienation
home/savona/words/a/abbreviate
home/savona/words/a/abbreviators
home/savona/words/a/abbreviature
home/savona/words/a/abdominous
home/savona/words/a/abannition
...OUTPUT TRUNCATED...

Extract Archive to a Different Directory

To extract an archive (compressed or uncompressed) to a different directory, specify the -C (--directory) options. The following command will extract the definitions.tar.gz to the /home/savona/new directory.

$ tar xvf definitions.tar.gz -C /home/savona/new/
home/savona/words/
home/savona/words/b/
home/savona/words/b/bachelor-at-arms
home/savona/words/b/babyhouse
home/savona/words/b/bacitracin
home/savona/words/b/babiroussa
...OUTPUT TRUNCATED...

NOTE: This works with compressed or uncompressed archives.

Extracting a Single File from an Archive

To extract a single file from an archive, just specify the file name after the extract statement like so:

$ tar xvf definitions.tar.gz home/savona/words/j/jackfruit
home/savona/words/j/jackfruit

Here we extracted the file named "jackfruit" from the definitions archive. When specifying a file, you have to use the full path as it is known to the archive. You can get the full path by listing out the archive with the -t option and finding your file. You can refine the output and pipe it to grep for easier searching.

$ tar tf definitions.tar.gz | grep jackfruit
home/savona/words/j/jackfruit

If your file has spaces or special characters in the name, you will want to wrap the name in double quotes.

$ tar xvf definitions.tar "jack of all trades"
jack of all trades

NOTE: This works for compressed and uncompressed archives.

Extracting Multiple Files from an Archive

You can extract multiple files from an archive by specifying them after the extract statement just as we did with the single file.

$ tar xvf definitions.tar jackfruit jacksmith jackknife
jackfruit
jackknife
jacksmith

Again, if you have spaces or special characters, wrap them in quotes:

$ tar xvf definitions.tar "jack of all trades" "Jack the Lad"
jack of all trades
Jack the Lad

Extract Files Using Wildcard Matches

You can use wildcards to extract files as well. To extract all files that begin with the word "jack" just add the --wildcards option then the pattern like so:

$ tar xvf definitions.tar --wildcards 'jack*'
jackfruit
jackknife
jacksmith
jack of all trades

Conclusion

There is a good reason tar has stood the test of time. It is a robust and feature rich utility. In this tutorial we covered the basic use of the tar command. We learned how to create a tar archive, using different kinds of compression and adding files to an existing archive. We also covered extraction, extracting to a different directory and extracting single files from a tar archive. You should have enough information to feel confident using the tar command now.

Please feel free to leave any feedback in the comments below. If you have an idea for an article please use the contact form and let us know.

Resources