|NO.Z.00014|——————————|LinuxBasicEnd|——|Linux&常用命令.V08|——|压缩操作|

一、压缩和解压缩命令:“.zip”“.gz” .bz2”“.tar”“.tar.gz”“.tar.bz2”等。
### --- 压缩和解压缩命令
### --- 在 Linux 中可以识别的常见压缩格式有十几种,比如“.zip”“.gz” .bz2”“.tar”“.tar.gz”“.tar.bz2”等。

### --- “.zip”格式
### --- “.zip”是 Windows 中最常用的压缩格式,Linux 也可以正确识别“.zip”格式,这可以方便地和Windows 系统通用压缩文件。
### --- “.zip”格式的压缩命令
~~~     压缩命令就是 zip,其基本信息如下。
~~~     命令名称:zip。
~~~     英文原意:package and compress (archive) files。
~~~     所在路径:/usr/bin/zip。
~~~     执行权限:所有用户。
~~~     功能描述:压缩文件或目录。
~~~     命令格式如下:

[root@localhost ~]# zip [选项] 压缩包名 源文件或源目录
### --- 选项: 
~~~     -r:压缩目录
~~~     例如:
### --- “.zip”格式的解压缩命令
~~~     “.zip”格式的解压缩命令是 unzip,其基本信息如下。
~~~     命令名称:unzip。

[root@localhost ~]# zip ana.zip anaconda-ks.cfg
~~~     英文原意:list, test and extract compressed files in a ZIP archive。
~~~     所在路径:/usr/bin/unzip。
~~~     执行权限:所有用户。
~~~     功能描述:列表、测试和提取压缩文件中的文件。
~~~     命令格式如下:
### --- 选项:
~~~     -d:指定解压缩位置
~~~     例如:

[root@localhost ~]# unzip [选项] 压缩包名
 
~~~     把压缩包解压到指定位置
[root@localhost ~]# unzip -d /tmp/ ana.zip
### --- zip
### --- 压缩文件

[root@server11 ~]# zip yanqi.zip yanqi yanqi_yasuo 
  adding: yanqi (deflated 80%)
  adding: yanqi_yasuo (stored 0%)
[root@server11 ~]# rm -rf yanqi yanqi_yasuo 
[root@server11 ~]# unzip yanqi.zip 
Archive:  yanqi.zip
  inflating: yanqi                   
 extracting: yanqi_yasuo             
[root@server11 ~]# ls
yanqi  yanqi_yasuo  yanqi.zip
### --- -d指定解压缩位置

[root@server11 ~]# unzip -d /tmp yanqi.zip 
Archive:  yanqi.zip
  inflating: /tmp/yanqi              
 extracting: /tmp/yanqi_yasuo        
[root@server11 ~]# ls /tmp/
yanqi  yanqi_yasuo
二、“.gz”格式    不会打包
### --- “.gz”格式    不会打包
### --- “.gz”格式的压缩命令
~~~     “.gz”格式是 Linux 中最常用的压缩格式,使用 gzip 命令进行压缩,其基本信息如下。
~~~     命令名称:gzip。
~~~     英文原意:compress or expand files。
~~~     所在路径:/bin/gzip。
~~~     执行权限:所有用户。
~~~     功能描述:压缩文件或目录。
~~~     这个命令的格式如下:

[root@localhost ~]# gzip [选项] 源文件
### --- 选项:
~~~     -c:将压缩数据输出到标准输出中,可以用于保留源文件
~~~     -d:解压缩
~~~     -r:压缩目录
~~~     使用-c 选项,但是不让压缩数据输出到屏幕上,而是重定向到压缩文件中
~~~     这样可以在压缩文件的同时不删除源文件

[root@localhost ~]# gzip -c anaconda-ks.cfg > anaconda-ks.cfg.gz
### --- “.gz”格式的解压缩命令
~~~     如果要解压缩“.gz”格式,那么使用“gzip -d 压缩包”和“gunzip 压缩包”命令都可以。我们
~~~     先看看 gunzip 命令的基本信息。
~~~     命令名称:gunzip。
~~~     英文原意:compress or expand files。
~~~     所在路径:/bin/gunzip。
~~~     执行权限:所有用户。
~~~     功能描述:解压缩文件或目录。

