Linux 压缩,归档 总结
前言
通常来说我们都是使用tar -参数 等方式进行压缩打包,或者解压缩,其实在linux里,压缩和归档是两种不同的程序负责的。下面来看下几种压缩的方式,和几种打包的方式。
压缩的几种方式
1) 后缀为.z的文件
compress压缩/uncompress解压缩
# 常用参数
# -d 解压缩 相当于 uncompress
# -v 显示详情
# -c 结果输出在屏幕上不删除源文件
1.压缩
[root@localhost ~]# ll -h
total 2.0M
-rw-r--r-- 1 root root 1.2M Sep 24 14:57 messages
[root@localhost ~]# compress messages
[root@localhost ~]# ll -h
total 144K
-rw-r--r-- 1 root root 143K Sep 24 14:57 messages.Z
[root@localhost ~]#
#这个命令使用过后,原始文件就会没有了。可以使用-c参数
2. -c参数
[root@localhost ~]# ll -h
total 1.2M
-rw-r--r-- 1 root root 1.2M Sep 24 14:57 messages
[root@localhost ~]# compress -c messages > messages.Z
[root@localhost ~]# ll -h
total 1.4M
-rw-r--r-- 1 root root 1.2M Sep 24 14:57 messages
-rw-r--r-- 1 root root 143K Sep 24 15:09 messages.Z
[root@localhost ~]#
# 这样可以 保留文件的同时,获得压缩文件
3.如果一个文件本身就是压缩文件,或者压缩格式的文件,compress 不压缩这个文件,所以有些文件不支持压缩。
[root@localhost ~]# cp /usr/share/backgrounds/morning.jpg .
[root@localhost ~]# ll -h
total 1.1M
-rw-r--r-- 1 root root 143K Sep 24 14:57 messages.Z
-rw-r--r-- 1 root root 958K Sep 24 15:04 morning.jpg
[root@localhost ~]# compress morning.jpg
[root@localhost ~]# ll -h
total 1.1M
-rw-r--r-- 1 root root 143K Sep 24 14:57 messages.Z
-rw-r--r-- 1 root root 958K Sep 24 15:04 morning.jpg
[root@localhost ~]#
zcat 查看.z 的文件内容,但不解压
[root@localhost ~]# zcat messages.Z | tail -n1
Sep 24 14:44:26 localhost nm-dispatcher: req:1 'dhcp4-change' [ens33]: start running ordered scripts...
[root@localhost ~]# ll
total 144
-rw-r--r-- 1 root root 145607 Sep 24 14:57 messages.Z
[root@localhost ~]#
2) 后缀为.gz的文件
gzip压缩/gunzip解压缩
zcat 查看.gz 的文件内容,但不解压
# 常用参数
# -d 解压缩 相当于 gunzip
# -c 结果输出在屏幕上不删除源文件
# -# #号的取值代表1-9, 9代表压缩比最大,压缩的文件更小
[root@localhost ~]# gzip messages -c > messages.gz
[root@localhost ~]# gzip -9 messages -c > messages2.gz
# 可使用 gzip -c 来压缩文件,并保留源文件
[root@localhost ~]# ll -h
total 1.5M
-rw-r--r-- 1 root root 1.2M Sep 24 14:57 messages
-rw-r--r-- 1 root root 81K Sep 24 15:21 messages2.gz
-rw-r--r-- 1 root root 83K Sep 24 15:17 messages.gz
#gzip 支持标准输入,下面这个例子是把/etc的目录 压缩成list文件
[root@localhost ~]# ls -lr /etc | gzip > /root/list.gz
[root@localhost ~]# zcat list.gz | tail -n1
-rw-r--r--. 1 root root 16 Aug 16 10:49 adjtime
[root@localhost ~]#
3) 后缀为.bz2的文件
bzip2压缩/bunzip2解压缩
bzcat 查看.bz2 的文件内容,但不解压
# 常用参数
# -d 解压缩 相当于 bunzip2
# -k 保留源文件
# -# #号的取值代表1-9, 9代表压缩比最大,压缩的文件更小
[root@localhost ~]# bzip2 -k messages
[root@localhost ~]# ll
total 1548
-rw-r--r-- 1 root root 2399 Sep 24 15:26 list.gz
-rw-r--r-- 1 root root 1229697 Sep 24 14:57 messages
-rw-r--r-- 1 root root 82147 Sep 24 15:21 messages2.gz
-rw-r--r-- 1 root root 25201 Sep 24 14:57 messages.bz2
-rw-r--r-- 1 root root 84290 Sep 24 15:17 messages.gz
-rw-r--r-- 1 root root 145607 Sep 24 15:09 messages.Z
[root@localhost ~]#
4) 后缀为.xz的文件
xz压缩/unxz解压缩
xzcat 查看.xz 的文件内容,但不解压
# 常用参数
# -d 解压缩 相当于 bunzip2
# -k 保留源文件
# -# #号的取值代表1-9, 9代表压缩比最大,压缩的文件更小,默认为6
[root@localhost ~]# xz -k messages
[root@localhost ~]# ll
total 1560
-rw-r--r-- 1 root root 2399 Sep 24 15:26 list.gz
-rw-r--r-- 1 root root 1229697 Sep 24 14:57 messages
-rw-r--r-- 1 root root 82147 Sep 24 15:21 messages2.gz
-rw-r--r-- 1 root root 25201 Sep 24 14:57 messages.bz2
-rw-r--r-- 1 root root 84290 Sep 24 15:17 messages.gz
-rw-r--r-- 1 root root 8588 Sep 24 14:57 messages.xz
-rw-r--r-- 1 root root 145607 Sep 24 15:09 messages.Z
[root@localhost ~]#
注意:
解压缩是看后缀来识别的,如果一个gz压缩的文件,文件名后缀为别的,那么是解压不了的。使用file 文件名,来看这是什么文件。
压缩不能压缩文件夹,需要配合zip,tar,cpio等归档打包工具来使用。
压缩成的文件越小,越消耗cpu资源,按照最后压缩成文件大小来排序的话,.z最大<.gz<.bz2<.xz最小。
打包压缩几种方式
1) zip方式打包
打包压缩
压缩后的大小和.gz压缩差不多。
[root@localhost data]#zip -r /root/root.zip /root/
# 把/root 文件夹,打包成root.zip
[root@localhost data]#zip /root/root.zip /root/message
# 把message文件进行压缩
[root@localhost data]#zip -r root.zip boot 123.txt
#把/root目录下面的boot文件夹和123.txt压缩成为root.zip
查看包内容
[root@localhost data]# unzip -v data.zip
Archive: data.zip
Length Method Size Cmpr Date Time CRC-32 Name
-------- ------ ------- ---- ---------- ----- -------- ----
0 Stored 0 0% 09-24-2019 16:26 00000000 123.txt
0 Stored 0 0% 09-24-2019 16:26 00000000 456/
-------- ------- --- -------
0 0 0% 2 files
[root@localhost data]#
解包压缩
#解压文件夹或文件,到当前目录
unzip root.zip
#把/root目录下面的root.zip解压到/data/目录里面
unzip root.zip -d /data
#解压文件
unzip -p file.zip > file
# unzip -p 提取文件到管道
[root@localhost ~]# file etc.zip
etc.zip: Zip archive data, at least v1.0 to extract # v1.0 代表是个文件夹
[root@localhost ~]# file boot2.zip
boot2.zip: Zip archive data, at least v2.0 to extract # v2.0 代表是个文件
[root@localhost ~]#
2) tar方式打包+压缩
仅打包
-c 代表创建这个tar文件
-p 代表打包文件权限,但是不会打包 文件acl
-v 过程
-f 指定档案文件名或设备名,这个选项通常是必选的。
tar -cvf /root/a.tar /root 创建一个打包文件,但是并未压缩,不过实际可能会小一点
tar tvf a.tar 预览包的内容,-tvf 和 tvf 效果一样
tar xvf a.tar 解压包到当前目录。
tar xvf a.tar -C /data 解压包到/data目录
tar rvf a.tar /boot/123.txt 把123.txt 追加进a.tar里
打包并且压缩
tar z 代表gz后缀
tar j 代表压缩bz2
tar J 代表压缩xz
eg:
tar zcvf /root/a.tar.gz /root 创建打包文件并且gz方式压缩
注意:
tar tvf 预览,可以不用带压缩包的前缀,自动识别
tar xvf 解压缩,可以不用带压缩包的前缀,自动识别
tar zcvf /root/a.tgz --exclude=/root/host1 /root 压缩的时候排除这个文件
tar zcvf myback.tgz -T /root/includefilelist -X /root/excludefilelist
-T 代表要打包的文件的路径,在includefilelist文件里编写路径
-X 代表要排除打包的文件的路径,在excludefilelist文件里编写排除路径
可以使用split 进行分割
split -b 10M a.tar.gz 按照10M进行分割
加上-d的话,后缀带数字,默认以字母
cat a* > a.tar.gz 进行合并
3) cpio方式打包
这个功能是打包归档,这里就不啰嗦了,感兴趣的可以去搜一搜。
cpio -tv < 123.cpio 预览123.cpio的文件
cpio -idv < 123.cpio 解压123.cpio到当前目录
find | cpio -ov > /root/123.cpio 把当前目录的内容打包进123.cpio里