How to use the tar Command

In this tutorial, we will take you through how to utilize the tar command so you can archive and extract data.

How to use tar command

You will recognize tar files by the filename extension .tar. A tar file typically consists of many files and directories and is often referred to as a tarball. The name tar came from the term tape archive, as it was initially developed for writing data to magnetic tape.

The tar command can both create and extract tar archive files. In addition, many options can alter the actions of the command. For example, you can tell the command to use gzip compression or delete the original files once archiving is complete. It is a very handy tool which I recommend learning if you plan to use a Unix or Linux distribution a lot.

This tutorial goes through a vast range of different topics such as installing tar, creating archives, extracting archives, using various compression programs, and much more. We cover most of the common ways you can use this command.

Table of Contents

Installing the tar Package

The tar package should already be installed on your Linux-based distribution. However, you can easily install it with a single command line if not already installed

1. To install the tar package for Ubuntu or Debian, simply enter the following command.

sudo apt install tar

2. If you are running CentOS or Fedora, you need to run the command below.

sudo yum install tar

tar Command Syntax

The typical syntax of the tar command is below. However, there are times when the syntax will differ slightly due to different options being used. For example, options that affect the operation include extracting or creating archives.

tar [OPTIONS]… [ARCHIVE-NAME] [FILES]…

Options are where you place a wide number of options to change the default behavior of the tar command. For example, to create an archive, you will use -c or --create here.

Archive-name is where you enter the name of the tar archive you wish to create or extract.

Files are where you enter either the files or directories that you wish to be archived or extracted.

tar Command Options

There are many options that you can use with the tar command. Below is a summary of the most common options.

  • -c or --create enables the creation of a new archive file.
  • -x, --extract, or --get will extract files from the specified archive. You can further specify specific files to extract from the archive.
  • -f or --file allows you to specify the name of the archive file.
  • -v or --verbose enables feedback on how the tar command progresses with its task.
  • -A, –-catenate, --concatenate allows you to append an archive to another archive.
  • -r or –-append allows you to append files to an existing archive.
  • -u or --update only append files newer than copy in archive.
  • -t or --list will list the contents of a valid tar archive.
  • -z or --gzip will filter the archive through gzip compression.
  • -j or --bzip2 will filter the archive through bzip2 compression.
  • -W or --verify will try and verify the archive after creation.

Below we explain some of these options in further detail and provide useful examples.

Using the tar Command

The tar command is incredibly useful for archiving and extracting tar archives. Luckily, it is an easy tool to use and will not take very long to understand fully.

1. To prepare for this guide, we created an example folder filled with a range of different files and a subdirectory. You can create a similar setup or download our example files using the wget command.

wget https://files.pimylifeup.com/tarcommand/example.tar

2. You can now extract our tar file and start running through some of the examples in this tutorial.

tar -xvf example.tar example/

Create an Uncompressed tar Archive

The most straightforward use of the command is creating an uncompressed tarball. However, we will need to use a few different options to do this correctly.

If you use the shorthand way of writing options, we will use the -c, -v, and -f options. The command should look something like below.

tar -cvf example.tar example/

Alternatively, the command will look similar to our example below if you use the longer way of writing options such as --create, --verbose, and --file=.

tar --create --verbose --file=example.tar example/

The output from the above examples should look similar to ours below. You can turn the output off by simply removing the -v or --verbose option. Files are extracted to the current working directory unless specified.

example/
example/test.txt
example/file.php
example/index.html

You can check if the archive file exists by using the ls command. You should get an output similar to the one below.

example  example.tar

Create a gzip tar Archive

By default, tar will not compress the archive. Luckily there are quite a few compression programs that you can use. For example, a file that has been compressed using gzip will have the extension .gz.

To use the popular gzip compression format, simply use the -z or --gzip option.

tar -cvzf example.tar.gz example/

Use the ls command to view the newly created gzip archive file. The output should be similar to the example below.

example  example.tar.gz

Create a bz2 tar Archive

Using bz2 compression is another option for making your archive file smaller. Bz2 is also known as bzip2 and is a free and open-source compression program. Files that have been compressed using bz2 will have the extension .bz2.

To use the bz2 compression file format, simply add the -j or --bzip2 option.

tar -cvjf example.tar.bz2 example/

After running the command, you can see if our new archive exists by using the ls command. You should see our directory and the bz2 file.

example  example.tar.bz2

Add Files or Directories to a tar Archive

It is possible to add files or directories to an existing archive file. Simply use the -r or --append option to add to an existing archive.

tar -vrf example.tar newfile.txt

To view whether tar successfully added the file to the archive, you can use the -t or --list option.

tar -tf example.tar

In the output, you should see the newly added file or directory. You can use -v or --verbose for extra information such as permissions, user, group, and a timestamp.

