Linux environment common packing, compression, decompression command (tar, gzip, bzip2, zip)

Foreword

Often people who use computers often come into contact with the compressed file, whether it is software, data or information, to download later is usually a compressed package, on the Windows platform if the WinRAR or 360 compressed installation, no matter what the format of the compressed file, generally click right-extracting compressed file option to select, very convenient. Because for a long time on the Windows platform, easy-extracting file, led me to understand the errors packaged, compressed concept, there are a lot of questions when the results of the linux operating compressed files, and today finally understand that a special sum up, while the list of commonly used compression , unzip command, easy to find later use.

operating a compressed file from the command also implemented on linux, but there are a lot of compressed file extension, such as .tar.gz, .tar.bz2, .gz, .zip, .Zetc., and to generate and extract the command of these documents are also many, such as tar, gzip, bzip2, zip, unzipand so on, we see people get confused, the process also often memories deviation, not a command does not correspond to that parameter error, especially those not commonly used compression format, often need to query attempt, wasted a lot of time, in fact, the cause of these problems or because the concept of packaged compressed It is not clear, then first look at these concepts.

Basic concepts

Often operate directly on the Windows graphical interface in the compression and decompression of files, this operation will cause my behavior brought on linux, rather than actually compress and decompress files as well as an operation on linux is "packed" because linux compression and decompression is generally act on a file, if you want to eventually become a bunch of file compression file, you need to be labeled as a package, then this package file is compressed.

Packing / Archive

Packaged or call archive is multiple files and directories (can also be a file) becomes a general file, but not all the files are fused using the tarcommand.

compression

Compression is a large file into a smaller file by a specific compression algorithm as far as possible, can reduce the storage space, speed up network transmission efficiency, use gzip, bzip2, zipand other commands.

Decompression

The final compression decompression is a small file that is generated is reduced to compress large files before you can use gzip, gunzip, bunzip2, unzipand other commands.

Compression packing

By the above concepts parsing we can know that we said before compression operation usually refers to packing and compression in two steps, since most of linux commands are compression can only compress a file, so before compression needs to be compressed All files to be packaged, compressed and then generates a file operation.

Understand the compression and packaging operations of meaning, we can through some naming convention, choose the appropriate compression and decompression method, such as following files:

  • xxx.tar : This is an archive file, which is only by tarcarried out the packaging operation
  • xxx.tar.gz : This is a compressed file, and then packaged in a gzipway is compressed
  • xxx.tar.bz2 : This is a compressed file, and then packaged in a bzip2way is compressed
  • xxx.gz : This is a compressed file that has not been packaged well, but the gzipway the compression

If you can generate a compressed file named in accordance with these rules, then unzip the file when a lot easier, but sometimes compress the file extension is not standard, you can fileview the actual file format, use the following command:

[albert@localhost#15:03:05#/home/albert/compress]$file test.tar.bz2
test.tar.bz2: bzip2 compressed data, block size = 900k

[albert@localhost#15:03:24#/home/albert/compress]$file test.tar.gz
test.tar.gz: gzip compressed data, from Unix, last modified: Wed Nov  6 12:02:05 2019

Compression decompression command

Compressed file format and command really is too much, it is a commonly used commands in this summary table, to facilitate future when needed directly brought on by speed up to solve the problem. Assuming that the original file is a.log and b.txt, there is a current directory output directory, the directory as unpacked file is stored, so commonly used to compress and decompress command as follows:

file format Compression command Command Notes Unzip command Command Notes
xxx.tar tar -cvf test.tar a.log b.txt - tar -xvf test.tar -C ./output Do not use -Cit unpacked in the current directory
xxx.tar.gz tar -zcvf test.tar.gz a.log b.txt - tar -zxvf test.tar.gz -C ./output Do not use -Cthe decompression in the current directory
xxx.tar.bz2 tar -jcvf test.tar.bz2 a.log b.txt - tar -jxvf test.tar.bz2 -C ./output Do not use -Cthe decompression in the current directory
xxx.tar.Z tar -Zcvf test.tar.Z a.log b.txt - tar -Zxvf test.tar.Z -C ./output Do not use -Cthe decompression in the current directory
xxx.gz gzip -c a.log > test.gzgzip a.log The former retained a.log, the latter delete a.log gzip -d test.gzgunzip test.gz You can not specify extracting file storage directory
xxx.bz2 bzip2 -c a.log > test.bz2bzip2 a.log The former retained a.log, the latter delete a.log bzip2 -d test.bz2bunzip2 test.bz2 You can not specify extracting file storage directory
xxx.Z compress -c a.log > test.Zcompress a.log The former retained a.log, the latter delete a.log compress -d test.Zuncompress test.Z You can not specify extracting file storage directory
xxx.rar rar a test.rar a.log - unrar e test.rar The eoption to replace the xdirectory can be specified
xxx.zip zip test.zip a.log b.txt - unzip test.zip -d ./output Do not use -dthe decompression in the current directory

Contrast the above analysis of compression can also extract the command can be found, tarthis command can be packaged and compressed merged together, you can also extract the reconciliation package merged together, only need to modify the parameters of options you can call a different program compress or decompress, such as -cvfrepresents not only packing compression, and -zcvfrepresents the package after use gzipcompression instead -jcvfexpress package after use bzip2compression, in fact, there are a lot of compression, you can refer to tarhelp document command, the specific compression options as follows.

Compression options:
-a, --auto-the compress use archive suffix to determine the compression program
-I, --use-compress-program = PROG filter through PROG (must be able to accept the program -d option)
-j, - bzip2 filter the archive through bzip2
-J, --xz filter archive through xz
-lzip by filtration lzip archive
-lzma filtered through archives LZMA
-lzop
the -no-the compress does not use Auto-archive suffix to determine the compression program
-z, --gzip , --gunzip, --ungzip by filtration gzip archive
-Z, --compress, --uncompress filtered through compress archived

to sum up

  • These commands only basic usage, there are many parameters selection does not mention, for example, tar -tf test.taryou can not view the contents directly extract the archive.
  • gzipCommand can compress a file, if a plurality of files after the command is added, it will generate a plurality of files are compressed.
  • It is said that compressthe command is a very old unix file compression instruction, now basically gzipcommand replaces.
  • As more command involved in the text, it is inevitable that some clerical error, in order not to spread wrong usage, I have conducted a number of checks, if we also found other errors, welcome criticism.
Published 150 original articles · won praise 277 · views 530 000 +

Guess you like

Origin blog.csdn.net/shihengzhen101/article/details/102938754