Common compression and decompression commands in Linux include: tar, gzip, gunzip, bzip2, bunzip2, compress, uncompress, zip, unzip, rar, unrar, etc.
tar
The most commonly used packaging command is tar. Packages created with the tar program are often called tar packages, and tar package file names usually end with .tar. After generating a tar package, it can be compressed with other programs, so let's first talk about the basic usage of the tar command.
The tar command has many options (you can view them with man tar), but only a few are commonly used. Let's illustrate with examples:
# tar -cf all.tar *.jpg
This command packages all .jpg files into a package named all.tar. -c means creating a new package, -f specifies the package file name.
# tar -rf all.tar *.gif
This command adds all .gif files to the all.tar package. -r means adding files.
# tar -uf all.tar logo.gif
This command updates the logo.gif file in the original tar package all.tar. -u means updating files.
# tar -tf all.tar
This command lists all files in the all.tar package. -t means listing files.
# tar -xf all.tar
This command extracts all files from the all.tar package. -x means extracting.
The above is the most basic usage of tar. To make it convenient for users to compress or decompress files while packaging or unpacking, tar provides a special feature. That is, tar can call other compression programs while packaging or unpacking, such as gzip, bzip2, etc.
1) tar invocation
gzip is a compression program developed by the GNU organization. Files ending with .gz are the result of gzip compression. The decompression program corresponding to gzip is gunzip. In tar, the -z parameter is used to call gzip. Let's illustrate with examples:
# tar -czf all.tar.gz *.jpg
This command packages all .jpg files into a tar package, compresses it with gzip, and generates a gzip-compressed package named all.tar.gz.
# tar -xzf all.tar.gz
This command extracts the package produced above.
2) tar calling bzip2
bzip2 is a compression program with stronger compression capability. Files ending with .bz2 are the result of bzip2 compression.
The decompression program corresponding to bzip2 is bunzip2. In tar, the -j parameter is used to call gzip. Let's illustrate with examples:
# tar -cjf all.tar.bz2 *.jpg
This command packages all .jpg files into a tar package, compresses it with bzip2, and generates a bzip2-compressed package named all.tar.bz2
# tar -xjf all.tar.bz2
This command extracts the package produced above.
3) tar calling compress
compress is also a compression program, but it seems fewer people use compress than gzip and bzip2. Files ending with .Z are the result of bzip2 compression. The decompression program corresponding to compress is uncompress. In tar, the -Z parameter is used to call compress. Let's illustrate with examples:
# tar -cZf all.tar.Z *.jpg
This command packages all .jpg files into a tar package, compresses it with compress, and generates a package compressed by uncompress, named all.tar.Z.
# tar -xZf all.tar.Z
This command extracts the package produced above.
With the above knowledge, you should be able to extract various compressed files. Below is a summary of the tar series of compressed files:
1) For files ending with .tar
tar -xf all.tar
2) For files ending with .gz
gzip -d all.gz gunzip all.gz
3) For files ending with .tgz or .tar.gz
tar -xzf all.tar.gz tar -xzf all.tgz
4) For files ending with .bz2
bzip2 -d all.bz2 bunzip2 all.bz2
5) For files ending with tar.bz2
tar -xjf all.tar.bz2
6) For files ending with .Z
uncompress all.Z
7) For files ending with .tar.Z
tar -xZf all.tar.z
In addition, for common compressed files .zip and .rar on Windows, Linux also has corresponding methods to extract them:
1) For .zip
Linux provides the zip and unzip programs. zip is a compression program, and unzip is a decompression program. They have many options. Here is only a brief introduction, still using examples to illustrate their usage:
# zip all.zip *.jpg
This command compresses all .jpg files into a zip package:
# unzip all.zip
This command extracts all files from all.zip.
2) For .rar
To process .rar files on Linux, you need to install RAR for Linux. Download address:http://www.rarsoft.com/download.htm, just install it after downloading.
# tar -xzpvf rarlinux-x64-5.6.b5.tar.gz # cd rar # make
Then it is installed. After installation, there are two programs, rar and unrar. rar is a compression program, and unrar is a decompression program. They have many options. Here is only a brief introduction, still using examples to illustrate their usage:
# rar a all *.jpg
This command compresses all .jpg files into a rar package named all.rar. The program will automatically append the .rar extension to the package name.
# unrar e all.rar
This command extracts all files from all.rar.
# unrar x all.rar
This command extracts all files from all.rar, preserving the directory hierarchy.
Extended content
tar
-c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件
These five are independent commands. One of them is needed for compression and decompression. They can be used with other commands, but only one of them can be used. The following parameters are optional when compressing or decompressing archives as needed.
-z:有gzip属性的 -j:有bz2属性的 -Z:有compress属性的 -v:显示所有过程 -O:将文件解开到标准输出
The following parameter -f is required:
-f: 使用档案名字,切记,这个参数是最后一个参数,后面只能接档案名。# tar -cf all.tar *.jpg
This command packages all .jpg files into a package named all.tar. -c means creating a new package, -f specifies the package file name.
# tar -rf all.tar *.gif
This command adds all .gif files to the all.tar package. -r means adding files.
# tar -uf all.tar logo.gif
This command updates the logo.gif file in the original tar package all.tar. -u means updating files.
# tar -tf all.tar
This command lists all files in the all.tar package. -t means listing files.
# tar -xf all.tar
This command extracts all files from the all.tar package. -x means extracting.
Compression
tar –cvf jpg.tar *.jpg // 将目录里所有jpg文件打包成 jpg.tar tar –czf jpg.tar.gz *.jpg // 将目录里所有jpg文件打包成 jpg.tar 后,并且将其用 gzip 压缩,生成一个 gzip 压缩过的包,命名为 jpg.tar.gz tar –cjf jpg.tar.bz2 *.jpg // 将目录里所有jpg文件打包成 jpg.tar 后,并且将其用 bzip2 压缩,生成一个 bzip2 压缩过的包,命名为jpg.tar.bz2 tar –cZf jpg.tar.Z *.jpg // 将目录里所有 jpg 文件打包成 jpg.tar 后,并且将其用 compress 压缩,生成一个 umcompress 压缩过的包,命名为jpg.tar.Z rar a jpg.rar *.jpg // rar格式的压缩,需要先下载 rar for linux zip jpg.zip *.jpg // zip格式的压缩,需要先下载 zip for linux
Decompression
tar –xvf file.tar // 解压 tar 包 tar -xzvf file.tar.gz // 解压 tar.gz tar -xjvf file.tar.bz2 // 解压 tar.bz2 tar –xZvf file.tar.Z // 解压 tar.Z unrar e file.rar // 解压 rar unzip file.zip // 解压 zip
Summary
1、*.tar 用 tar –xvf 解压 2、*.gz 用 gzip -d或者gunzip 解压 3、*.tar.gz和*.tgz 用 tar –xzf 解压 4、*.bz2 用 bzip2 -d或者用bunzip2 解压 5、*.tar.bz2用tar –xjf 解压 6、*.Z 用 uncompress 解压 7、*.tar.Z 用tar –xZf 解压 8、*.rar 用 unrar e解压 9、*.zip 用 unzip 解压