Tar Command Examples in Linux

Learn how to use the tar command in Linux with practical examples such as adding or updating files in
existing archive, removing and extracting a single file from the archive and preserving SELinux contexts in archiving process.

Lab setup for practice

Create a new directory and create some files and few sub-directories with files in this directory.

lab setup for tar command practice

Create an archive data.tar with few files.

create tar archive

That’s all setup we need for the practice.

This tutorial is the last 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 and Syntax Explained with Examples

This tutorial is the second part of the article. It explains basic usages of tar command with syntax and options.

Adding new file in exiting archive file

To add an additional file in existing archive, use the options -rvf and specify the new file name as argument.
For example, following command adds the file c in the existing archive data.tar.

#tar -rvf data.tar c

New file will be added in the end of the archive.

adding new file in existing tar archive

Removing or deleting files from an archive

To delete a file from 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

removing a file from archive tar example

Updating files in existing archive

To add only those files which are modified or created since the archive was created, 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 change in any file or finds any new
file and directory, it appends that file and directory in the end of the archive.

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

This feature allows us to store multiple copies of the same file where first copy
contains the original data and all later incremental copies contains the original data
plus the data which was added since the last incremental copy was created.

To view how many incremental copies of a file are stored in an archive,
we can use option t. The option t lists the name 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.

In extraction process, files are extracted in the same sequence in which they were added in the
archive. It means, original copy extracts first and incremental copies extract later in their respective sequence.

By default, extraction process overwrites existing files. Due this feature, the original file which extracts
first is overwritten with the updated file which extracts later.

To understand this process practically, create an archive file from a directory which contains sub-directories
with files. Once archive is created, run update process immediately.

Since nothing has changed since the archive was created, update process adds nothing in archive.

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

List the contents of archive with options -tvf, to verify that updated file has added in the end of the archive.

Remove the original directory. It helps us in understanding how the extraction process works with an archive that contains incremental copies.

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

Following figure illustrates this example step by step.

updating archive file

Compressing and decompressing archive

By default, the tar command doesn’t perform compression on archived files.
But if require, it can use the third party compression utility such as gzip and bzip2 to compress and decompress the archived file.

The gzip compress faster but provides low compression ratio. To create and compress the archive file with gzip utility,
use the option g with options cvf as cgvf and use the file extension .gz with the archive file name.

The bzip2 compress slower but provides high compression ratio.
To create and compress the archive file with bzip2 utility, use the option j with options cvf as cjvf and
use the file extension .bz2 with the archive file name.

Following commands create the archive home.tar from the /home directory without compression,
with gzip compression and with bzip2 compression.

#tar -cvf /tmp/home.tar /home
#tar -czvf /tmp/home.tar.gz /home
#tar -cjvf /tmp/home.tar.bz2 /home

To view the size of compressed archive file, once compression is done, use du command with the option h.

compressing archive file with gzip and bzip2

Once an archived file is compressed, advance operations such as append, update and individual delete can’t be performed with it.

no advance options can be perform with compressed file

To extract a compressed archive, use the same option which you used to create it.
For example, if archive is compressed with gzip, use the option z with the options xvf as xzvf.
And if archive is compressed with bzip2, use the option j with the options xvf as xjvf.

Alternatively, you can skip the compression related option (such as g and j)
and let the tar to automatically detect and use the correct compression utility to decompress the compressed archive.

For example to extract the compressed archive data.tar.gz, you can use any one command from following commands.

#tar -xvf data.tar.gz
#tar -xzvf data.tar.gz

If you are not sure which compression utility was used to compress the archive file,
don’t specify the compression related option. If compression related option is specified,
the tar command uses the utility that is associated with that option. And if that utility is incorrect, archived file is not decompressed.

For example, following command will fail, as it instructs tar to use the bzip2
utility to decompress the archived file that is compressed with the gzip utility.

#tar -xjvf data.tar.gz

extracting a compressed file

Preserving SELinux context in archive

File attributes are used to store additional information about the file.
There are two types of attributes; regular attributes and extended attributes.
Regular attributes contain the essential information such as owner info, access permission, created date etc. Extended attributes contain the metadata information such as access control list, SELinux etc.

By default, the tar command doesn’t retrain the extended attributes.
If we want to preserve the extended attributes, we have to instruct the tar command to do the same.
For example, if you want to preserve the SELinux contexts, use the option –selinux.

Let’s take an example, to understand how it works practically.

  • Create two directories in our practice directory.
  • Switch to directory /var/www/html and create few files.
  • Create two archive files with the files which created in above step.
    While creating second archive file, use the option –selinux before the options -cvf.
  • Copy both archive files in the directories created in first step respectively.
  • Switch in both directories and extract the copied archive files.
  • While extracting the archived file which was created with the
    option –selinux, use the same option –selinux again before the options -xvf.

Following figure illustrates above process step by step.

preserve selinux contexts in tar archive

Now compare the SELinux contexts of extracted files with the SELinux contexts of the original files.

tar command selinux example

As we can see in above figure, an archive created and extracted with –selinux option preserves the SELinux contexts.

The tar command v/s star command

The original version of tar does not support the extended attributes. To support the extended attributes, an enhanced version of tar, known as star, was designed. Besides the options which require for extended attributes, it uses the same options for creating, listing, appending, updating and extracting the archive. Later, the support for extended attributes was also added in the original version of tar. After this update, there is no significant difference between both commands.

You can perform almost all tasks, which you can perform with the star command, with the tar command also. To support the tar command, star command is not installed by default. In exam, unless you are explicitly asked to use the star command, there is no need to install and use the star command. You can use the tar command to perform all archive related tasks.

That’s all for this article. If you have any suggestion, feedback or comment about this article, let me know. If you like this article, please don’t forget to share it with friends through your favorite social network.

Full Version EX300 Dumps

Try EX300 Dumps Demo