前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >学习笔记0328----压缩文件

学习笔记0328----压缩文件

作者头像
嘻哈记
发布2022-01-11 10:35:21
1.7K0
发布2022-01-11 10:35:21
举报
文章被收录于专栏:运维学习交流运维学习交流

压缩文件

预习内容

6.1 压缩打包介绍 6.2 gzip压缩工具 6.3 bzip2压缩工具 6.4 xz压缩工具 6.5 zip压缩工具 6.6 tar打包 6.7 打包并压缩 看下这个帖子: http://ask.apelearn.com/question/5435

1. linux上的压缩文件后缀名

  • windows上的压缩文件:
    • .zip .7z .rar
  • linux 上的压缩文件:
    • .zip .gz .bz2 .xz .tar.gz .tar.bz2 .tar.xz

2. gzip(gunzip)压缩工具

2.1 gzip压缩文件,无法压缩目录

2.1.1【gzip 文件名】 压缩后源文件消失,文件名后多一个.gx
代码语言:javascript
复制
[root@linux-01 ~]# ll
总用量 444
-rw-r--r--. 1 root root 425886 3月  26 22:26 1.txt
-rw-r--r--. 1 root root   3147 3月  26 22:31 2.txt
-rw-------. 1 root root   1420 3月  14 01:02 anaconda-ks.cfg
-rw-r--r--. 1 root root  17301 3月  26 22:20 bb.log
[root@linux-01 ~]# gzip 1.txt  2.txt  bb.log 
[root@linux-01 ~]# ll
总用量 124
-rw-r--r--. 1 root root 107247 3月  26 22:26 1.txt.gz
-rw-r--r--. 1 root root    830 3月  26 22:31 2.txt.gz
-rw-------. 1 root root   1420 3月  14 01:02 anaconda-ks.cfg
-rw-r--r--. 1 root root   6116 3月  26 22:20 bb.log.gz
[root@linux-01 ~]# 
2.1.2 【gzip -c 1.txt > 1.txt.gz】压缩文件后源文件不丢失
代码语言:javascript
复制
[root@linux-01 ~]# gzip -c 1.txt  > 1.txt.gz
[root@linux-01 ~]# ll
总用量 552
-rw-r--r--. 1 root root 425886 3月  26 22:26 1.txt
-rw-r--r--. 1 root root 107247 3月  26 22:51 1.txt.gz
-rw-r--r--. 1 root root   3147 3月  26 22:31 2.txt
-rw-------. 1 root root   1420 3月  14 01:02 anaconda-ks.cfg
-rw-r--r--. 1 root root  17301 3月  26 22:20 bb.log
2.1.3 【gzip -1 1.txt】gzip的压缩级别

  • gzip的压缩级别有9个,是1-9 ,默认压缩级别是6.
代码语言:javascript
复制
[root@linux-01 ~]# gzip -1 -c 1.txt > 1.txt.gz 
[root@linux-01 ~]# file 1.txt.gz   # 用file可以查看文件信息
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Tue Mar 26 22:26:08 2019, max speed
[root@linux-01 ~]# gzip -9 -c 1.txt > 1.2.txt.gz 
[root@linux-01 ~]# file 1.2.txt.gz 
1.2.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Tue Mar 26 22:26:08 2019, max compression

2.2 gzip解压文件

