Tar command Compress and Extract Archives

The tutorial explains how to use the tar command to compress and extract files in Linux. Learn how to preserve the SELinux context while creating a tar archive file.

The tar command is one of the most commonly used commands for creating archives. Archives allow us to manage several files and directories as a single file. Archives are usually created for backup or transferring files.

To save disk space (if the archive is being created for backup) or to save network bandwidth (if the archive is being created to transfer files), usually files are compressed before they are added to the archive.

The tar command does not have any inbuilt functionality to compress files when adding them to the archive. But if required, it can use third-party compression utilities such as the gzip and bzip2 to compress files before adding them to the archive.

The gzip utility compresses faster but provides a low compression ratio. The bzip2 command compresses slower but provides a high compression ratio. In the following section, we will understand how to use both utilities with the tar command.

This tutorial is the third and last part of the article "How to use Tar Command in Linux Explained with examples". The previous 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 examples in Linux

This is the second part of this article. It explains how to manage an archive after creation.

Using gzip and bzip2 to compress tar archives

To instruct the tar command to use the gzip utility to compress files before adding them to the archive, use the option g with the options cvf. To indicate that the gzip compression is used, use the extension .gz with the archive file name.

To create and compress the archive file with the bzip2 utility, use the option j with options cvf and use the file extension .bz2 with the archive file name.

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

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

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

The following image shows the above commands with output.

create a tar archvie for practice

Once an archived file is compressed, advance operations such as append, update, and individual delete can't be performed on the archived file. If you perform these advanced operations on a compressed archived file, the tar command will show the following error.

Tar: Error is recoverable: exiting now

The following image shows what will happen if we perform the following tasks on a compressed archive file: Append, update, and individual delete.

tar command error Tar: Error is recoverable: exiting now

Extracting a compressed archive file

To extract an archived file, the options xvf are used with the tar command. If the archive file is compressed by using the gzip utility, specify the option g along with these options. If the archive file is compressed by using the bzip2 utility, specify the option j along with these options.

Alternatively, you can omit the compression related option (g and j). If the compression related option is not specified and the archived file is compressed, the tar command automatically detects compression and use the correct compression utility to decompress the compressed archived file.

For example, to extract the compressed archive data.tar.gz, you can use any one command from the 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 the compression related option is specified, the tar command uses the utility that is associated with the specified option. And if that utility is incorrect, the archived file will not be decompressed.

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

#tar -xjvf data.tar.gz

The following image a few more examples of how to use the correct options to decompress and extract compressed archived files.

extracting an compressed archive

Preserving SELinux context when adding files to the 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 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 for the practice: reg and ext.
  • Switch to the directory /var/www/html and create a few files.
  • Create two archive files by using the files which are created in the above step. When creating the second archive file, specify the option --selinux before specifying the options -cvf.
  • Copy both archive files into the directories (reg and ext) that we created in the first step, respectively.
  • Switch to both directories one-by-one and extract the copied archive files.
  • When extracting the archived file that was created with the option --selinux, specify the same option --selinux before specifying the options -xvf.

The following image shows the above exercise step by step along with the required commands and their output.

extract archive preserve SELinux

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

The following image shows the SELinux context of the original files, extracted files without --selinux option, and extracted files with the --selinux option.

preserve SELinux context tar archive

As you can see in the above image, if the option --selinux is used when creating and extracting an archive file, the SELinux contexts of the files are preserved.

That's all for this article. If you have any suggestions, 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.

Advertisements

ComputerNetworkingNotes Linux Tutorials Tar command Compress and Extract Archives