当前位置: 移动技术网 > 科技>操作系统>Linux > 文件打包压缩

文件打包压缩

2020年05月04日  | 移动技术网科技  | 我要评论

河南省农村信用联社,智取生辰纲教案,隐婚七年

文件打包压缩

 

自我感觉,文件压缩打包这块很好学,别看压缩命名多,好几个不常用,常用的几个选项基本都通用。就会产生一个很好的结果,只要学一个命令的选项,剩下的几个命令只要记住名字就可以了。

本篇的重点在tar命令,大部分网上的文件都是经过打包压缩处理过的。

linux中常见的压缩文件的扩展名:*.gz  *.bz2  *.xz  *.tar  *.tar.gz  *.tar.bz2  *.tar.xz  *.tgz  *.z  *.tar.z

 

 

0.compress/uncompress
compress 压缩文件不会保留源文件,压缩后的文件后缀是.z
-d 解压缩,相当于uncompress,可以重定向以保留源文件
-c 输出压信息到屏幕,可以重定向以保留源文件
-v 显示详情
 
zcat命令
可以查看.z和.gz的压缩文件内容
此命令太过久远了,基本上已经很少见*.z的压缩文件了。而且使用gzip命令也可以解压*.z压缩文件。
 
 
1.gzip/gunzip
gzip压缩文件会删除源文件,生成.gz为后缀的压缩文件。
gzip [option]....file.....
-c 把压缩文件数据流输出到屏幕上,可以使用重定向保存到文件中,用于保留源文件。
-d 解压缩,相当于gunzip
-k 保留源文件,仅在centos8以后的版本中有效
-v 压缩后,可以显示出压缩比
-t 用于测试压缩包的完整性
-# 按照指定的压缩比压缩。默认为6,支持1-9
 
zcat、zmore、zless命令
在不解压.gz后缀的压缩文件的情况下查看压缩文件内容。zcat file.gz
zgrep命令
可以在解压的情况下直接搜索关键字
zgrep 关键字 /path/to/compress.gz

 示例:

 1  #演示在不解压的情况下搜索关键字
 2 [root@centos8 tmp]# zgrep http services.gz  
 3 #       http://www.iana.org/assignments/port-numbers
 4 http            80/tcp          www www-http    # worldwideweb http
 5 http            80/udp          www www-http    # hypertext transfer protocol
 6 http            80/sctp                         # hypertext transfer protocol
 7 https           443/tcp                         # http protocol over tls/ssl
 8 https           443/udp                         # http protocol over tls/ssl
 9 https           443/sctp                        # http protocol over tls/ssl
10 gss-http        488/tcp
11 gss-http        488/udp
12 
13 
14 #-c选项和管道符结合使用,主要用于保存源文件
15 [root@centos8 tmp]# gzip -c services > services.gz   
16 [root@centos8 tmp]# ll
17 total 824
18 -rwx------. 1 root root   1379 may  2 08:53 ks-script-drjaqc7p
19 -rw-r--r--  1 root root 692241 may  3 17:35 services
20 -rw-r--r--  1 root root 142549 may  3 18:02 services.gz

 

 

2.bzip2/bunzip2
bzip2压缩文件会删除源文件,生成.bz2为后缀的压缩文件。
bzip2 [option]....file.....
-c 把压缩文件数据流输出到屏幕上,可以使用重定向保存到文件中,用于保留源文件。
-k 压缩后保留源文件,同时生成压缩文件。
-d 解压缩,相当于bunzip2
-# 按照指定的压缩比压缩。默认为6,支持1-9
-q 静默 压缩
-v 压缩后,显示出压缩比
-r 把目录下的文件分别进行独立压缩
bzcat/bzmore/bzless命令
在不解压.bz2后缀的压缩文件的情况下查看压缩文件内容,和zcat用法相同。
bzcat file.bz2
bzgrep命令
在不解.bz2后缀的压缩文件的情况下搜索压缩包内容
bzgrep 关键字 /path/to/compress.bz2