2.2.1【gzip -d 文件名】解压文件,源文件消失
代码语言:javascript
复制
[root@linux-01 ~]# ll
总用量 124
-rw-r--r--. 1 root root 107247 3月  26 22:26 1.txt.gz
-rw-r--r--. 1 root root    830 3月  26 22:31 2.txt.gz
-rw-------. 1 root root   1420 3月  14 01:02 anaconda-ks.cfg
-rw-r--r--. 1 root root   6116 3月  26 22:20 bb.log.gz
[root@linux-01 ~]# gzip -d 1.txt.gz  2.txt.gz  bb.log.gz 
[root@linux-01 ~]# ll
总用量 444
-rw-r--r--. 1 root root 425886 3月  26 22:26 1.txt
-rw-r--r--. 1 root root   3147 3月  26 22:31 2.txt
-rw-------. 1 root root   1420 3月  14 01:02 anaconda-ks.cfg
-rw-r--r--. 1 root root  17301 3月  26 22:20 bb.log
[root@linux-01 ~]# 
2.2.2 【gzip -d -c 3.txt.gz > 3.txt】解压文件,源文件不消失
代码语言:javascript
复制
[root@linux-01 ~]# gzip -d -c 3.txt.gz 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
xihaji:x:1000:1000::/home/xihaji:/bin/bash
xihaji2:x:1001:1001::/home/xihaji2:/bin/bash
readonly:x:1002:1002::/home/readonly:/bin/bash
[root@linux-01 ~]# gzip -d -c 3.txt.gz  > 3.txt
[root@linux-01 ~]# ll
总用量 560
-rw-r--r--. 1 root root 425886 3月  26 22:26 1.txt
-rw-r--r--. 1 root root 107247 3月  26 22:51 1.txt.gz
-rw-r--r--. 1 root root   3147 3月  26 22:31 2.txt
-rw-r--r--. 1 root root    933 3月  26 23:18 3.txt
-rw-r--r--. 1 root root    413 3月  26 22:54 3.txt.gz
-rw-------. 1 root root   1420 3月  14 01:02 anaconda-ks.cfg
-rw-r--r--. 1 root root  17301 3月  26 22:20 bb.log

2.3 【zcat 3.txt.gz 】查看gzip压缩文件中的内容

代码语言:javascript
复制
[root@linux-01 ~]# cat /etc/passwd > 3.txt
[root@linux-01 ~]# gzip -c 3.txt > 3.txt.gz 
[root@linux-01 ~]# zcat 3.txt.gz 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
xihaji:x:1000:1000::/home/xihaji:/bin/bash
xihaji2:x:1001:1001::/home/xihaji2:/bin/bash
readonly:x:1002:1002::/home/readonly:/bin/bash

3. bzip2(bunzip2)压缩工具

3.1 bzip2 命令安装

bzip2 系统最小化安装没有安装,需要安装下。

代码语言:javascript
复制
[root@linux-01 ~]# yum install -y bzip2-1.0.6-13.el7.x86_64
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ap.stykers.moe
 * extras: mirrors.zju.edu.cn
 * updates: mirrors.njupt.edu.cn
base                                                                                                          | 3.6 kB  00:00:00     
extras                                                                                                        | 3.4 kB  00:00:00     
updates                                                                                                       | 3.4 kB  00:00:00     
updates/7/x86_64/primary_db                                                                                   | 3.4 MB  00:00:01     
正在解决依赖关系
--> 正在检查事务
---> 软件包 bzip2.x86_64.0.1.0.6-13.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

=====================================================================================================================================
 Package                      架构                          版本                                   源                           大小
=====================================================================================================================================
正在安装:
 bzip2                        x86_64                        1.0.6-13.el7                           base                         52 k

事务概要
=====================================================================================================================================
安装  1 软件包

总下载量:52 k
安装大小:82 k
Downloading packages:
bzip2-1.0.6-13.el7.x86_64.rpm                                                                                 |  52 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : bzip2-1.0.6-13.el7.x86_64                                                                                        1/1 
  验证中      : bzip2-1.0.6-13.el7.x86_64                                                                                        1/1 

已安装:
  bzip2.x86_64 0:1.0.6-13.el7                                                                                                        

完毕!

3.2 bzip2 压缩文件,无法压缩目录

3.2.1 【bzip2 文件名】压缩文件
代码语言:javascript
复制
[root@linux-01 ~]# bzip2 3.txt 
[root@linux-01 ~]# ll
总用量 448
-rw-r--r--. 1 root root 425886 3月  26 22:26 1.txt
-rw-r--r--. 1 root root   3147 3月  26 22:31 2.txt
-rw-r--r--. 1 root root    453 3月  26 23:18 3.txt.bz2
-rw-------. 1 root root   1420 3月  14 01:02 anaconda-ks.cfg
-rw-r--r--. 1 root root  17301 3月  26 22:20 bb.log
3.2.2 【bzip2 -c 3.txt >3.txt.bz2】压缩文件,源文件保留
代码语言:javascript
复制
[root@linux-01 ~]# bzip2 -c 3.txt > 3.txt.bz2
[root@linux-01 ~]# ll
总用量 452
-rw-r--r--. 1 root root 425886 3月  26 22:26 1.txt
-rw-r--r--. 1 root root   3147 3月  26 22:31 2.txt
-rw-r--r--. 1 root root    933 3月  26 23:18 3.txt
-rw-r--r--. 1 root root    453 3月  27 00:36 3.txt.bz2
-rw-------. 1 root root   1420 3月  14 01:02 anaconda-ks.cfg
-rw-r--r--. 1 root root  17301 3月  26 22:20 bb.log
[root@linux-01 ~]# 
3.2.3 【bzip2 -1 文件名】压缩级别