example/
example/test.txt
example/file.php
example/index.html
newfile.txt

Adding files will only work on non-compressed archives. So, if you try adding files or directories that have been compressed using gzip, bz2, or any other compression software, then it simply will not work. You will get an error similar to the one below.

tar: Cannot update compressed archives
tar: Error is not recoverable: exiting now

Delete Files After Archive

If you want the original files removed once the archive has been created, you can use the --remove-files option. I would not use this option and instead remove the files manually once I confirm the archive is valid and not corrupted.

tar --remove-files -cvf example.tar example/

Using the ls command, you can confirm that the example folder has been deleted and an archive called example.tar exists.

List Contents of a tar Archive

If you want to view the contents of an archive file, you can use the -t or --list option.

tar -tf example.tar

The output will show a list of all the files in the archive. As you can imagine, this list will get pretty long if you have an archive with lots of files and directories.

example/
example/test.txt
example/file.php
example/index.html
example/sub/
example/sub/helloworld.txt

Extract a tar Archive

One of the most important things to know about the tar command is how to extract a tar archive. Luckily extracting an archive is pretty straightforward and can be done by using the -x, --extract, or --get option.

Extracting a tar file is sometimes referred to as untar or uncompress.

To extract an uncompressed archive, simply use the command below.

tar -xvf example.tar

Since we have the -v option defined, you should get a list of all the files and directories extracted.

example/
example/test.txt
example/file.php
example/index.html
example/sub/
example/sub/helloworld.txt

When you extract an archive that has been compressed using a compression program, you will need to specify which program to use for extraction. For example, use -z or --gzip for any gzip archive. Another example is to use -j or --bzip2 for a bzip archive file.

Extract a tar Archive to a Specific Directory

There may be a time when you need to extract an archive to a specific directory, but you do not want to use the mv command. Luckily you can easily do this by specifying the directory in the tar command.

You will need to use the -C or --directory= option to specify the directory you wish to extract the files inside. For example, in the command below, we extract to our example folder untar-folder.

tar -xvf example.tar -C untar-folder

You can confirm the files and directories have been created by using the following command.

ls untar-folder

The output should display one directory named example. You should find the rest of our previously archived files and directories inside this directory.

example

Extract Certain Files from a tar Archive

Sometimes you may only want to extract specific files from the archive. Thankfully, you can do this if you know which file or files you want. Using the --list or -t command can help find the required file.

1. To untar a single file, we will be using the -x command. Do not forget to use a compression option if the archive is compressed.

So, to extract a single file from the archive, simply list it after the file name.

tar -xvf example.tar example/test.txt

2. If you have the --verbose or -v option defined, then the command will print out the names of the files that have been extracted. See the example output below for our command above.

example/test.txt

3. You can also extract multiple files by simply adding them one after another.

tar -xvf example.tar example/test.txt example/file.php

4. Lastly, you can extract files using wildcards. So, if you want to extract all files that end with .txt, simply use the star symbol followed by the extension. You will also need to use the --wildcards option.

tar --wildcards -xvf example.tar '*.txt'

5. You should next have an output listing all the text files in the archive. This output confirms it has extracted the files, but you can double-check using the ls command.

example/test.txt
example/sub/helloworld.txt

Comparing the Difference Between an Archive and File System

You can compare your tar archive with the non-archived file system versions by using the -d, --diff, or --compare options. It is useful to check that the archive matches your current directory or the specified one.

The comparison only occurs in one direction, so if a file exists in the archive but not the directory, the command will throw an error. However, tar will throw no error if a file exists in the directory but not in the archive.

To run the command, simply structure it to look similar to our example below.

tar -dvf example.tar example/

We have removed the test.txt from our directory for this example, but it remains in the archive. As you can see below, an error is thrown for the test.txt, but the rest of the files and directories are fine.

example/
example/test.txt
tar: example/test.txt: Warning: Cannot stat: No such file or directory
example/file.php
example/index.html
example/sub/
example/sub/helloworld.txt

More Help

There are many options for the tar command, and I only touched on a small portion of them. You can quickly get more information regarding the extra options by using the man command.

man tar

Use the Q key to exit the manual pages once you have finished reading.

Alternatively, you can also use the --help option for more information.

tar --help

Either of the above help commands will help give you further information on using tar. Hopefully, one of them will have the answer to your problem.

Conclusion

By now, you should have a good understanding of all the basics of the tar command. It is a very useful command when dealing with the extraction and creation of tar archives. In addition, it is convenient for packaging directories and files up for backups, moving, or distribution.

There is a ton to learn about Linux, so if you want to learn more, be sure to check out our Linux tutorials. We are forever adding more tutorials. If there is something in particular that you would like to learn, be sure to let us know.

Lastly, if you notice something incorrect or require further explaining, please let us know by leaving a comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *