Tar command examples in Linux

This tutorial presents the most useful examples of the tar command. Learn how to create, list, update, and manage an archived file in Linux.

Suppose, you want to move several thousand files and directories from your computer to a remote computer. If you have to transfer all these files separately, then it would be a difficult task. But if all these files and directories are packed into a single archived file and you only have to move that file, then it would be an easier task.

The tar command creates a single archived file from several files or directories. A single archived file is much easier to manage than managing a lot of files, especially when moving files from one location to another or backing up files.

In this tutorial, we will understand how to use the tar command to create and manage an archived file.

Lab setup for the practice

Access the shell prompt and create a directory. In this directory, create some dummy files and subdirectories. You can also create some files in subdirectories.

The following image shows how to set up this lab.

lab setup for tar command practice

Creating and listing archived file

The following command creates an archived file from multiple files and directories.

#tar -cvf [archived file name] [source files or directories name]

For example, to create an archived file data.tar from the files: a and b, use the following command.

#tar -cvf data.tar a b

Once an archived file is created, you can the following command to list its contents.

#tar -tvf [archived file name]

For example, to list the contents of the archived file data.tar, use the following command.

#tar -tvf data.tar

The following image shows the above command with the output.

tar command adding files to tar

I have already explained how to use the options c, v, f, t, and x in the previous part of this tutorial. These options are required to create and list an archived file. In this part, instead of repeating the same options, I will explain the options that are required to manage an archived file.

This tutorial is the second part of the article "How to use Tar Command in Linux Explained with examples". The other parts of this article are the following.

Tar command options and syntax explained

This is the first part of this article. It explains how to use the tar command to create an archive.

Tar command Compress and Extract Archives

This is the third and last part of this article. It explains how to compress and extract a compressed archive in detail.

Adding new file in exiting archive file

To add an additional file to an existing archive, use the options -rvf and specify the name of the new file as an argument.

For example, the following command adds the file c to the archive data.tar.

#tar -rvf data.tar c

The tar command adds the specified file to the end of the archive.

The following image shows the above commands with the output.

adding files to an existing archive

Removing or deleting files from an archive

To delete a file from the archive, use the option --delete.

For example, to delete a file named b from the archive data.tar, use the following command.

#tar --delete b -vf data.tar

The following image shows the output of the above command.

deleting a file from an archive

Updating files of an existing archive

To add only those files that have been modified or created since the creation of the archive, use the option u.

This option compares the supplied files and directories (as arguments) with the files and directories stored in the supplied archive. If it detects any changes to any file or finds a new file or directory, it adds that file or directory to the end of the archive.

Since this option, instead of replacing the current version of the file, adds the updated version of the file as a separate file at the end of the archive, the original version of the file also remains in the archive.

This feature allows us to store multiple copies of the same file, where the first copy contains the original data and all subsequent incremental copies contain the original data and the data that was added after the last incremental copy was made.

To see how many incremental copies of a file are stored in the archive, we can use the option t. The option t lists the names of files and directories stored in the archive. If a file is listed two or more times, it simply means that the file has been changed since the archive was created.

When extracting archives, the tar extracts files and directories in the same order in which they were added to the archive. This means, the original copy will be extracted first and incremental copies will be extracted later in their respective sequence.

By default, the tar uses overwrite mode to extract archives. In this mode, if incremental copies of a file are available, the tar first extracts the original copy and then all the incremental copies in their respective order. In this way, the original file will be overwritten by the last available incremental copy of the file.

To understand this process, let's take an example.

Add a directory (which includes sub-directories and files) to an archive. Once the archive is created, run the update process immediately.

Since nothing has changed since the creation of the archive, the update process does not add anything to the archive.

Now change a file and run the update process again. This time, since a file has changed, the update process adds the modified file at the end of the archive.

To verify that the update file has been added to the end of the archive, list the contents of the archive using the options -tvf.

To understand how the tar extracts an archive that contains incremental copies, remove the original directory.

Now extract the archive and list the directory that contains the modified file. It should only contain the updated version of the file.

The following image shows this exercise step-by-step with the output.

updating an archive

Listing the contents of a tarball or an archive file

To list the contents of an archive, you can use the options -tvf. For example, to see what files and directories are stored in the archive data.tar, you can use the following command.

#tar -tvf data.tar

The following image shows some more examples that list the contents of the archives.

listing an archive

Extracting files and directories from a tarball or an archive file

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

#tar -xvf test.tar

As mentioned earlier, when extracting an archive, the tar command uses the overwrite mode. It means, if the destination directory already contains a file of the same name, the tar command will overwrite the existing file if it finds a file of the same name in the archive.

Let's understand it with an example.

You added a file to an archive on Sunday. On Monday, you made some changes to the original file. On Tuesday, you extract the archive 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.

The following image shows this exercise step-by-step with the output.

extracting archive

When extracting the archive, the tar command uses the same directory structure in which the file and directory were added to the archive. It first creates the directory and then extracts the files in the respective directories.

The following image shows this behavior of the tar command through an example.

extracting a tar archive file

When extracting an archive, by default, the tar command uses the current directory as the destination directory. It creates the same directory structure in the current directory and extracts files in that structure.

The following image explains this behavior of the tar command through an example.

tar command extract to another directory

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

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

The following image shows how to use both options with examples.

extracting file to another directory

That's all for this tutorial. In the next and last part of this tutorial, we will understand how to preserve SELinux contexts when extracting an archive. If you like this tutorial, please don't forget to share it with friends through your favorite social channel.

Advertisements

ComputerNetworkingNotes Linux Tutorials Tar command examples in Linux