bzip2和gzip的压缩级别类似,也是有9个,1-9,默认压缩级别是9.

代码语言:javascript
复制
[root@linux-01 ~]# bzip2 -1 3.txt 
[root@linux-01 ~]# file 3.txt.bz2 
3.txt.bz2: bzip2 compressed data, block size = 100k
[root@linux-01 ~]# bzip2 -d 3.txt.bz2   # 解压文件
[root@linux-01 ~]# bzip2 -9 3.txt 
[root@linux-01 ~]# file 3.txt.bz2 
3.txt.bz2: bzip2 compressed data, block size = 900k

3.3 bzip2 解压文件

3.3.1 【bzip2 -c 文件名】解压文件
代码语言:javascript
复制
[root@linux-01 ~]# bzip2 -d 3.txt.bz2 
[root@linux-01 ~]# ll
总用量 448
-rw-r--r--. 1 root root 425886 3月  26 22:26 1.txt
-rw-r--r--. 1 root root   3147 3月  26 22:31 2.txt
-rw-r--r--. 1 root root    933 3月  26 23:18 3.txt
-rw-------. 1 root root   1420 3月  14 01:02 anaconda-ks.cfg
-rw-r--r--. 1 root root  17301 3月  26 22:20 bb.log
[root@linux-01 ~]# 
3.3.2 【bzip2 -c -d 3.txt.bz2 > 3.txt】解压文件,源文件保留
代码语言:javascript
复制
[root@linux-01 ~]# bzip2 -c -d 3.txt.bz2 > 3.txt
[root@linux-01 ~]# ll
总用量 452
-rw-r--r--. 1 root root 425886 3月  26 22:26 1.txt
-rw-r--r--. 1 root root   3147 3月  26 22:31 2.txt
-rw-r--r--. 1 root root    933 3月  27 00:56 3.txt
-rw-r--r--. 1 root root    453 3月  26 23:18 3.txt.bz2
-rw-------. 1 root root   1420 3月  14 01:02 anaconda-ks.cfg
-rw-r--r--. 1 root root  17301 3月  26 22:20 bb.log

3.4 【bzcat 3.txt.bz2 】 查看bzip2 压缩文件中的内容

代码语言:javascript
复制
[root@linux-01 ~]# bzcat  3.txt.bz2 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
xihaji:x:1000:1000::/home/xihaji:/bin/bash
xihaji2:x:1001:1001::/home/xihaji2:/bin/bash
readonly:x:1002:1002::/home/readonly:/bin/bash

4. xz 解压工具

4.1 xz压缩文件,无法压缩目录

4.1.1 【xz 文件名】压缩文件,源文件消失
代码语言:javascript
复制
[root@linux-01 ~]# xz 3.txt 
[root@linux-01 ~]# ll
总用量 448
-rw-r--r--. 1 root root 425886 3月  26 22:26 1.txt
-rw-r--r--. 1 root root   3147 3月  26 22:31 2.txt
-rw-r--r--. 1 root root    472 3月  27 00:56 3.txt.xz
-rw-------. 1 root root   1420 3月  14 01:02 anaconda-ks.cfg
-rw-r--r--. 1 root root  17301 3月  26 22:20 bb.log
[root@linux-01 ~]# 
4.1.2 【xz -c 2.txt > 2.txt.xz】压缩文件,保留源文件
代码语言:javascript
复制
[root@linux-01 ~]# xz -c 2.txt  > 2.txt.xz
[root@linux-01 ~]# ll
总用量 452
drwxr-xr-x. 2 root root      6 3月  27 02:47 111
-rw-r--r--. 1 root root 425886 3月  26 22:26 1.txt
-rw-r--r--. 1 root root   3147 3月  26 22:31 2.txt
-rw-r--r--. 1 root root    904 3月  27 02:50 2.txt.xz
-rw-r--r--. 1 root root    472 3月  27 00:56 3.txt.xz
-rw-------. 1 root root   1420 3月  14 01:02 anaconda-ks.cfg
-rw-r--r--. 1 root root  17301 3月  26 22:20 bb.log
[root@linux-01 ~]# 
4.1.3 【xz -1 1.txt】压缩级别

