Home  >  Article  >  System Tutorial  >  Compression command in linux

Compression command in linux

王林
王林forward
2024-02-13 10:54:03671browse

There are mainly .zip, .gz, .bz2, .tar.gz and .tar.bz2 compression formats in Linux

1. .zip, .gz, .bz2 format

.zip format syntax:

  • zip compressed file name source file #Compressed file (directories can also be compressed, but only the first directory will be compressed, and the contents in the directory will not be compressed)
  • zip -r compressed file name source directory #compressed directory
  • unzip compressed file name #unzip file

Compress the 11.txt file into: 11.zip file: zip 11.zip 11.txt

Compression command in linux

Compress the coding directory into: coding.zip file: zip -r coding.zip coding (As you can see from the picture below, all the contents in the directory will be compressed)

Compression command in linux

Compress the coding directory into: coding.zip file: zip coding.zip coding (it only compresses coding/, which does not contain the original content of the coding folder)

Compression command in linux

Decompress the coding.zip file (the coding.zip file obtained in the picture above), you can see that all the original first.c and other files are gone

Compression command in linux

.gz format syntax:

  • gzip source file #Compress to a compressed file in .gz format, the source file will disappear

  • gzip -c source file > compressed file #Compress to .gz format, keep the source file. For example: gzip-c cangls>cangls.gz

  • gzip -r directory #Compress all subfiles in the directory, but the directory cannot be compressed

  • gunzip compressed file name #unzip compressed file

Compression command in linux


.bz2 format syntax

  • bzip2 source file #Compress to .bz2 format, do not retain the source file
  • bzip2 -k source file #Keep the source file after compression
  • Note: The bzip2 command cannot compress directories
  • bzip2 -d compressed file #Decompress, -k retain compressed file
  • bunzip2 compressed file #Decompress, -k retains the compressed file
Compression command in linux

2. .tar.gz, .tar.bz2 format

In response to the shortcoming that the .gz and .bz2 formats cannot compress directories, Linux can solve this problem by first packaging all the files in a directory through the tar command, and then compressing them into .gz or .bz2 formats.

Packaging command tar

  • tar-cvf package file name source file
  • Options: -C: Packaging -V: Display process -f: Specify the packaged file name

For example:

Compression command in linux

.tar.gz compression format In fact, the .tar.gz format is first packaged into .tar format and then compressed into .gz format

  • tar-zcvf compressed package name # -z: compress to .tar.gz format (the source file will be retained), the absolute path can be added before the compressed package name
  • tar-zxvf compressed package name # -x: decompress .tar.gz format (the original compressed file will be retained)
  • tar-ztvf compressed package name # -t: Do not decompress the compressed package, directly view the contents of the compressed package
Compression command in linux

.tar.bz2 compression format

  • tar -jcvf compressed package name.tar.bz2 source file
  • tar -jxvf compressed package name.tar.bz2
  • tar -jtvf #View the contents of the compressed package without decompressing the file
Compression command in linux

In Linux, .tar.gz and .tar.bz2 are the two most common compressed file formats. From the above practice, we can see that these two file formats can easily compress files. and directory, you can also view the contents contained in the compressed package, so these two formats need to be mastered proficiently. As for the .zip, .gz, and .bz2 formats, it is enough to understand them. No proficiency is required. When using them, just a little bit Impression, just find Du Niang.

The above is the detailed content of Compression command in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete