1 introduction

Tar functions like WinRAR in Windows. It can package multiple directories or files into a single large file and compress files in the process with gzip/bzip2/xz support 1.

2 option

Tar has many options, including the following:

Option parameters instructions Help memory
-c Create a packaged file, usually with -v to view the packaged file name create
-t View the list of files included in the package file contents
-x Decompress the package to a specified directory extract
-z Use gzip to compress and decompress the file. The file name is *.tar.gz gzip
-j Use bzip2 for compression and decompression. The file name is usually *.tar.bz2
-J Xz is used for compression and decompression. The file name is *.tar.xz
-v During compression/decompression, prints the file name being processed verbose
-f filename Whatever comes after the -f space,tar is assumed to be the name of the package file file
-C Used to specify the decompression directory Change
-p Retain the original permissions and attributes of backup data perserve
-P Backup data can contain a root directory
–exclude=FILE Some files are excluded during compression

Or too many to remember? Just remember these most common commands:

  • Gzip compression: tar -zcvf filename.tar.gz Name of the file or directory to be compressed

  • To query the compressed package, run the tar -ztvf filename.tar.gz command

  • Gzip To decompress the file: tar -zxvf filename.tar.gz -c Directory to be decompressed

To summarize: z is gzip; J for bzip2; J for xz. C is compression; T is query; X is the decompression. And then vf. F must be the last one; if it comes before any other option, tar will get the filename wrong. For example, the tar -zcfv XXX command will assume that the compressed file is named v!

3 File name specifications

The compressed file name is suffixed with the response according to the compression algorithm:

algorithm File name specification
gzip filename.tar.gz
bzip2 filename.tar.bz2
xz filename.tar.xz

We can only solve it if we know the compression algorithm, can’t we? So file name specifications are important.

4 Packing Example

4.1 Basic Usage

Suppose we need to back up the /etc folder into a compressed package 1. This is common in practice because the /etc folder stores the main configuration files of the system. In English the full word is etc., which means “etc.” followed by a period.

Next, we use gzip, bzip2, and Xz algorithms respectively to pack and compress the /etc folder.

Gzip compression command: time tar -zpcvf /root /etc/tar. gz /etc

Bzip2 compression command: time tar -jpcvf /root /etc/tar. bz2 /etc

Xz compression command: time tar -jpcvf /root /etc/tar.xz /etc

As you can see from Real, Gzip has the fastest compression speed and XZ has the slowest.

In terms of size, XZ has the highest compression rate and GZIP the worst.

High compression rate, naturally need time cost. Therefore, the folder that needs to be compressed is very large, so it is necessary to consider the time cost. Because gzip compression is fast, this is one of the reasons *.tar.gz is popular.

4.2 Partial Packaging

Consider a scenario where we need to package a folder (/root), but a file or subfolder in that folder (prefixed with etc) does not want to package it. So you want this folder to be partially packaged and partially unpackaged. You can execute the following commands:

tar -zcvf /root/test.tar.gz --exclude=/root/etc* --exclude=/root/test.tar.gz /root

–exclude=/root/test.tar.gz –exclude=/root/test.tar.gz

Tar -ztvf test.tar.gz to view the list of files in the newly added package:

5 Viewing Examples

Run the tar -ztvf et.tar. gz command to view the compressed package. For example, gz is used as the suffix. Therefore, -z is added to indicate that the gzip algorithm is used for decompression.

Partial output:

You can see that this command also prints out the permissions, accounts, and owning groups for the file.

And, crucially, none of these files have a root directory. If the root directory is not removed, the extracted file name will be an absolute path, meaning that the extracted data will be placed in /etc/xxx. In this case, the system’s /etc folder will be overwritten, with serious consequences. Therefore, the tar packaging directive by default removes the root directory of the files placed in the package. Unless the root directory for these files is displayed through -p.

6 Decompress example

(1) Completely decompress

Run the tar -zxvf et.tar. gz -c/TMP command to decompress /etc/tar. gz to/TMP. After the decompression is successful, you will see the /etc folder under/TMP.

If you want to decompress the file in the local directory, run the tar -zxvf et.tar. gz command.

(2) Partial decompression

Suppose we need to extract vconsole.conf from the package (etc. Tar. gz). Run the following command to query the relative path of the file:

The tar - ZTVF etc. Tar. Gz | grep "console"

After obtaining the file path to decompress, run the tar -zxvf et.tar. gz /etc/vconsole. conf command

If you go to the etc folder of the current directory, you can see that vconsole.conf has been extracted:


[1] Bird Brother. Bird Brother’s Linux Private Kitchen Basic Learning [M]. The fourth edition. Beijing: Posts and Telecommunications Press, 2018:359-362.