xz与bzip2和gzip类似, -0 到 -9调节压缩率。如果不设置,默认压缩等级是6.

代码语言:javascript
复制
[root@linux-01 ~]# xz -1 1.txt 
[root@linux-01 ~]# ll
总用量 88
drwxr-xr-x. 2 root root     6 3月  27 02:47 111
-rw-r--r--. 1 root root 52372 3月  26 22:26 1.txt.xz
-rw-r--r--. 1 root root  3147 3月  26 22:31 2.txt
-rw-r--r--. 1 root root   904 3月  27 02:50 2.txt.xz
-rw-r--r--. 1 root root   472 3月  27 00:56 3.txt.xz
-rw-------. 1 root root  1420 3月  14 01:02 anaconda-ks.cfg
-rw-r--r--. 1 root root 17301 3月  26 22:20 bb.log

4.2 xz 解压文件

4.2.1 【xz -d 文件名】解压文件,源文件消失
代码语言:javascript
复制
[root@linux-01 ~]# xz -d 1.txt.xz 
[root@linux-01 ~]# ll
总用量 452
drwxr-xr-x. 2 root root      6 3月  27 02:47 111
-rw-r--r--. 1 root root 425886 3月  26 22:26 1.txt
-rw-r--r--. 1 root root   3147 3月  26 22:31 2.txt
-rw-r--r--. 1 root root    904 3月  27 02:50 2.txt.xz
-rw-r--r--. 1 root root    472 3月  27 00:56 3.txt.xz
-rw-------. 1 root root   1420 3月  14 01:02 anaconda-ks.cfg
-rw-r--r--. 1 root root  17301 3月  26 22:20 bb.log
4.2.2 【xz -d -c 3.txt.xz >3.txt】解压文件,源文件保留
代码语言:javascript
复制
[root@linux-01 ~]# xz -d -c 3.txt.xz >3.txt
[root@linux-01 ~]# ll
总用量 456
drwxr-xr-x. 2 root root      6 3月  27 02:47 111
-rw-r--r--. 1 root root 425886 3月  26 22:26 1.txt
-rw-r--r--. 1 root root   3147 3月  26 22:31 2.txt
-rw-r--r--. 1 root root    904 3月  27 02:50 2.txt.xz
-rw-r--r--. 1 root root    933 3月  27 02:58 3.txt
-rw-r--r--. 1 root root    472 3月  27 00:56 3.txt.xz
-rw-------. 1 root root   1420 3月  14 01:02 anaconda-ks.cfg
-rw-r--r--. 1 root root  17301 3月  26 22:20 bb.log

4.3 【xzcat 3.txt.xz 】查看 xz 压缩文件中的内容

代码语言:javascript
复制
[root@linux-01 ~]# xzcat 3.txt.xz 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
xihaji:x:1000:1000::/home/xihaji:/bin/bash
xihaji2:x:1001:1001::/home/xihaji2:/bin/bash
readonly:x:1002:1002::/home/readonly:/bin/bash
[root@linux-01 ~]# 

5. zip文件,可以压缩文件夹

5.1安装zip命令

代码语言:javascript
复制
[root@linux-01 ~]# yum install -y zip
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ap.stykers.moe
 * extras: mirrors.zju.edu.cn
 * updates: mirrors.njupt.edu.cn
正在解决依赖关系
--> 正在检查事务
---> 软件包 zip.x86_64.0.3.0-11.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

=====================================================================================================================================
 Package                     架构                           版本                                  源                            大小
=====================================================================================================================================
正在安装:
 zip                         x86_64                         3.0-11.el7                            base                         260 k

事务概要
=====================================================================================================================================
安装  1 软件包

总下载量:260 k
安装大小:796 k
Downloading packages:
zip-3.0-11.el7.x86_64.rpm                                                                                     | 260 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : zip-3.0-11.el7.x86_64                                                                                            1/1 
  验证中      : zip-3.0-11.el7.x86_64                                                                                            1/1 

已安装:
  zip.x86_64 0:3.0-11.el7                                                                                                            

完毕!

5.2 zip 压缩文件

