SoFunction
Updated on 2025-04-21

Linux uses the tar command to create archives and compress files

Preface

  • In Red Hat Linux system,tar is a very powerful tool for creating archive files and performing compression operations
  • Whether it is backing up important data or transferring files between systems, it is very useful to master the use of tar.
  • This article will start with the basic concepts and gradually introduce how to create archive files using tar and combine them with different compression formats (such as gzip, bzip2 and xz) for compression operations.

1. Basic concepts of archive and compression

1.1 The difference between archive and compression

Before we officially start learning, it is necessary to understand clearly the difference between archiving and compression:

  • Archive: The essence of an archive operation is to combine multiple files or directories into a single file.

  • Archive files are often used in data backup scenarios, or to make the file transfer process more convenient.It should be noted that the archive file itself does not reduce the size of the file, it simply brings together multiple scattered files.

  • compression: Compression is to effectively reduce the disk space occupied by files with the help of specific algorithms.

  • Through compression, the file size can be significantly reduced, which not only saves a lot of storage space, but also speeds up the transfer speed during file transfer.

In Red Hat Linux system,tarCommands can not only complete archive tasks independently, but can also be used in conjunction with various compression tools to realize the dual functions of archive and compression, providing users with a more efficient file management method.

2. Create archive files using tar

2.1 tar command format

tarThe basic format of the command is as follows:

tar + Options + parameter

Among them, the commonly used options are:

  • -c: Used to create a new archive file.
  • -f: Specify the specific name of the created archive file.
  • -x: Perform the operation of decompressing the archive file.
  • -P: Keep the original path information of the file so that it can be restored to its original location when decompressed.
  • -C: Specify the target directory when decompressing the archive file.

2.2 Sample Operation

  • Create an archive file
    Suppose we want to/etcAll files in the directory are archived and saved as a separate file. At this point, you can use the following command:
tar -cf /root/ /etc

The specific meaning of this command is:
-c: Create an archive file.
-f: Clearly specify the name of the archive file as/root/
/etc: Specifies the target directory to be archived.
After executing the command,/etcAll files and subdirectories in the directory will be successfully archived to/root/in the file.

  • Unzip the archive file
    When we need to decompress the already created archive file, we can use the following command:
tar -xf /root/

By default, the archive file is decompressed in the current directory. If we want to unzip the file into the specified directory, we can use-COptions, commands are as follows:

tar -xf /root/ -C /opt

This command will/root/Unzip the contents in the file to/optIn the directory, the decompression operation of the specified directory is implemented.

  • Keep the original path
    If you want to retain the original path information of the file during the archiving process, so that it can be restored to its original location accurately during decompression, you can add it to the archive command.-PThe specific commands are as follows:
tar -cPf /root/ /etc

In this way, when performing subsequent decompression operations, it is also necessary to add-POptions to ensure that the file can be restored to the original path correctly, the decompression command is:

tar -xPf /root/
  • View the size of the archive file
    If we want to see the specific size of the archive file, we can useduThe command is implemented, the command is as follows:
du -sh /root/

This command will be displayed in human-readable formats (such as KB, MB, GB, etc.)/root/The size of the file makes it easier for us to intuitively understand the file's space.

3. Use tar for compression

tarThe command itself is mainly an archive tool, but it is very scalable and can be used closely with a variety of compression tools such as gzip, bzip2, and xz to achieve powerful compression functions.

3.1 Command format

tarThe format when used in combination with the compression tool is still:

tar + Options + parameter

Among them, commonly used compression options include:

  • -z: means to compress the file in gzip format.
  • -j: means to compress the file in bzip2 format.
  • -J: means to compress the file in xz format.

3.2 Sample Operation

  • Use gzip compression
    Suppose we want to/etcAll files in the directory are archived and compressed in gzip format at the same time. You can use the following command:
tar -czPf /root/ /etc

The specific meaning of this command is:
-c: Create an archive file.
-z: Compressed in gzip format.
-P: Keep the original path to the file.
-f: Specify the name of the archive file as/root/
/etc: Specify the directory to archive.
After executing the command,/etcAll files and subdirectories in the directory will be successfully archived and compressed to/root/in the file.

  • Compress using bzip2
    If you need to compress the file in bzip2 format, you can use the following command:
tar -cjPf /root/.bz2 /etc

This command will/etcAll files in the directory are archived and compressed in bzip2 format, and finally generated/root/.bz2document.

  • Using xz compression
    If you want to compress in xz format, you can use the following command:
tar -cJPf /root/ /etc

This command will/etcAll files in the directory are archived and compressed in xz format to generate/root/document.

This is the article about the operation process of creating archives and compressed files with tar commands in Linux. For more related contents of creating archives and compressed files in Linux tar, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!