示例:

 1 #保留源文件压缩,压缩后显示压缩比信息
 2 [root@centos8 tmp]# bzip2 -kv services      
 3   services:  5.334:1,  1.500 bits/byte, 81.25% saved, 692241 in, 129788 out.
 4 [root@centos8 tmp]# ll
 5 total 952
 6 -rwx------. 1 root root   1379 may  2 08:53 ks-script-drjaqc7p
 7 -rw-r--r--  1 root root 692241 may  3 18:15 services
 8 -rw-r--r--  1 root root 129788 may  3 18:15 services.bz2
 9 -rw-r--r--  1 root root 142549 may  3 18:02 services.gz
10 
11 [root@centos8 tmp]# bunzip2 services.bz2 

 

 
3.xz/unxz
xz压缩文件会删除源文件,生成.xz为后缀的压缩文件。
xz [option]....file.....
-c 把压缩文件数据流输出到屏幕上,可以使用重定向保存到文件中,用于保留源文件。
-k 压缩后保留源文件,同时生成压缩文件。
-d 解压缩,相当于unxz
-# 按照指定的压缩比压缩。默认为6,支持1-9
-q 静默 压缩
-v 压缩后,显示压缩比等信息
 
xzcat/xzmore/xzless命令
在不解压.xz后缀的压缩文件的情况下查看压缩文件内容,和zcat用法相同。xzcat file.xz
xzgrep命令
在不解压的情况下搜索压缩包内容
xzgrep 关键字 /path/to/compress.xz

示例:

#保留源文件压缩,并且输出压缩比等信息
[root@centos8 tmp]# xz -kv services
services (1/1)
  100 %       103.4 kib / 676.0 kib = 0.153                                    
[root@centos8 tmp]# ll
total 1056
-rw-r--r--  1 root root 692241 may  3 18:15 services
-rw-r--r--  1 root root 129788 may  3 18:15 services.bz2
-rw-r--r--  1 root root 142549 may  3 18:02 services.gz
-rw-r--r--  1 root root 105872 may  3 18:15 services.xz


[root@centos8 tmp]# rm services   #删除源文件
rm: remove regular file 'services'? y
[root@centos8 tmp]# xz -dk services.xz     #保留源文件解压缩
[root@centos8 tmp]# ll
total 1056
-rw-r--r--  1 root root 692241 may  3 18:15 services
-rw-r--r--  1 root root 129788 may  3 18:15 services.bz2
-rw-r--r--  1 root root 142549 may  3 18:02 services.gz
-rw-r--r--  1 root root 105872 may  3 18:15 services.xz

#使用xzgrep搜索压缩包
[root@centos8 tmp]# xzgrep http services.xz 
#       http://www.iana.org/assignments/port-numbers
http            80/tcp          www www-http    # worldwideweb http
http            80/udp          www www-http    # hypertext transfer protocol
http            80/sctp                         # hypertext transfer protocol
https           443/tcp                         # http protocol over tls/ssl
https           443/udp                         # http protocol over tls/ssl
https           443/sctp                        # http protocol over tls/ssl

#使用-l选项显示长格式信息
[root@centos8 tmp]# xz -l services.xz 
strms  blocks   compressed uncompressed  ratio  check   filename
    1       1    103.4 kib    676.0 kib  0.153  crc64   services.xz

 

 
