Read in 6 minutes

last updated 

How to Create and Extract Archives Using the Tar Command in Linux

In this tutorial, we will show you how to use the Tar utility to extract and create tar archives through practical examples and detailed explanations of the most common Tar options.

What is Tar?

The tar command is used to create tar archives by converting a group of files into an archive. It also has ability to extract tar archives, display a list of the files included in the archive, add additional files to an existing archive, as well as various other kinds of operations.

Tar supports a vast range of compression programs such as gzip, bzip2, lzip, lzma, lzop, xz and compress. When creating compressed tar archives it is an accepted convention to append the compressor suffix to the archive file name. For example, if an archive has been compressed with gzip it should be named archive.tar.gz.

Tar was originally designed for creating archives to store files on magnetic tape which is why it has its name “Tape ARchive”.

There are two versions of tar, BSD tar and GNU tar with some functional differences between. Most Linux systems comes with GNU tar pre-installed by default.

Tar Command Syntax

Before going into how to use the tar command, let’s start by reviewing the basic syntax.

tar [OPERATION_AND_OPTIONS] [ARCHIVE_NAME] [FILE_NAME(s)]
  • OPERATION - Only one operation argument is allowed and required. The most frequently used operations are:
    • --create (-c) - Create a new tar archive.
    • --extract (-x) - Extract the entire archive or one or more files from an archive.
    • --list (-t) - Display a list of the files included in the archive
  • OPTIONS - The most frequently used operations are:
    • --verbose (-v) - Show the files being processed by the tar command.
    • --file=archive=name (-f archive-name) - Specifies the archive file name.
  • ARCHIVE_NAME - The name of the archive.
  • FILE_NAME(s) - A space-separated list of file names to be extracted. If not provided the entire archive is extracted.

When executing tar commands, you can use the long or the short form of the tar operations and options. The long forms are more readable while the short forms are faster to type. The long form options are prefixed with a double dash (--). The short form options are prefixed with a single dash (-) which can be omitted.

Creating Tar Archive

Use the -c operator to create a tar archive. For example, to create an archive file named archive.tar from the files named file1, file2, file3, run the following command:

tar -cf archive.tar file1 file2 file3

Here is the equivalent command using the long form options:

tar --create --file=archive.tar file1 file2 file3

The following example will create an archive named backup.tar from the /home/username directory:

tar -cf backup.tar /home/username

You can create archives from the contents of one or more directories or files. By default directories are archived recursively unless --no-recursion option is specified. Use the -v option if you want to see the files that are being processed.

Advertisement

Creating Tar Gz Archive

Gzip is the most popular algorithm for compressing tar files. When compressing tar archives with gzip the archive name should end with either tar.gz or tgz.

To create a tar.gz archive from given files you can use the following command:

tar -czf archive.tar.gz file1 file2

The -z option tells tar to compress the archive using the gzip algorithm as it is created.

Creating Tar Bz2 Archive

Another popular algorithm for compressing tar files is bzip2. When compressing tar archives with bzip2 the archive name should end with either tar.bz2 or tbz.

When -j option is specified tar will use bzip2 algorithm to compress the archive.

The following command will create a tar.bz2 archive from given files:

tar -cjf archive.tar.bz2 file1 file2

Listing Tar Archives

To list the content of a tar archive use the --list (-t) operation.

tar -tf archive.tar   
file1
file2
file3

If you list the content of the archive using the --verbose (-v) option, then tar will print more information such as owner, file size, timestamp ..etc:

tar -tvf archive.tar   
-rw-r--r-- linuxize/users       0 2018-09-08 01:19 file1
-rw-r--r-- linuxize/users       0 2018-09-08 01:19 file2
-rw-r--r-- linuxize/users       0 2018-09-08 01:19 file3

Extracting Tar Archive

To extract a tar archive use the --extract (-x) operator and specify the archive file name:

tar -xf archive.tar

It is also common to add the -v option to print the names of the files being extracted.

tar -xvf archive.tar

By default tar will extract the archive contents in the current working directory. Use the --directory (-C) to extract archive files in a specific directory:

For example, to extract the archive contents to the /opt/files directory, you can use:

tar -xf archive.tar -C /opt/files

Extracting Tar Gz and Tar Bz2 Archives

When extracting compressed archives such as tar.gz or tar.bz2 you do not have to specify a decompression option. The command is same as when extracting tar archive:

tar -xf archive.tar.gz
tar -xf archive.tar.bz2

Extracting Specific Files from a Tar Archive

To extract specific file(s) from a tar archive, append a space-separated list of file names to be extracted after the archive name:

tar -xf archive.tar file1 file2

When extracting files, you must provide their exact names including the path, as printed by --list (-t).

Extracting one or more directories from an archive is same as extracting files:

tar -xf archive.tar dir1 dir2

If you try to extract a file that doesn’t exist, an error message similar to the following will be displayed:

tar -xf archive.tar README
tar: README: Not found in archive
tar: Exiting with failure status due to previous errors

Extracting Files from a Tar Archive using Wildcard

To extract files from an archive based on a wildcard pattern, use the --wildcards switch and quote the pattern to prevent the shell from interpreting it.

For example, to extract files whose names end in .js (Javascript files), you can use:

tar -xf archive.tar --wildcards '*.js'

Adding Files to Existing Tar Archive

To add files or directories to an existing tar archive, use the --append (-r) operation.

For example to add a file named newfile to archive.tar, you can use:

tar -rvf archive.tar newfile

Removing Files from a Tar Archive

Use the --delete operation to remove files from an archive.

For example to remove a file named file1 from archive.tar, you can use:

tar --delete -f archive.tar file1

Conclusion

By now you should have a good understanding on how to create and extract tar archives. For more information about the tar command, consult the Gnu tar documentation page.