5.2.1【zip 压缩后的文件名 源文件】压缩文件
代码语言:javascript
复制
[root@linux-01 ~]# zip 2.txt.zip 2.txt
  adding: 2.txt (deflated 74%)
[root@linux-01 ~]# ll
总用量 568
drwxr-xr-x. 2 root root      6 3月  27 02:47 111
-rw-r--r--. 1 root root 425886 3月  26 22:26 1.txt
-rw-r--r--. 1 root root 108925 3月  27 03:12 1.txt.zip
-rw-r--r--. 1 root root   3147 3月  26 22:31 2.txt
-rw-r--r--. 1 root root    904 3月  27 02:50 2.txt.xz
-rw-r--r--. 1 root root    966 3月  27 03:15 2.txt.zip
-rw-r--r--. 1 root root    933 3月  27 02:58 3.txt
-rw-r--r--. 1 root root    472 3月  27 00:56 3.txt.xz
-rw-------. 1 root root   1420 3月  14 01:02 anaconda-ks.cfg
-rw-r--r--. 1 root root  17301 3月  26 22:20 bb.log
[root@linux-01 ~]# 
5.2.2 【zip 压缩后的文件名 源文件1 目录1】压缩目录
代码语言:javascript
复制
[root@linux-01 ~]# zip 1.txt.zip 1.txt  2.txt  3.txt  111/
  adding: 1.txt (deflated 75%)
  adding: 2.txt (deflated 74%)
  adding: 3.txt (deflated 58%)
  adding: 111/ (stored 0%)
  [root@linux-01 ~]# ll
总用量 564
drwxr-xr-x. 2 root root      6 3月  27 02:47 111
-rw-r--r--. 1 root root 425886 3月  26 22:26 1.txt
-rw-r--r--. 1 root root 108925 3月  27 03:12 1.txt.zip
-rw-r--r--. 1 root root   3147 3月  26 22:31 2.txt
-rw-r--r--. 1 root root    904 3月  27 02:50 2.txt.xz
-rw-r--r--. 1 root root    933 3月  27 02:58 3.txt
-rw-r--r--. 1 root root    472 3月  27 00:56 3.txt.xz
-rw-------. 1 root root   1420 3月  14 01:02 anaconda-ks.cfg
-rw-r--r--. 1 root root  17301 3月  26 22:20 bb.log
[root@linux-01 ~]# 

5.3 unzip 解压文件

5.3.1 安装unzip命令
代码语言:javascript
复制
[root@linux-01 ~]# yum install -y unzip
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ap.stykers.moe
 * extras: mirrors.zju.edu.cn
 * updates: mirrors.njupt.edu.cn
正在解决依赖关系
--> 正在检查事务
---> 软件包 unzip.x86_64.0.6.0-19.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

=====================================================================================================================================
 Package                       架构                           版本                                源                            大小
=====================================================================================================================================
正在安装:
 unzip                         x86_64                         6.0-19.el7                          base                         170 k

事务概要
=====================================================================================================================================
安装  1 软件包

总下载量:170 k
安装大小:365 k
Downloading packages:
unzip-6.0-19.el7.x86_64.rpm                                                                                   | 170 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : unzip-6.0-19.el7.x86_64                                                                                          1/1 
  验证中      : unzip-6.0-19.el7.x86_64                                                                                          1/1 

已安装:
  unzip.x86_64 0:6.0-19.el7                                                                                                          

完毕!
[root@linux-01 ~]# 
5.3.2 【unzip 文件名】解压缩文件
代码语言:javascript
复制
[root@linux-01 ~]# unzip 1.txt.zip 
Archive:  1.txt.zip
replace 1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
  inflating: 1.txt                   
replace 2.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
  inflating: 2.txt                   
replace 3.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
  inflating: 3.txt
5.3.3 【unzip 文件名 -d 目标路径】解压缩文件到指定目录
代码语言:javascript
复制
[root@linux-01 ~]# unzip  1.txt.zip -d  /root/test/
Archive:  1.txt.zip
  inflating: /root/test/1.txt        
  inflating: /root/test/2.txt        
  inflating: /root/test/3.txt        
   creating: /root/test/111/