4.zip/unzip
zip一个及压缩打包于一身的压缩命令,而且还是各个操作系统通用的压缩命令。支持windows,unix,liunx。
zip [option]... 压缩后的文件名.zip 要进行压缩的源文件
备注:如果要用zip进行压缩目录,需要指明目录下的文件名,如果不指明则会指压缩目录本身。例子:要使用 zip pam.d.zip /etc/pam.d/*
 
5.tar
用于打包的工具。能够把多个文件打包成一个文件。tar打包或者展开不会删除源文件
1.创建文件 tar -c -f -[jjz] /path/to/somefile.tar file....
备注:-[jjz]通常只在创建压缩文件的时候使用,解压缩tar会自动调用对应的压缩工具实现解压
2.展开文件 tar -x -f -[jjz] /path/to/somefile.tar -c /path/to/dir
备注:在展开的时候其实是可以不用加-j/-z/-j选项,tar会自动调用对应的解压工具解压。
3.不展开显示打包文件的列表 tar -t -f -[jjz] /path/to/somefile.tar
备注:在展开的时候其实是可以不用加-j -z -j 选项的,tar会自动调用对应的解压工具解压。
 
-c 创建打包文件
-x 展开打包文件
-t 列出打包文件的文件列表,而不展开
 
-f 指定要创建或展开的打包文件路径
 
-j 调用bzip2压缩工具
-j 调用xz压缩工具
-z 调用gzip压缩工具
-z 调用compress压缩工具进行压缩
不允许同时出现,-j -j -z -z
-v 显示过程
-p 保留源文件的原始属性,只有管理员有权限
-p 保留绝对路径,就是允许贝恩数据中含有根目录。默认情况下是自动取消根目录
--xattrs 保留源文件的扩展属性
 
-c 把展开的文件保存到指定文件下,默认为当前目录。
--exclude=/path/to/{somedir|somefile} 排除某些目录或文件不压缩

示例:

 1 #使用-z压缩
 2 [root@centos8 tmp]# tar -zcvf etc.gz /etc/*
 3 tar: removing leading `/' from member names
 4 /etc/adjtime
 5 tar: removing leading `/' from hard link targets
 6 /etc/aliases
 7 /etc/alternatives/
 8 ...
 9 #使用-j压缩,并保留源文件属性
10 [root@centos8 tmp]# tar -jcvpf etc.bz2 /etc/*
11 tar: removing leading `/' from member names
12 /etc/adjtime
13 tar: removing leading `/' from hard link targets
14 /etc/aliases
15 /etc/alternatives/
16 /etc/alternatives/libnssckbi.so.x86_64
17 ...
18 #使用-j压缩,并保留源文件属性和facl属性,这里没有使用-v选项
19 [root@centos8 tmp]# tar --xattrs -jcpf etc.xz /etc/*
20 tar: removing leading `/' from member names
21 tar: removing leading `/' from hard link targets
22 [root@centos8 tmp]# ll
23 total 11508
24 -rw-r--r-- 1 root root 3654689 may  3 19:05 etc.bz2
25 -rw-r--r-- 1 root root 5053462 may  3 19:03 etc.gz
26 -rw-r--r-- 1 root root 3069780 may  3 19:07 etc.xz
27 
28 #使用-c选项指定解压目录
29 [root@centos8 tmp]# mkdir test
30 [root@centos8 tmp]# tar xf etc.gz -c /tmp/test

 

 

6.cpio
老牌打包解包工具
-o output模式,打包,将标准输入传入的文件名打包发送到标准输出
-i input模式,解包
-t 预览,查看标准输入传入的打包文件中的包含的文件列表
cpio -tv < name.cpio
-a 向已存在的归档文件中追加文件
-v 显示详细过程
-d 如果有文件夹自动创建,常与-i一起使用,用于解压时自动创建目录
cpio -idv < etc.cpio
-f filename 使用指定的文件名代替标准输入或输出
-i filename 对指定的归档文件名解压
-o filename 输出到指定归档文件名

 

 1 #示例演示cpio不会区分绝对路径和相对路径
 2 [root@centos8 /]# find ./etc | cpio -h newc -o > /tmp/etc.cpio
 3 40756 blocks
 4 [root@centos8 /]# find /etc | cpio -h newc -o > /tmp/etc2.cpio
 5 40757 blocks
 6 [root@centos8 /]# cd /tmp
 7 [root@centos8 tmp]# ls
 8 boot.cpio  etc2.cpio  etc.cpio
 9 [root@centos8 tmp]# cpio -tv < etc.cpio 
10 drwxr-xr-x  78 root     root            0 may  3 18:14 etc
11 lrwxrwxrwx   1 root     root           19 may  2 08:51 etc/mtab -> ../proc/self/mounts
12 -rw-r--r--   1 root     root          615 may  2 08:50 etc/fstab
13 -rw-------   1 root     root            0 may  2 08:50 etc/crypttab
14 -rw-r--r--   1 root     root           69 may  3 17:31 etc/resolv.conf
15 。。。
16 [root@centos8 tmp]# cpio -tv < etc2.cpio 
17 drwxr-xr-x  78 root     root            0 may  3 18:14 /etc
18 lrwxrwxrwx   1 root     root           19 may  2 08:51 /etc/mtab -> ../proc/self/mounts
19 -rw-r--r--   1 root     root          615 may  2 08:50 /etc/fstab
20 -rw-------   1 root     root            0 may  2 08:50 /etc/crypttab
21 -rw-r--r--   1 root     root           69 may  3 17:31 /etc/resolv.conf
22 drwxr-xr-x   8 root     root            0 may  2 08:51 /etc/dnf
23 #对比发现,使用find /etc生成cpio文件中竟然会保留根目录,如果解压的时候就会覆盖对应根目录下的文件,很危险
24 #而使用find ./etc生成的cpio文件中没有保留根目录。
25 
26 #解压etc.cpio
27 [root@centos8 tmp]# ls
28 boot.cpio  etc2.cpio  etc.cpio
29 [root@centos8 tmp]# mkdir test
30 [root@centos8 tmp]# cd test
31 [root@centos8 test]# cpio -id <../etc.cpio 
32 40756 blocks
33 [root@centos8 test]# ls
34 etc

 

 

关于时间成本:
通过下面小示例可以看到,使用gzip、bzip2、xz压缩同一个文件,耗费的时间长短。xz消耗的时间最长,gzip消耗的时间最短。
 1 [root@centos8 tmp]# rm -fr *
 2 [root@centos8 tmp]# ls
 3 [root@centos8 tmp]# cp /etc/services .
 4 [root@centos8 tmp]# ls
 5 services
 6 [root@centos8 tmp]# time gzip -k services ;time bzip2 -k services ;time xz -k services 
 7 
 8 real    0m0.016s  #使用gzip压缩消耗的时间0.016s
 9 user    0m0.013s
10 sys    0m0.003s
11 
12 real    0m0.036s  #使用bzip2压缩消耗的时间0.036s
13 user    0m0.025s
14 sys    0m0.010s
15 
16 real    0m0.184s  #使用xz压缩消耗的时间0.184s
17 user    0m0.107s
18 sys    0m0.077s
19 [root@centos8 tmp]# ll
20 total 1052
21 -rw-r--r-- 1 root root 692241 may  3 18:36 services
22 -rw-r--r-- 1 root root 129788 may  3 18:36 services.bz2
23 -rw-r--r-- 1 root root 142549 may  3 18:36 services.gz
24 -rw-r--r-- 1 root root 105872 may  3 18:36 services.xz

 

 

总结一下:

linux中常见的压缩文件的扩展名:*.gz  *.bz2  *.xz  *.tar  *.tar.gz  *.tar.bz2  *.tar.xz  *.tgz  *.z  *.tar.z

常见的压缩命令有:gzip bzip2 xz。其中压缩比率最好的是xz,但是消耗的时间也是最高的。需要综合考虑时间成本和cpu性能。

  几个选项基本上都通用:-d 解压 -v 显示详细过程 -c 输出内容到屏幕 -k保留源文件

tar可以打包文件和目录,打包时可以调用gzip bzip2 xz进行压缩

  压缩:  tar -[zjj]cv -f filename.后缀  要被压缩的文件或目录

  查询:  tar -[zjj]tv -f filename.后缀

  解压:  tar -[zjj]xv -f filename.后缀 -c /path/to/somedir

cpio命令在压缩的时候需要配合find命令一起使用。注意使用相对路径

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网