TAR
tar is the most popular archiving tool among other similar tools used to archive Files and Directories and also has the ability to compress them with the help of Gzip and Bzip2 compression tools. The tar term is taken from the Tape Archive and is also known as Tarball. Usually, tar is used for backup purposes. It stores single or multiple files/directories in one single file which is also called an archive.

Features of Tar archive tool

  • Create an Archive file
  • Extract an Archive file
  • List the contents present in an archive
  • Add a file or directory in an existing archive
  • Delete a file or directory from an archive
  • Concatenate multiple archive files
  • Compress archive files using compression tools like gzip and bzip2
  • Verify an archive file

First of all, let’s focus on some of the most important options that we can use with the tar command.

Options Explanation
-c, –create Create a new archive.
-x, –extract, –get Extract Files/Directories from an archive.
-t, –list List the contents of an archive.
-A, –catenate, –concatenateAppend archive to the end of another archive.
-C –directory=DIR Extract contents(Files/Directories) to a specific directory
–delete Delete from the archive.
-r, –append Append files to the end of an archive.
-z, –gzip Filter the archive through gzip.
-j, –bzip2 Filter the archive through bzip2.
–wildcards Use Wildcards with tar Command
-v, –verbose Verbosely list files processed.
-?, –help Help/Manual page access
version Display the version of tar Command.

You must follow the syntax given below to use the tar command.

tar [OPTION...] [FILE]...
Create a new tar archive

To create a simple tar archive type the following command.

Here in this example, we are archiving the directory named data/ which contains these files: file1.txt, file2.txt, file3.txt, file4.txt, file5.txt.

~]# tree data
data
├── file1.txt
├── file2.txt
├── file3.txt
├── file4.txt
└── file5.txt
and the output file that will be created after archiving is named data.tar

~$ tar -cvf data.tar data/
data/
data/file2.txt
data/file3.txt
data/file5.txt
data/file1.txt
data/file4.txt
The extension of the tar archive file is .tar

~$ ls
data  data.tar

Extract contents from an archive.

You can use the following command to extract an existing tar archive file.

In this example, we are extracting the archive named data.tar.

~$ tar -xvf data.tar 
data/
data/file2.txt
data/file3.txt
data/file5.txt
data/file1.txt
data/file4.txt
List the contents of an archive

To display or list the contents(files/directories) inside an archive, you can use the following command.

~$ tar -tvf data.tar 
drwxr-xr-x ubuntu/ubuntu     0 2020-07-10 03:11 data/
-rw-r--r-- ubuntu/ubuntu     0 2020-07-10 03:11 data/file2.txt
-rw-r--r-- ubuntu/ubuntu     0 2020-07-10 03:11 data/file3.txt
-rw-r--r-- ubuntu/ubuntu     0 2020-07-10 03:11 data/file5.txt
-rw-r--r-- ubuntu/ubuntu     0 2020-07-10 03:11 data/file1.txt
-rw-r--r-- ubuntu/ubuntu     0 2020-07-10 03:11 data/file4.txt
Create a tar archive with gzip compression(tar.gz/tgz)

As mentioned at the beginning of this article, we can compress the tar archive with the help of gzip and bzip2 tools.

The extension of the gzip compressed tar archive file is .tar.gz or tgz.

Type the following command to create a compressed tar archive from gzip. The -z option is used to compress the tar archive with gzip.

~/data$ tar -czvf data.tar.gz data/
Or you can also use the following command.

~/data$ tar -czvf data.tgz data/
Output:

data/
data/file2.txt
data/file3.txt
data/file5.txt
data/file1.txt
data/file4.txt
Create a tar archive with bzip2 compression(tar.bz2/.tbz/tar.tb2/tar.tbz)

The extension of the bzip2 compressed tar archive file is .tar.bz2 or .tbz or tar.tb2 or tar.tbz.

Type any of the following commands to create a compressed tar archive from bzip2. The -j option is used to compress the tar archive with bzip2.

~$ tar -cjvf data.tar.bz2 data/
OR

~$ tar -cjvf data.tar.tb2 data/
OR

~$ tar -cjvf data.tar.tbz data/
OR

~$ tar -cjvf data.tbz data/
Output:

data/
data/file2.txt
data/file3.txt
data/file5.txt
data/file1.txt
data/file4.txt
Extract contents from an Compressed(tar.gz/tar.bz2) archive

Whether the Tar archive is gzip compressed or bzip2 compressed we have to use the same options with the tar command to extract/uncompress and that is -xvf.

Let’s take some examples :

Extract tar.gz archive File:

Type the following command to extract/uncompress tar.gz archive file.

~$ tar -xvf data.tar.gz
data/
data/file3.txt
data/file4.txt
data/file1.txt
data/file5.txt
data/file2.txt
Extract tar.bz2 archive File:

Type the following command to extract/uncompress tar.bz2 archive file.

~$ tar -xvf data.tar.bz2
data/
data/file3.txt
data/file4.txt
data/file1.txt
data/file5.txt
data/file2.txt
Extract contents to a specified directory

As you know, By default tar extracts the contents in the current working directory.

But if you want to extract your contents in the specified directory, it is possible. Let’s take some examples.

Extract standard tar archive to a specified directory:

Following command will extract the data.tar(Standard Tar archive) file to mydata/files/ directory.

~$ tar -xvf data.tar -C mydata/files/
data/
data/file3.txt
data/file4.txt
data/file1.txt
data/file5.txt
data/file2.txt
Extract tar.gz archive to a specified directory:

Following command will extract the data.tar.gz(gzip compressed Tar archive) file to mydata/files/ directory.

