Linux file packaging and compression
Link: https://www.cnblogs.com/yang877/p/5860798.html
Introduction
Commonly used compression/decompression tools on Linux, introducing the use of zip, rar, and tar.
File packaging and compression
The compressed file formats on Linux include the most common *.zip, *.rar, and .7z suffixes on Windows, as well as .gz, .xz, .bz2, .tar, .tar.gz, .tar.xz, and tar.bz2.
File extension | illustrate |
---|---|
*.zip | zip program packages compressed files |
*.rar | Files compressed by the rar program |
*.7z | Files compressed by the 7zip program |
*.tar | tar program package, uncompressed file |
*.gz | Files compressed by the gzip program (GNU zip) |
*.xz | Files compressed by the xz program |
*.bz2 | tar package, gzip program compressed file |
*.tar.gz | tar package, gzip program compressed file |
*.tar.xz | tar package, xz program compressed file |
*.tar.bz2 | tar package, bzip2 program compressed file |
*.tar.7z | tar package, 7z program compressed file |
1 zip compression packaging program
-
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 is the largest, 1 is the smallest), and repack
# 1表示最快压缩但体积大,9表示体积最小但耗时最久,-x 排除上一次我们创建的zip文件,路径必需为绝对路径
zip -r -9 -q -o test_9.zip /home/test -x ~/*.zip
zip -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: Regarding the zip command, due to some compatibility issues between Windows and Linux/Unix in text file formats, such as line breaks (invisible characters), which are CR+LF (Carriage-Return+Line-Feed) on Windows and LF (line feed) on Linux/Unix, if you do not process it, the text edited on Linux may not appear to have line breaks when opened on a Windows system. If you want the zip compressed file you created on Linux to be unzipped on Windows without any problems, you also need to make some changes to the command. Variables
in the shell have different types, can participate in calculations, and have scope limitations.
# 使用 -l 参数将 LF 转换为 CR+LF
zip -r -l -o test.zip /home/test
2 Use the unzip command to decompress the zip file
-
Use zip to package files
# 将 test.zip 解压到当前目录
unzip test.zip
# 使用安静模式,将文件解压到指定目录
unzip -q test.zip -d ziptest
# 不想解压,只想查看压缩包的内容可以使用 -l 参数
unzip -l test.zip
# Linux 上面默认使用的是 UTF-8 编码,防止解压后出现中文乱码,要用参数 -O
unzip -O GBK 中文压缩文件.zip
3 rar packaging compression command
On Linux, you can use rar and unrar tools to create and decompress rar archives respectively.
-
Install rar and unrar tools
sudo apt-get update
sudo apt-get install rar unrar
-
Create a compressed archive from the specified file or directory or add files to the compressed archive
rm *.zip
# 使用a参数添加一个目录~到一个归档文件中,如果该文件不存在就会自动创建
rar a test.rar .
Note: There is no - in the rar command parameter. If you add it, an error will be reported.
-
Delete a file from the specified compressed file
rar d test.rar .bashrc
-
View unzipped files
rar l test.rar
-
Use unrar to extract rar files
# 全路径解压
unrar x test.rar
# 去掉路径解压
mkdir tmp
unrar e test.rar tmp/
4 tar packaging tool
The tar tool is more commonly used on Linux. Tar was originally just a packaging tool, but it also supports 7z, gzip, xz, bzip2 and other tools. These compression tools can only compress files or directories (compress files in a directory separately), but do not realize the packaging and compression of files. Therefore, we do not need to learn other tools separately. The decompression and compression of tar are the same command, only the parameters are different, which is more convenient to use.
-
Creating a tarball
# -c 表示创建一个 tar 包文件,-f 用于指定创建的文件名,注意文件名必须紧跟在 -f 参数之后
# 会自动去掉表示绝对路径的 /,你也可以使用 -P 保留绝对路径符
tar -cf test.tar ~
-
Unpack a file (-x parameter) to an existing directory in the specified path (-C parameter)
mkdir tardir
tar -xf test.tar -C tardir
-
Only view the file without unpacking -t parameter
tar -tf test.tar
-
Preserve file attributes and follow links (symbolic links or soft links). Sometimes we use tar backup files when you restore them on other hosts. You want to preserve the file attributes (-p parameter) and backup the source file pointed to by the link instead of the link itself (-h parameter)
tar -cphf etc.tar /etc
-
Take the example of using the gzip tool to create a *.tar.gz file. Just add the -z parameter to create a tar file and use gzip to compress the file.
tar -czf etc.tar.gz ~
-
Unzip the *.tar.gz file
tar -xzf etc.tar.gz
Now we need to use other compression tools to create or decompress the corresponding files by just changing one parameter:
|压缩文件格式 |参数 |
|:-----------:|:--:|
|*.tar.gz | -z |
|*.tar.xz | -J |
|*tar.bz2 | -j |
autumn
The recruitment has already begun. If you are not well prepared,
autumn
It is difficult to find a good job.
Here is a big employment gift package for everyone. You can prepare for the spring recruitment and find a good job!