[root@linux-01 ~]# ll test/
总用量 424
drwxr-xr-x. 2 root root      6 3月  27 02:47 111
-rw-r--r--. 1 root root 425886 3月  26 22:26 1.txt
-rw-r--r--. 1 root root   3147 3月  26 22:31 2.txt
-rw-r--r--. 1 root root    933 3月  27 02:58 3.txt
[root@linux-01 ~]# 

5.4 【unzip -l 1.txt.zip 】查看压缩文件的文件列表

代码语言:javascript
复制
[root@linux-01 ~]# unzip -l 1.txt.zip 
Archive:  1.txt.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
   425886  03-26-2019 22:26   1.txt
     3147  03-26-2019 22:31   2.txt
      933  03-27-2019 02:58   3.txt
        0  03-27-2019 02:47   111/
---------                     -------
   429966                     4 files
[root@linux-01 ~]# 

6. tar打包文件

6.1 tar命令参数

代码语言:javascript
复制
## 基本格式:tar [Options] file_archive  //注意tar的第一参数必须为命令选项,即不能直接接待处理文件
- 常用命令参数:
-c   #创建一个新的打包文件(archive)
-x # 对打包文件(archive)进行解压操作
-t # 查看打包文件(archive)的内容,主要是构成打包文件(archive)的文件名
-z # 使用gzip进行压缩/解压,一般使用.tar.gz后缀
-j # 使用bzip2进行压缩/解压,一般使用.tar.bz2后缀
-f  # 指定tar指令使用的文件,若没有压缩操作,则以.tar作为后缀
-v    # 详细显示正在处理的文件名
-C Dir  # 将解压文件放置在 -C 指定的目录下
-p(小写) # 保留文件的权限和属性,在备份文件时较有用
-P(大写) # 保留原文件的绝对路径,即不会拿掉文件路径开始的根目录,则在还原时会覆盖对应路径上的内容
--exclude=file  # 排除不进行打包的文件

6.2 【tar -cvf 111.tar 111】打包一个文件夹

代码语言:javascript
复制
[root@linux-01 ~]# tar -cvf 111.tar  111
111/
111/1.txt
111/1.txt.zip
111/2.txt.xz
111/3.txt.xz
111/3.txt
111/2.txt
111/test/
[root@linux-01 ~]# ll
总用量 564
drwxr-xr-x. 3 root root    106 3月  28 03:08 111
-rw-r--r--. 1 root root 552960 3月  28 03:11 111.tar
-rw-------. 1 root root   1420 3月  14 01:02 anaconda-ks.cfg
-rw-r--r--. 1 root root  17301 3月  26 22:20 bb.log
drwxr-xr-x. 3 root root     56 3月  27 03:27 test
[root@linux-01 ~]# 
代码语言:javascript
复制
## 打包文件排除某些文件 ##
[root@linux-01 ~]# tar -cvf 111.tar  111/ --exclude 1.txt --exclude 2.txt
111/
111/test/
111/1.txt.zip
111/2.txt.xz
111/3.txt.xz
111/3.txt
[root@linux-01 ~]# 

6.3 【tar -xvf 111.tar 】解包文件

代码语言:javascript
复制
[root@linux-01 ~]# tar -xvf 111.tar  
111/
111/1.txt
111/1.txt.zip
111/2.txt.xz
111/3.txt.xz
111/3.txt
111/2.txt
111/test/

6.4 【tar -tf 111.tar 】查看打包文件的文件列表

代码语言:javascript
复制
[root@linux-01 ~]# tar -tf 111.tar 
111/
111/1.txt
111/1.txt.zip
111/2.txt.xz
111/3.txt.xz
111/3.txt
111/2.txt
111/test/

7.tar打包并压缩

7.1 打包压缩

  • 1.tar打包压缩gz格式(加-z选项)
代码语言:javascript
复制
[root@linux-01 ~]# tar -czvf 111.tar.gz  111/
111/
111/test/
111/1.txt
111/1.txt.zip
111/2.txt.xz
111/3.txt.xz
111/3.txt
111/2.txt
[root@linux-01 ~]# ll
总用量 360
drwxr-xr-x. 3 root root    106 3月  28 03:08 111
-rw-r--r--. 1 root root 122880 3月  28 03:20 111.tar
-rw-r--r--. 1 root root 221138 3月  28 03:23 111.tar.gz
-rw-------. 1 root root   1420 3月  14 01:02 anaconda-ks.cfg
-rw-r--r--. 1 root root  17301 3月  26 22:20 bb.log
drwxr-xr-x. 3 root root     56 3月  27 03:27 test
[root@linux-01 ~]# du -sh 111.tar.gz 
216K	111.tar.gz
[root@linux-01 ~]# 
  • 2 tar打包压缩bz2格式(加-j选项)