### --- 例如:
~~~     两个命令都可以解压缩“.gz”格式
[root@localhost ~]# gunzip install.log.gz
[root@localhost ~]# gzip -d anaconda-ks.cfg.gz
### --- gzip压缩完会把源文件删除掉;gzip默认是不保留源文件的

[root@server11 ~]# gzip yanqi yanqi_yasuo 
[root@server11 ~]# ls
yanqi.gz  yanqi_yasuo.gz
[root@server11 ~]# gzip -d yanqi.gz yanqi_yasuo.gz
[root@server11 ~]# ls
yanqi  yanqi_yasuo  yanqi.zip
### --- 保留源文件
[root@server11 ~]# gzip -c yanqi                                    // 保留源文件
# ®׀`•yanqi342剌J䰴2•Q@
                      dq9:9s9•¹NΉ\䨥4#   

[root@server11 ~]# gzip -c yanqi >> yanqi.gz                        // 输出保留源文件,单大于号是覆盖,双大于号是追加
[root@server11 ~]# ls
yanqi  yanqi.gz 
### --- 压缩目录:不会压缩目录,而是把里面的文件直接压缩

[root@server11 ~]# gzip -r yanqi.mulu/
[root@server11 ~]# ls
yanqi.mulu
[root@server11 ~]# ls yanqi.mulu/
yanqi.gz  yanqi_yasuo.gz
### --- gz格式是不会打包的
 
[root@server11 ~]# gzip -r yanqi_mulu/
三、“.bz2”格式                不能压缩目录
### --- “.bz2”格式                不能压缩目录
### --- “.bz2”格式的压缩命令
~~~     “.bz2”格式是 Linux 的另一种压缩格式,从理论上来讲,“.bz2”格式的算法更先进、压缩比更好;而“.gz”格式相对来讲压缩的时间更快。
~~~     “.bz2”格式的压缩命令是 bzip2,我们来看看这个命令的基本信息。
~~~     命令名称:bzip2。
~~~     英文原意:a block-sorting file compressor。
~~~     所在路径:/usr/bin/bzip2。
~~~     执行权限:所有用户。
~~~     功能描述:.bz2 格式的压缩命令。
~~~     来看看 bzip2 命令的格式。

[root@localhost ~]# bzip2 [选项] 源文件
### --- 选项: 
~~~     -d:解压缩
~~~     -k:压缩时,保留源文件
~~~     -v:显示压缩的详细信息
### --- 例如:
[root@localhost ~]# bzip2 anaconda-ks.cfg
 
### --- 压缩成.bz2 格式
~~~     保留源文件压缩
[root@localhost ~]# bzip2 -k install.log.syslog
### --- “.bz2”格式的解压缩命令
~~~     “.bz2”格式可以使用“bzip2 -d 压缩包”命令来进行解压缩,也可以使用“bunzip2 压缩包”命令来进行解压缩。先看看 bunzip2 命令的基本信息。
~~~     命令名称:bunzip2。
~~~     英文原意:a block-sorting file compressor。
~~~     所在路径:/usr/bin/bunzip2。
~~~     执行权限:所有用户。
~~~     功能描述:.bz2 格式的解压缩命令。
~~~     两个命令都可以解压缩

[root@localhost ~]# bunzip2 anaconda-ks.cfg.bz2
[root@localhost ~]# bzip2 -d install.log.syslog.bz2
### --- .bz2不能压缩目录
[root@server11 ~]# bzip2 yanqi
[root@server11 ~]# ls
yanqi.bz2  yanqi_yasuo

### --- 保留源文件
[root@server11 ~]# bzip2 -k yanqi_yasuo 
[root@server11 ~]# ls
yanqi.bz2  yanqi_yasuo  yanqi_yasuo.bz2
### --- 解压缩

