Tar command and Syntax Explained with Examples

This tutorial explains the basic usages of tar command in Linux with practical examples. Learn how to create an archive file, list the contents of tarball and extract the archived file in current directory or in specified directory.

The tar command is used to manage the archives. An archive is the collection of multiple files that can be transferred as a single file or can be stored as a backup copy. An archive file, created with tar command, is also known as a tarball.

The tar command syntax

To create, list and extract an archived file, the tar command uses following syntax respectively.

tar -cvf [destination-file-name.tar] [source-files or directories]
tar -tvf archive-file.tar
tar -xvf archive-file.tar

The tar command options

The option c creates archive or tarball from the supplied files or directories. If a directory is supplied, all of its files and sub-directories are also included in the archived file recursively.

The option v displays the progress of archiving process at the command prompt. This option is optional. You can use it; if you want see what the tar command is actually doing. Or you can skip it; if you don’t want to print the progress of archiving process at the command prompt.

The option f specifies the file or device name. If it is used with option c, it instructs the tar command to store the output data to the specified device or file. If we don’t specify this option, the tar command uses the standard output device (console or command prompt) to store the output data. In other words, if we don’t specify the output location with this option, the tar command simply dumps all output data to the command prompt.

While specifying a file to store the output data, usually the extension .tar is used with file name. Although it’s not required, still you should always use this extension with the file name. Using an extension for archived files helps keeping them separate from other files.

The option t lists the contents of archive or tarball. Since this option lists the content of archive, the archive file or device must to be specified with the option f.

The option x extracts the archive. Just like option t, this option also need an archive file or device specified with the option f.

Key points
  • While specifying options, you can use a dash (-) before the options or not. For example, you can specify options as -cvf or as cvf. Tar supports both styles.
  • The order of options is important. First you have to specify the action (create, list or extract), next specify any additional requirement such verbose message and in the last specify the file or device name where the desired action need to be performed.
tar command options order

This tutorial is the second part of the article "Compressing and archiving explained in Linux". This tutorial explains following RHCSA/RHCE topic.

Archive, compress, unpack, and uncompress files using tar, star, gzip, and bzip2

Other parts of this article are following.

Compress and Decompress Files in Linux with gzip and bzip2

This tutorial is the first part of the article. It explains how to compress and decompress files in Linux with gzip and bzip2.

Tar Command Examples in Linux

This tutorial is the last part of the article. It explains how to use the tar command in Linux with practical examples.

Creating archive or tarball

To create an archive, three options c, v and f are used together.

Let’s take some examples to understand how these options are used with the tar command to create archives.

Following command creates an archive from a single file named test and puts that archive file in the same directory.

#tar -cvf  test.tar test

creating archive with tar

To add multiple files in an archive, specify them as arguments. For example, following command creates the archive file files.tar from files; a, b, c, d and f.

#tar -cvf files.tar a b c d f

creating archive with multiple files

Just like the files, we can also specify the directories as arguments. If we specify a directory as an argument, all of its files and sub-directories are added in archived file recursively. For example, following command creates the archive file data.tar from the directory testdir.

#tar -cvf data.tar testdir

creating archive with directories

If files and directories that you want to add in archive file are not available in current directory, use their full path. For example, following command creates the archive conf.tar from the files; /etc/hosts, /etc/hostname and /etc/host.conf.

adding files in archive from other directories

Just like we can create an archive from the files which are not available in current directory, we can also put the archived file in any location or device. For example, following command adds all files and directories from home partition in the archive home.tar and stores that archive in /tmp partition.

#tar -cvf /tmp/home.tar /home

adding all files and directories recursively  in archive

Listing the content of tarball or archive file

Once an archive or tarball file is created, its contents (name of files and directories) can be listed with the options -tvf. For example, to see what files are stored in the archive data.tar, you can use following command.

#tar -tvf data.tar

Following figure shows few more examples of listing the contents from archive file.

tar command listing the contents of archive file

Extracting files and directories from a tarball or an archive file

To extract an archive or tarball file, the option x is used with options v and f. For example, following command extracts an archive file test.tar.

#tar -xvf test.tar
 

In extraction process, if a file already exists, it will be overwritten without any warning and notification message. While extracting an archive file, we must take care of this default behavior. Let’s understand it with an example.

You added a file in an archive on Sunday. On Monday, you made some changes in original file. On Tuesday, you extracted the archived created on Sunday in the same directory.

What will happen in this case?

In this case, all changes which were made on Monday will be lost as the updated file will be overwritten with the file which was backed up on Sunday.

default extraction process tar command

In extraction process, the tar command uses the same directory structure in which files and directories were added in archive. It first creates directories and then extracts files in respective directories.

extract directories and files from tar command

The tar command, instead of the directory where the archived file is stored, extracts it in the directory from which it is executed. In simple words, it extracts archive in the current directory.

tar extracts archive in current directory

To extract archive in a specific directory, we have two choices.

  1. Switch to the directory before executing the tar command.
  2. Specify the directory path with option -C

Following figure shows examples of both.

extract archive in specific directory

That’s all for this part. In next part of this tutorial I will explain how to perform some advance operations with the tar command. If you like this tutorial, please don’t forget to share with friends.

ComputerNetworkingNotes RHCE 7 Study Guide Tar command and Syntax Explained with Examples