~$ tar -xvf data.tar.gz -C mydata/files/
data/
data/file3.txt
data/file4.txt
data/file1.txt
data/file5.txt
data/file2.txt
Extract tar.bz2 archive to a specified directory:

Following command will extract the data.tar.bz2(bzip2 compressed Tar archive) file to mydata/files/ directory.

~$ tar -xvf data.tar.bz2 -C mydata/files/
data/
data/file3.txt
data/file4.txt
data/file1.txt
data/file5.txt
data/file2.txt
List the contents of tar.gz and tar.bz2 Archive

The contents of the compressed tar archive can be listed easily. Let’s take an example of each.

Type the following command to list the contents of a tar.gz archive. In this example, we are listing the contents of data.tar.gz

~$ tar -tvf data.tar.gz
drwxr-xr-x helpdesk/helpdesk 0 2021-01-26 06:25 data/
-rw-r--r-- helpdesk/helpdesk 0 2021-01-26 06:25 data/file3.txt
-rw-r--r-- helpdesk/helpdesk 0 2021-01-26 06:25 data/file4.txt
-rw-r--r-- helpdesk/helpdesk 0 2021-01-26 06:25 data/file1.txt
-rw-r--r-- helpdesk/helpdesk 0 2021-01-26 06:25 data/file5.txt
-rw-r--r-- helpdesk/helpdesk 0 2021-01-26 06:25 data/file2.txt
Type the following command to list contents of a tar.bz2 archive. In this example, we are listing the contents of data.tar.bz2

~$ tar -tvf data.tar.bz2 
drwxr-xr-x helpdesk/helpdesk 0 2021-01-26 06:25 data/
-rw-r--r-- helpdesk/helpdesk 0 2021-01-26 06:25 data/file3.txt
-rw-r--r-- helpdesk/helpdesk 0 2021-01-26 06:25 data/file4.txt
-rw-r--r-- helpdesk/helpdesk 0 2021-01-26 06:25 data/file1.txt
-rw-r--r-- helpdesk/helpdesk 0 2021-01-26 06:25 data/file5.txt
-rw-r--r-- helpdesk/helpdesk 0 2021-01-26 06:25 data/file2.txt
Append(Add) content(File/Directory) to a Existing Tar Archive

You can always add files/directories to an existing tar archive. to do so use the following command.

In this example, we are adding a file named myfile.txt to data.tar

~$ tar -rvf data.tar myfile.txt
Adding a directory named mydata/ to data.tar

~$ tar -rvf data.tar mydata/
Note: You cannot add/update contents to an existing compressed tar archive(tar.gz/tar.bz2).

I got this error when I added a file to the compressed tar archive

~$ tar -rvf data.tar.gz myfile.txt
tar: Cannot update compressed archives
tar: Error is not recoverable: exiting now
Delete content(File/Directory) from an Existing Tar Archive

You can also remove/delete files/directories from an existing tar archive. to do so run the following commands.

Deleting a file named myfile.txt from data.tar

~$ tar --delete -f data.tar myfile.txt
Deleting a directory named mydata/ from data.tar

~$ tar --delete -f data.tar mydata/
Note: You cannot remove contents from an existing compressed tar archive(tar.gz/tar.bz2).

Extract Specific Content(File/Directory) from a Tar Archive

You can use the following commands to extract a specific file/directory from a Standard or Compressed tar archive. Let’s take an example of each.

Extract file or directory from a Standard tar archive

Following command will extract two files named file2.txt and file3.txt from data.tar

~$ tar -xvf data.tar data/file2.txt data/file3.txt
Note: Always remember that when extracting a specific file or directory from any tar archive, mention its complete path.

If you notice in the above example, we have mapped the entire path of the file to extract specific files. i.e. Both file2.txt and file3.txt are inside the data/ directory, so I had to mention data/file2.txt, data/file3.txt in the command.

To check the path of files/directories use the following command.

~$ tar -tvf data.tar       
drwxr-xr-x helpdesk/helpdesk 0 2021-01-27 01:58 data/
-rw-r--r-- helpdesk/helpdesk 0 2021-01-27 01:58 data/file3.txt
-rw-r--r-- helpdesk/helpdesk 0 2021-01-27 01:58 data/file4.txt
-rw-r--r-- helpdesk/helpdesk 0 2021-01-27 01:58 data/file1.txt
-rw-r--r-- helpdesk/helpdesk 0 2021-01-27 01:58 data/file5.txt
-rw-r--r-- helpdesk/helpdesk 0 2021-01-27 01:58 data/file2.txt
Here in this example, we are extracting a directory named mydata/ from data.tar

~$ tar -xvf data.tar mydata/
You also have to use the same command to extract specific content from a compressed archive. for example:

Extract file or directory from a tar.gz archive

~$ tar -xvf data.tar.gz data/file2.txt data/file3.txt
Extract file or directory from a tar.bz2 archive

~$ tar -xvf data.tar.bz2 data/file2.txt data/file3.txt
Usage of Wildcards with Tar Command

Wildcard is a symbol or a special character using which we can match the pattern of a word or a string to get our desired output.

Some popular Wildcard Characters are Asterisk (*), Percent (%), Brackets ([])…etc.

Now let’s see how well we can use wildcard with tar command.

Example #1

Include those files in a tar archive whose extension is “.txt

~$ tar -cvf myfiles.tar --wildcards *.txt
Example #2

Extract all files from the archive starting with “fi

~$ tar -xvf myfiles.tar --wildcards fi*
Check the version of tar command

You can check the tar command version using the following command.

~$ tar --version
Help/Manual page access

Use the following commands to access the Manual Page/Help Page of tar command.

~$ man tar

~$ tar --help

We use cookies to personalize and enhance your experience on our site. By using our site, you agree to our use of cookies.
  More information about cookies