The command commonly used to compress files in Linux - tar

Generally, when we use a computer to transfer emails to the mailbox of the other party, when encountering a relatively large file, we usually compress the file to be transferred, and then transfer it to the mailbox of the other party. This is because the compressed file can reduce the file size. The size allows us to transfer files to the other party's mailbox quickly and conveniently.

The second situation is that when we need to download the installation package from the official website, the files downloaded to the local are usually compressed packages, because compressed files can reduce the file size, thereby increasing the download speed of the file.

Another situation is that when the file is very large, it will occupy the disk space of the computer. In order to deal with this problem, we can pack and compress the large file to greatly reduce the disk space occupied by the file. When we need to use it, just decompress the compressed file.

If you feel that just packing and compressing is not safe enough, you can also encrypt the compressed file to improve the security of the file.


Similarly, in Linux, we generally have the above requirements. On Windows, we only need to download a WinRAR software to compress and decompress the target file at any time, but in Linux, the target file cannot be compressed and decompressed through the graphical interface. Compression and decompression. At this time, we need to use the frequently used tar command

The tar command is an archive compression command commonly used in Linux systems. It can pack several files or directories into a package and compress them to facilitate operations such as transmission and backup. The following are the usage details of the tar command:

  1. basic grammar
tar [选项] 归档文件 源文件或目录
  1. Common options

-c: Create a package file in .tar format, -c has the same effect as c, - can be omitted

-x: Unpack the package file in .tar format

-z: Call the gzip program for compression

-J: Call the xz program for compression (J is uppercase)

-f: Indicates the use of archive files, f must be placed at the end

-t: List the files in the package

-C: Specify the storage path of the decompressed file

  1. example
3.1 Compressed files
tar -czf wonderful.tar.gz pm

The function of this command is to create a package file ending in .tar.gz, and then call the gzip program to compress the pm folder in the current directory into the file ending in .tar.gz


tar czf  /lianxi/fangjie/passwd.tar.gz   /etc/passwd

The function of this command is to create a package file ending in .tar.xz, and then call the xz program to compress the pm folder in the current directory into the file ending in .tar.xz. Among them, the compression effect of the xz program is good, but its compression time is relatively long

3.2 Unzip the file
tar -xf wonderful.tar.gz

By default, the files or folders in the compressed package will be decompressed to the current directory


tar -xf wonderful.tar.gz -C /lianxi/fangjie

The function of this command is to decompress the wonderful.tar.gz compressed file in the current folder to the /lianxi/fangjie directory, which is the function of the -C option - specify the storage location of the decompressed file


3.3 View compressed files
tar -tf /lianxi/wonderful/wonderful.tar.gz

View the folders and files contained in this compressed package

Guess you like

Origin blog.csdn.net/m0_53891399/article/details/129740504