Brief introduction
The use of zip, rar, and Tar is described in the compression/decompression tools commonly used on Linux.
File packaging and compression
The compressed package file format on Linux, in addition to the most common Windows *.zip, *.rar,. 7z suffix compressed files, as well as. GZ,. XZ,. bz2,. Tar,. tar.gz, . TAR.XZ,tar.bz2
file suffix name |
Description |
*.zip |
Zip program to package compressed files |
*.rar |
RAR Program Compressed files |
*.7z |
7zip Program Compressed Files |
*.tar |
Tar program packaging, uncompressed files |
*.gz |
Gzip (GNU Zip) compressed files |
*.xz |
XZ Program Compressed Files |
*.bz2 |
Tar packaging, gzip program compressed files |
*.tar.gz |
Tar packaging, gzip program compressed files |
*.tar.xz |
Tar packaging, XZ program compressed files |
*.tar.bz2 |
Tar package, BZIP2 program compressed files |
*.tar.7z |
Tar pack, 7z program compressed files |
1 Zip compression Wrapper
Use Zip to package files
# 将 test 目录打包成一个文件,-r 表示递归打包包含子目录的全部内容,-q 表示安静模式,-o 表示输出文件,其后紧跟打包输出文件名zip -r -q -o test.zip /home/test# 使用 du 命令查看打包后文件的大小du -h test.zip# 使用 file 命令查看文件大小和类型file test.zip
Set the compression level to 9 and 1 (9 max, 1 min), repack
# 1表示最快压缩但体积大,9表示体积最小但耗时最久,-x 排除上一次我们创建的zip文件,路径必需为绝对路径zip -r -9 -q -o test_9.zip /home/test -x ~/*.zipzip -r -1 -q -o test_1.zip /home/test -x ~/*.zip# 再用 du 命令分别查看默认压缩级别、最低、最高压缩级别及未压缩的文件的大小,-h 表示可读,-d 表示所查看文件的深度du -h -d 0 *.zip ~ | sort
Create an encrypted ZIP package
# 使用 -e 参数可以创建加密压缩包zip -r -q -o test.zip /home/test
Note: For the zip command, there are some compatibility issues with Linux/unix in the text file format, such as line breaks (for invisible characters), in Windows CR+LF (carriage-return+line-feed: Carriage return add line) , and on the Linux/unix for LF (newline), so if you edit the text on Linux without processing, the open on the Windows system may look like there is no line break. If you want to make a zip archive of Linux created on Windows without any problems, then you need to make some changes to the command
There are different types of variables in the shell that can participate in the operation, scoped to the scope
# 使用 -l 参数将 LF 转换为 CR+LFzip -r -l -o test.zip /home/test
2 Unzip the zip file using the unzip command
3 RAR Packaging Compression command
On Linux, RAR and Unrar tools can be used to create and decompress RAR packages respectively.
Installing RAR and Unrar tools
updatesudo apt-get install rar unrar
Create a compressed package or add a file to a compressed package from a specified file or directory
rm *.zip# 使用a参数添加一个目录~到一个归档文件中,如果该文件不存在就会自动创建rar a test.rar .
Note: RAR command parameters do not-if added will error.
To delete a file from the specified compressed package file
rar d test.rar .bashrc
View unresolved files
rar l test.rar
Extracting RAR files using Unrar
# 全路径解压unrar x test.rar# 去掉路径解压mkdir tmpunrar e test.rar tmp/
4 Tar Packaging tools
More commonly used in Linux is the Tar tool, Tar was originally a packaging tool, but also to implement a tool such as 7Z,GZIP,XZ,BZIP2 support, the compression tools themselves can only achieve the file or directory (separate compressed directory files) compression, Does not implement the packaging of the file compression, so we do not have to learn a few other tools, tar decompression and compression are the same command, only the parameters are different, the use of more convenient.
Create a tar Package
# -c 表示创建一个 tar 包文件,-f 用于指定创建的文件名,注意文件名必须紧跟在 -f 参数之后# 会自动去掉表示绝对路径的 /,你也可以使用 -P 保留绝对路径符tar -cf test.tar ~
Unpack a file (-x parameter) to the existing directory of the specified path (-c parameter)
mkdir tardirtar -xf test.tar -C tardir
View only unresolved package file-T parameter
tar -tf test.tar
Keep file attributes and follow links (symbolic links or soft links), sometimes we use tar to back up files when you restore to other hosts you want to keep the properties of the file (-p parameter) and the source file that the backup link points to instead of the link itself (-H parameter)
tar -cphf etc.tar /etc
-
To use the Gzip tool to create a *.tar.gz file as an example, simply add the-Z parameter to the build of the tar file, use gzip to compress the file
tar -czf etc.tar.gz ~
Unzip the *.tar.gz file
tar -xzf etc.tar.gz
Now we're going to create or unzip the file using the other compression tools just to change one parameter:
compressed file format |
Parameters |
*.tar.gz |
-Z |
*.tar.xz |
-j |
*tar.bz2 |
-j |
File packaging and compression for Linux