Creating and Extracting Archives Using Tar Command in Linux

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

Team tarused to create tar archives by converting a group of files to an archive. It also has the ability to extract tar archives, display a list of files included in the archive, add additional files to an existing archive, and various other kinds of operations.

Tar supports a wide variety of compression programs such as gzip, bzip2, lzip, lzma, lzop, xzand compress… When creating compressed tar archives, it is generally considered to add a compressor suffix to the archive file name. For example, if the archive was compressed using gzip, it should be named archive.tar.gz.

Tar was originally designed to create archives while storing files on magnetic tape, hence the name “ T ape AR chive “.

There are two versions of tar, BSD tar and GNU tar, with some functional differences. Most Linux systems come with GNU tar preinstalled by default. If you do not have tar installed, we recommend that you first install it following our article on how to install tar.gz on CentOS.

Before moving on to using the tar command, let’s start by looking at the basic syntax.


  • OPERATION– Only one argument is permitted and required. The most commonly used operations:
    • --create( -c) – Create a new tar archive.
    • --extract( -x) – Extract the entire archive or one or several files from the archive.
    • --list( -t) – display a list of files included in the archive
  • OPTIONS – The most commonly used operations:
    • --verbose( -v) – Show files processed by tar command.
    • --file=archive=name( -f archive-name) – Specifies the name of the archive file.
  • ARCHIVE_NAME – The name of the archive.
  • FILE_NAME(s)– a space-separated list of filenames to extract. If not provided, the entire archive will be extracted.

When executing the tar command, you can use the long or short form of tar operations and options. Long forms are more readable, while short forms are faster. Long form options are prefixed with a double dash ( --). Short form parameters are prefixed with one dash ( -), which can be omitted.

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

tar -cf archive.tar file1 file2 file3

Here is the equivalent command using long form parameters:

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

The following example will create an archive backup.tar from catalog /home/username:

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 if the parameter is not specified --no-recursion… Use the parameter -vif you want to view the files that are being processed.

Gzip is the most popular compression algorithm for tar files. When compressing tar archives with gzip, the archive name must end with either tar.gzor tgz

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

tar -czf archive.tar.gz file1 file2

Option -ztells tar to compress the archive using an algorithm gzip

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

When option is specified -j , tar will use the algorithm bzip2 archive compression.

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

tar -cjf archive.tar.bz2 file1 file2

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

tar -tf archive.tar


If you specify the contents of the archive using the parameter --verbose( -v), tar will print more information like owner, file size, timestamp, etc .:

tar -tvf archive.tar

-rw-r--r-- linuxize/users       0 2018-09-08 20:15 file1
-rw-r--r-- linuxize/users       0 2018-09-08 20:15 file2
-rw-r--r-- linuxize/users       0 2018-09-08 20:15 file3

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

tar -xf archive.tar

Also the option is usually added -v to print the names of the extracted files.

tar -xvf archive.tar

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

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

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

When extracting compressed archives, for example, tar.gzor tar.bz2 you don’t need to specify the decompression parameter. The command is the same as when extracting an archive tar:

tar -xf archive.tar.gz

tar -xf archive.tar.bz2

To extract specific files from a tar archive, add a space-separated list of filenames to be extracted after the archive name:

tar -xf archive.tar file1 file2

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

Extracting one or more directories from an archive is similar to extracting files:

tar -xf archive.tar dir1 dir2

If you try to extract a file that does not exist, an error message similar to the one below appears:

tar -xf archive.tar README

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

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

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

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

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

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

tar -rvf archive.tar newfile

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

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

tar --delete -f archive.tar file1

By now, you should have a good understanding of how to create and extract tar archives.

Please disable your ad blocker or whitelist this site!

Related Posts