[root@server11 ~]# bzip2 -d yanqi.bz2 
[root@server11 ~]# ls
yanqi 
[root@server11 ~]# bzip2 -d yanqi_yasuo.bz2 
bzip2: Output file yanqi_yasuo already exists.
[root@server11 ~]# rm -rf yanqi_yasuo
[root@server11 ~]# bzip2 -d yanqi_yasuo.bz2 
[root@server11 ~]# ls
yanqi  yanqi_yasuo
### --- 压缩目录

[root@server11 ~]# bzip2 -r yanqi_mulu/                             // 直接报错
四、“.tar”格式            打包不会压缩
### --- “.tar”格式            打包不会压缩
### --- “.tar”格式的打包命令
~~~     “.tar”格式的打包和解打包都使用 tar 命令,区别只是选项不同。我们先看看 tar 命令的基本信息。
~~~     命令名称:tar。
~~~     英文原意:tar。
~~~     所在路径:/bin/tar。
~~~     执行权限:所有用户。
~~~     功能描述:打包与解打包命令。
~~~     命令的基本格式如下:

[root@localhost ~]# tar [选项] [-f 压缩包名] 源文件或目录
### --- 选项: 
~~~     -c:打包
~~~     -f:指定压缩包的文件名。压缩包的扩展名是用来给管理员识别格式的,所以一定要正确指定扩展名
~~~     -v:显示打包文件过程
~~~     打包,不会压缩
[root@localhost ~]# tar -cvf anaconda-ks.cfg.tar anaconda-ks.cfg

### --- “.tar”格式的解打包命令
~~~     “.tar”格式的解打包也需要使用 tar 命令,但是选项不太一样。命令格式如下:
[root@localhost ~]# tar [选项] 压缩包
### --- 选项: 
~~~     -x:解打包
~~~     -f:指定压缩包的文件名
~~~     -v:显示解打包文件过程
~~~     -t:测试,就是不解打包,只是查看包中有哪些文件
~~~     -C(大) 目录: 指定解打包位置
### --- 例如
~~~     解打包到当前目录下

[root@localhost ~]# tar -xvf anaconda-ks.cfg.tar
### --- 打包
[root@server11 ~]# ls
 yanqi_mulu.tar
[root@server11 ~]# tar -cvf yanqi_mulu.tar

### --- 解打包
[root@server11 ~]# tar -xvf yanqi_mulu.tar 
yanqi_mulu/
yanqi
yanqi_yasuo
### --- 压缩

[root@server11 ~]# ls
yanqi_yasuo.tar
[root@server11 ~]# gzip yanqi_yasuo.tar 
[root@server11 ~]# ls
yanqi_yasuo.tar.gz
### --- 解压缩;解打包

[root@server11 ~]# ls
yanqi_yasuo.tar.gz
[root@server11 ~]# gzip -d yanqi_yasuo.tar.gz 
[root@server11 ~]# ls
yanqi_yasuo.tar
[root@server11 ~]# tar -xvf yanqi_yasuo.tar 
yanqi
yanqi_mulu/
yanqi_yasuo
[root@server11 ~]# ls
yanqi  yanqi_mulu  yanqi_yasuo  yanqi_yasuo.tar
五、“.tar.gz”和“.tar.bz2”格式
### --- “.tar.gz”和“.tar.bz2”格式
~~~     使用 tar 命令直接打包压缩。命令格式如下:
### --- 选项:
~~~     -z:压缩和解压缩“.tar.gz”格式
~~~     -j:压缩和解压缩“.tar.bz2”格式

[root@localhost ~]# tar [选项] 压缩包 源文件或目录
### --- 例如:.tar.gz 格式
~~~     把/tmp/目录直接打包压缩为“.tar.gz”格式
[root@localhost ~]# tar -zcvf tmp.tar.gz /tmp/
 
~~~     解压缩与解打包“.tar.gz”格式
[root@localhost ~]# tar -zxvf tmp.tar.gz
### --- 例如:.tar.bz2 格式
~~~     打包压缩为“.tar.bz2”格式,注意压缩包文件名
[root@localhost ~]# tar -jcvf tmp.tar.bz2 /tmp/
 