代码语言:javascript
复制
[root@linux-01 ~]# tar -cjvf 111.tar.bz2  111/
111/
111/test/
111/1.txt
111/1.txt.zip
111/2.txt.xz
111/3.txt.xz
111/3.txt
111/2.txt
[root@linux-01 ~]# du -sh 111.tar.bz2 
176K	111.tar.bz2
[root@linux-01 ~]# 
  • 3 tar打包压缩xz格式(加-J选项)
代码语言:javascript
复制
[root@linux-01 ~]# tar -cJvf 111.tar.xz  111/
111/
111/test/
111/1.txt
111/1.txt.zip
111/2.txt.xz
111/3.txt.xz
111/3.txt
111/2.txt
[root@linux-01 ~]# du -sh 111.tar.xz 
156K	111.tar.xz

7.2 解压解包

  • 1.解压tar.gz文件
代码语言:javascript
复制
[root@linux-01 222]# tar -zxvf 111.tar.gz 
111/
111/test/
111/1.txt
111/1.txt.zip
111/2.txt.xz
111/3.txt.xz
111/3.txt
111/2.txt
  • 2.解压tar.bzip2文件
代码语言:javascript
复制
[root@linux-01 222]# tar -jxvf 111.tar.bz2 
111/
111/test/
111/1.txt
111/1.txt.zip
111/2.txt.xz
111/3.txt.xz
111/3.txt
111/2.txt
  • 3.解压tar.xz文件
代码语言:javascript
复制
[root@linux-01 222]# tar -Jxvf 111.tar.xz
111/
111/test/
111/1.txt
111/1.txt.zip
111/2.txt.xz
111/3.txt.xz
111/3.txt
111/2.txt

8.压缩文件命令总结

命令

文件格式

压缩文件(源文件不保留)

压缩文件(源文件保留)

默认压缩级别

解压 (源文件不保留)

解压 (源文件保留)

查看压缩文件

gzip

file.gz

gzip file.gz

gzip -c file > file.xz

6

gzip -d file.gz

gzip -d -c file.gz >file

zcat file.gz

bzip2

file.bz2

bzip2 file.bz2

bzip2 -c file > file.xz

9

bzip2 -d file.bz2

bzip2 -d -c file.bz2 >file

bzcat file.bz2

xz

file.xz

xz file.xz

xz -c file > file.xz

6

xz -d file.xz

xz -d -c file.xz >file

zxcat file.xz

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-03-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 压缩文件
  • 预习内容
    • 1. linux上的压缩文件后缀名
      • 2. gzip(gunzip)压缩工具
        • 2.1 gzip压缩文件,无法压缩目录
        • 2.2 gzip解压文件
        • 2.3 【zcat 3.txt.gz 】查看gzip压缩文件中的内容
      • 3. bzip2(bunzip2)压缩工具
        • 3.1 bzip2 命令安装
        • 3.2 bzip2 压缩文件,无法压缩目录
        • 3.3 bzip2 解压文件
        • 3.4 【bzcat 3.txt.bz2 】 查看bzip2 压缩文件中的内容
      • 4. xz 解压工具
        • 4.1 xz压缩文件,无法压缩目录
        • 4.2 xz 解压文件
        • 4.3 【xzcat 3.txt.xz 】查看 xz 压缩文件中的内容
      • 5. zip文件,可以压缩文件夹
        • 5.1安装zip命令
        • 5.2 zip 压缩文件
        • 5.3 unzip 解压文件
        • 5.4 【unzip -l 1.txt.zip 】查看压缩文件的文件列表
      • 6. tar打包文件
        • 6.1 tar命令参数
        • 6.2 【tar -cvf 111.tar 111】打包一个文件夹
        • 6.3 【tar -xvf 111.tar 】解包文件
        • 6.4 【tar -tf 111.tar 】查看打包文件的文件列表
      • 7.tar打包并压缩
        • 7.1 打包压缩
        • 7.2 解压解包
      • 8.压缩文件命令总结
      相关产品与服务
      文件存储
      文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档