打包是将多个文件放在一个文件中,打包成一个文件后,可以对文件进行特定格式的压缩,解压后将保留文件打包压缩前的属性。
[En]
Packaging is to put multiple files in a file, and after being packaged into a file, the file can be compressed in a specific format, and the properties of the file before packaging and compression will be retained after decompression.
tar:(Tape ARchive) 磁带归档,实现对目录和多个文件打包一个文件,并且可以压缩,保留文件属性不丢失,常用于备份功能,tar是linux的一个常用打包工具。
1、打包:
打包过程不是压缩的,但多个文件放在同一个包中。
[En]
The packaging process is not compressed, but multiple files are put in the same package.
格式: tar -cf 打包成的文件名.tar 需要打包的文件
注意: -f必须放在参数的最后面,用于指定打包成的文件
例如:
将当前目录中的所有文件打包到一个文件中<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>Package all files in the current directory into one file</font>*</details>
ehigh@ubuntu:~/0324/test$ ls
1.txt 2.txt 3.txt 4.txt
进入该目录下进行打包
ehigh@ubuntu:~/0324/test$ tar -cf txt.tar *
检查包装信息。打包不会影响源文件。源文件仍然存在。<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>Check the packaging information. Packaging will not affect the source file. The source file still exists.</font>*</details>
ehigh@ubuntu:~/0324/test$ ls
1.txt 2.txt 3.txt 4.txt txt.tar
注意:打包的时候, “ 需要打包的文件”
要采用相对路径的形式
如果使用的是绝对路径会出现以下问题:
- 将把整条路都塞进去。
[En]
will pack the whole path in.*
- 当被压缩文件是绝对路径是,会报一个错误:tar: 从成员名中删除开头的”/”
例如:需要将/home/ehigh/text1/tom/bob下的所有文件打包成一个text.tar.gz的文件 ehigh@ubuntu:~/text1/tom/bob$ tar -czf /home/ehigh/test.tar.gz /home/ehigh/text1/tom/bob/ tar: Removing leading `/' from member names ehigh@ubuntu:~$ ls | grep test.tar.gz test.tar.gz解压这个文件 ehigh@ubuntu:~$ tar -xzf test.tar.gz结果发现,整条路都被打包了。<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>It was found that the whole path was packaged.</font>*</details> ehigh@ubuntu:~/home/ehigh/text1/tom/bob$ pwd /home/ehigh/home/ehigh/text1/tom/bob
打包时,请跳过指定的目录,不能添加斜杠,否则将打包该目录下的所有文件。<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>When packing, skip the specified directory and cannot add a slash, otherwise all the files in this directory will be packaged.</font>*</details> tar -czf file_name.tar.gz --exclude='/path/dir' file_name打包的时候跳过特定文件 tar -czf file_name.tar.gz --exclude='/path/file_name' file_name打包时跳过指定类型的文件<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>Skip files of the specified type when packing</font>*</details> tar -czf file_name.tar.gz --exclude='*.log' file_name说明:支持通配符,如果需要排除多个文件就使用多个 --exclude
例如:
tar -czf /home/ehigh/work/PythonWorker.tar.gz /home/ehigh/work/PythonWorker
--exclude='/home/ehigh/work/PythonWorker/Worker/*/NVR_SDK'
# 排除匹配到路径中 Worker/*/NVR_SDK 的目录,其中 * 表示匹配任意子目录。 Bash 的路径中可以使用通配符
打包时,使用指定的压缩工具对包文件进行压缩。
[En]
When packing, use the specified compression tool to compress the package file.
格式: tar -czf 需要压缩的文件 或 tar -cjf 需要压缩的文件
例如:
ehigh@ubuntu:~/0324$ tar -czf test.tar.gz test
ehigh@ubuntu:~/0324$ ls
test test.tar.gz
ehigh@ubuntu:~/0324$ tar -cjf test.tar.bz test
ehigh@ubuntu:~/0324$ ls
test test.tar.bz
2、解包
作用:把一个包拆开,露出里面的文件。如果时一个压缩过的包,会自动检测压缩使用的算法来解包。
格式: tar -xf 包名
注意:使用tar解包会默认覆盖同名文件
例如:
打包前的文件名
ehigh@ubuntu:~/0324/test$ cat *
2221.txt
12342.txt
3333.txt
1231234.txt
进行打包
ehigh@ubuntu:~/0324/test$ tar -cf txt.tar *
修改文件内容
ehigh@ubuntu:~/0324/test$ echo 1.txt > 1.txt
ehigh@ubuntu:~/0324/test$ echo 2.txt > 2.txt
ehigh@ubuntu:~/0324/test$ echo 3.txt > 3.txt
ehigh@ubuntu:~/0324/test$ echo 5.txt > 4.txt
解包
ehigh@ubuntu:~/0324/test$ tar -xf txt.tar
查看内容 源文件已经被默认覆盖了
ehigh@ubuntu:~/0324/test$ cat *
2221.txt
12342.txt
3333.txt
1231234.txt
格式: tar -xzf fine_name.tar.gz 或 tar -xjf file_name.tar.bz
说明:解包的时候会将同名文件默认覆盖。
例如:
解压gizp格式的压缩包
ehigh@ubuntu:~/0324$ tar -xzf test.tar.gz
解压bzip2格式的压缩包
ehigh@ubuntu:~/0324$ tar -xjf test.tar.gz
3、查看包中文件
实现以查看包中有哪些文件,如果包是压缩的,它将自动检测压缩算法。
[En]
Implement to see what files are in a package, and if the package is compressed, it will automatically detect the compression algorithm.
格式: tar -tf finename.tar
查看test.tar.bz 中有哪些文件
ehigh@ubuntu:~/0324$ tar -tf test.tar.bz
test/
test/1.txt
test/2.txt
test/3.txt
test/4.txt
4、参数说明
-f # 打包成一个文件(表示将结果输出到指定文件中)
-v # 显示过程
-c # 打包
-x # 解压文件
-z # 默认调用gzip命令来进行压缩
-j # 调用bzip2命令来进行压缩
-C # 解压到指定的路径,默认解压到当前所在的位置
-p # 保留原始属性和权限
--exclude='/path' # 排除指定的文件
1、gzip工具
gizp是应用最广泛的压缩工具,会生成.gz结尾的压缩文件。
来源: gzip包
特点:通常用于压缩单个文件。它的压缩率比zip高,但压缩速度较慢。
对应解压工具:gunzip
格式:
gzip [选项] 文件
选项
-c 将压缩数据输出到标准输出中,并保留源文件
-d decompress。解压缩,相当于gunzip
-k, keep,保留源文件
例如:
使用-c选项,为了不让压缩数据输出到屏幕上,而是重定向到压缩文件中,实现压缩文件时不删除源文件
gzip -c messages >messages.gz
-d:压缩目录文件
gzip -c -d messages.gz > messages
zcat:不解压文件的情况下看里面的内容
zcat messages.gz > messages
2、bzip2工具
使用bzip2压缩文件会生成.bz2结尾的压缩文件。
来源:bzip2包
解压工具:bunzip2
特点:压缩算法比gzip更为复杂,能够达到更高的压缩率,但压缩速度更慢。
格式: bzip2 [选项] 文件
常用选项:
-d # 解压缩
例如:
压缩:
bzip2 filename
解压缩:
bunbzip2 filename
3、xz工具
xz压缩文件会生成.xz结尾的压缩文件。
来源:xz软件包
解压工具:unxz
常用选项
-d # 解压缩
#用法和gzip差不多
特点:压缩率非常高,但压缩和解压缩速度比较慢,通常用于压缩大型文件或数据集合。
4、zip工具
zip可以实现默认对目录打包,多个文件合成一个文件压缩,生成.zip结尾的文件。但是可能会丢失属性信息,建议用tar代替。
格式: zip 压缩成的文件名 需要压缩的文件
解压工具:unzip
特点:但不适合压缩大量的数据
常用选项:
-r # 压缩目录文件的时候需要添加这个参数
-d # 解压到指定目录
例如:
#打包并压缩
zip -r /backup/sysconfig.zip /etc/sysconfig/
默认解压缩至当前目录
unzip /backup/sysconfig.zip
解压缩至指定目录,如果指定目录不存在,会在其父目录(必须事先存在)下自动生成
unzip /backup/sysconfig.zip -d /tmp/config
5、 总结:
- 大文件:例如几百KB或几MB,可以使用zip或gzip,因为它们的压缩速度比较快。
- 小文件:例如几百MB或几GB,可以使用bzip2或xz进行压缩。虽然它们的压缩速度较慢,但它们能够提供更好的压缩率,从而减少磁盘空间的使用。
- 一般文件:使用tar工具就行了
Original: https://www.cnblogs.com/heyongshen/p/16389064.html
Author: 背对背依靠
Title: Linux 文件的打包压缩
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/523351/
转载文章受原作者版权保护。转载请注明原作者出处!