~~~     解压缩与解打包“.tar.bz2”格式
[root@localhost ~]# tar -jxvf tmp.tar.bz2
### --- 再举几个例子:
~~~     建立测试目录和测试文件

[root@localhost ~]# mkdir test
[root@localhost ~]# touch test/abc
[root@localhost ~]# touch test/bcd
[root@localhost ~]# touch test/cde
### --- 压缩
[root@localhost ~]# tar -zcvf test.tar.gz test/
### --- 只查看,不解压
[root@localhost ~]# tar -ztvf test.tar.gz

~~~     解压缩到指定位置
[root@localhost ~]# tar -zxvf test.tar.gz -C /tmp
~~~     只解压压缩包中的特定文件,到指定位置
[root@localhost ~]# tar -zxvf test.tar.gz -C /tmp test/cde
### --- .tar.gz
### --- 压缩

[root@server11 ~]# ls
yanqi  yanqi_mulu  yanqi_yasuo
[root@server11 ~]# tar -zcvf yanqi.tar.gz yanqi yanqi_mulu/ yanqi_yasuo 
yanqi
yanqi_mulu/
yanqi_yasuo
[root@server11 ~]# ls
yanqi  yanqi_mulu  yanqi.tar.gz  yanqi_yasuo
### --- 解压缩

[root@server11 ~]# rm -rf yanqi yanqi_mulu/ yanqi_yasuo 
[root@server11 ~]# tar -zxvf yanqi.tar.gz 
yanqi
yanqi_mulu/
yanqi_yasuo
[root@server11 ~]# ls
yanqi  yanqi_mulu  yanqi.tar.gz  yanqi_yasuo
### --- .tar.bz2
### --- 压缩

[root@server11 ~]# tar -jcvf yanqi.tar.bz2 yanqi yanqi_mulu/ yanqi_yasuo 
yanqi
yanqi_mulu/
yanqi_yasuo
[root@server11 ~]# ls
yanqi  yanqi_mulu  yanqi.tar.bz2  yanqi_yasuo
### --- 解压缩

[root@server11 ~]# rm rm -rf yanqi yanqi_mulu/ yanqi_yasuo 
[root@server11 ~]# tar -jxvf yanqi.tar.bz2 yanqi yanqi_mulu/ yanqi_yasuo 
yanqi
yanqi_mulu/
yanqi_yasuo
[root@server11 ~]# ls
yanqi  yanqi_mulu  yanqi.tar.bz2  yanqi_yasuo
### --- 只看不解压       -t:选项;只查看不解压

[root@server11 ~]# ls
yanqi.tar.gz
[root@server11 ~]# tar -ztvf yanqi.tar.gz 
-rw-r--r-- root/root     10240 2021-03-04 21:10 yanqi
drwxr-xr-x root/root         0 2021-03-04 21:03 yanqi_mulu/
-rw-r--r-- root/root         0 2021-03-04 21:01 yanqi_yasuo
[root@server11 ~]# ls
yanqi.tar.gz
### --- 指定解压位置 -C:选项:指定解压位置

[root@server11 ~]# tar -ztvf yanqi.tar.gz -C /tmp/
-rw-r--r-- root/root     10240 2021-03-04 21:10 yanqi
drwxr-xr-x root/root         0 2021-03-04 21:03 yanqi_mulu/
-rw-r--r-- root/root         0 2021-03-04 21:01 yanqi_yasuo
[root@server11 ~]# rm -rf /tmp/*
[root@server11 ~]# ls /tmp/     
yanqi  yanqi_yasuo
### --- 解压执行文件                                                  // 将需要解压的某一个文件写在目标目录后面

[root@server11 ~]# tar -zxvf yanqi.tar.gz -C /tmp/ yanqi             // yanqi这个文件就是需要解压的单个文件
yanqi
[root@server11 ~]# ll /tmp/
total 12
-rw-r--r-- 1 root root 10240 Mar  4 21:10 yanqi

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(23)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示