How to use the linux tar command

02-05-2023

This article introduces the knowledge about how to use the linux tar command. During the operation of the actual case, many people will encounter such a dilemma. Next, let the editor guide you to learn how to deal with these situations. ! I hope you read it carefully and learn something!


In linux, the tar command can save many files together to a separate tape or disk for archiving, syntax tar [option] source file or directory; You can also restore the required files from the archive, that is, the reverse process of packaging, called unpacking, syntax tar [option] compressed package; you can also package and compress at the same time, syntax tar [option] compressed package source file or directory.

Detailed explanation of Linux tar packaging command

In the Linux system, the most commonly used archive (package) command is tar, which can pack many Files are saved together to a single tape or disk for archiving. Not only that, but the command can also restore the required files from the archive, which is the reverse process of packing, called unpacking.

A package archived using the tar command is usually called a tar package (tar package files all end in .tar).

tar command for packaging operations

When the tar command is used for packaging operations, the basic format of the command is:

[root@localhost ~]#tar [option] source file or directory

The commonly used options of this command and their respective meanings are shown in Table 1.


Table 1 Common options and meanings of tar packaging commands
OptionsMeaning
-cPackage multiple files or directories.
-AAppend tar file to archive.
-f packagenameSpecify the filename of the package. The extension of the package is used to identify the format for the administrator, so the extension must be specified correctly;
-vDisplays the process of packaging files;

It should be noted that when using the tar command to specify options, you do not need to enter - in front of the options. For example, using the cvf option and -cvf doesSame.

Let’s give you a few examples to see how to use the tar command to package files and directories.

Example 1: Packaging files and directories


[root@localhost ~]# tar -cvf anaconda-ks.cfg. tar anaconda-ks.cfg



The option "-cvf" is generally a customary usage. Remember to specify the file name after packaging when packaging. And use ".tar" as the extension. The same is true for packaging directories:


Example 2: Packing and compressing directories

First of all, the compression command cannot directly compress directories , the directory must be packaged with the tar command first, and then the packaged file can be compressed with the gzip command or the bzip2 command. For example:


[root@localhost ~]#ll -d test test.tar drwxr-xr-x 2 root root 4096 Jun 17 21:09 test -rw-r--r-- 1 root root 10240 Jun 18 01:06 test.tar #We have previously packaged the test directory into a test.tar file [root@localhost ~]# gzip test.tar [root@localhost ~]# ll test.tar.gz -rw-r--r-- 1 root root 176 Jun 18 01:06 test.tar.gz #gzip command will compress test.tar into test.tar.gz


tar command to unpack operation

When the tar command is used to unpack the tar package, the basic format of the command is as follows:


[root@localhost ~]#tar [option] compressed package


When used for unpacking, the commonly used options and meanings are shown in Table 2.

< /tr>

Table 2 Common options and meanings of tar unpacking
OptionsMeaning
-xUnpack the tar package.
-fSpecify the package name of the tar package to be decompressed.
-tOnly check which files or directories are in the tar package, and do not unpack the tar package.
-C directorySpecify the unpacking location.
-vDisplay the specific process of unpacking.

In fact, compared with unpacking and packing, it just replaces the packing option "-cvf" with "-xvf". Let's try:


[root@localhost ~]# tar -xvf anaconda-ks.cfg.tar #Unpack to the current directory


If the "-xvf" option is used, the files in the package will be unpacked to the current directory. If you want to specify the decompression location, you need to use the "-C (uppercase)" option. For example:


[root@localhost ~]# tar -xvf test.tar -C /tmp #Unpack the file package test.tar into the /tmp/ directory


If you only want to see which files are in the file package, you can unpack the option "-x " replaced with the test option "-t". For example:


[root@localhost ~]# tar -tvf test.tar


1.jpg

The tar command performs packaging and compression (decompression and unpacking) operations

You may think that Linux is too unintelligent, a package compression, it is actually packaged into ".tar" " format, and then compressed into ".tar.gz" or ".tar.bz2" format. In fact, the tar command can be packaged and compressed at the same time. In the previous explanation, the packaging and compression are separated to let everyone understand the difference between packaging and compression in Linux.

When the tar command performs packaging and compression operations at the same time, its basic format is as follows:


[root@localhost ~]#tar [option] compressed package source File or directory


There are two commonly used options here, namely:

  • -z< /code>: compressionand decompress ".tar.gz" format;

  • -j: compress and decompress ".tar.bz2" format.

Example 1: Compression and decompression ".tar.gz" format


[ root@localhost ~]# tar -zcvf tmp.tar.gz /tmp/ #Pack and compress the /temp/ directory directly into ".tar.gz" format, use "-z" to identify the format, "-cvf" is consistent with the packaging option


Decompression is just adding a "-z" option in front of the unpacking option "-xvf".


[root@localhost ~]# tar -zxvf tmp.tar.gz #Decompression and unpacking ".tar.gz" format


The option "-C" mentioned earlier is used to specify the decompression location, "-t" is used to view the compression Package content, the same applies here.

Example 2: Compression and decompression ".tar.bz2" format

The only difference between ".tar.gz" format and ".tar.gz" format is "-zcvf The " option was replaced with "-jcvf", as follows:


[root@localhost ~]# tar -jcvf tmp.tar.bz2 /tmp/ #Pack and compress into ".tar.bz2" format, pay attention to the file name of the compressed package [root@localhost ~]# tar -jxvf tmp.tar.bz2 #Decompression and unpacking ".tar.bz2" format


Compress the file directly into ".tar.gz" and ".tar.bz2" format, which is the The most commonly used compression method in Linux, this is the compression and decompression method that everyone must master.

The tar command was originally used to create backups on tape, but now it can be created on any device. A large number of files and directories can be packaged into one file by using the tar command, which is very useful for backing up files or combining several files into one file for network transmission.


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us