Tar Command in Linux with Examples

Today you will learn how to archive files and directories in Linux using the tar command.

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.

If you are a regular reader of this blog, you must have noticed that I certainly tells you that each Linux command has been derived from which common word.

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.

Because it stores your data in an archive file, which makes it easy to transfer data from one computer to another.

We have several tools in Linux by which you can compress files like gzip, bzip2, zip but we cannot compress the directory and its contents using any of these tools.

Suggested Read:

If you want to compress a directory then you have to use the tar command.

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.

OptionsExplanation
-c, --createCreate a new archive.
-x, --extract, --getExtract Files/Directories from an archive.
-t, --listList the contents of an archive.
-A, --catenate, --concatenateAppend archive to the end of another archive.
-C --directory=DIRExtract contents(Files/Directories) to a specific directory
--deleteDelete from the archive.
-r, --appendAppend files to the end of an archive.
-z, --gzipFilter the archive through gzip.
-j, --bzip2Filter the archive through bzip2.
--wildcardsUse Wildcards with tar Command
-v, --verboseVerbosely list files processed.
-?, --helpHelp/Manual page access
--versionDisplay the version of tar Command.

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

tar [OPTION...] [FILE]...

1. Create a new tar archive

To create a simple tar archive type the following command.

Here in this example, I am 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
Tar Command Explained

2. Extract contents from an archive.

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

In this example, I am 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

3. 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

4. Create a tar archive with gzip compression(tar.gz/tgz)

As I 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

5. 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

6. 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

7. 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

8. 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, I am 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, I am 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

9. 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, I am 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

10. 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).

11. 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, I 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, I am 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

12. 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*

13. Check the version of tar command

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

~$ tar --version

14. Help/Manual page access

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

~$ man tar
~$ tar --help

You can visit at following websites to get more information on tar command.

Conclusion

I hope you have learned something from this article and you may have found that tar is a very important command in Linux.

I have tried my best to include all the features of tar command in this guide.

Now I’d like to hear your thoughts.

Was this guide useful to you?

Or maybe you have some queries.

Have I not included any command in this guide?

Leave a comment below.

If you like our content, please consider buying us a coffee.

Buy Me A Coffee

We are thankful for your never ending support.

Leave a Comment