文章目录
- 1 . tar: 作用打包压缩文件
 
- 1) 把当前的路径下的文件打包,命名为loacl.tar
 - 2) 指定解压位置 -C
 - 3) 不解包查看tar包中的内容
 
- 2. tar 归档+压缩
 
- 1.创建tar.gz的包
 - 2. 创建tar.bz2包
 - 3. 创建.tar.xz的包
 
- 3. zip 管理压缩文件
 - 小技巧
 
- 1. 使用vim直接查看压缩包的内容
 - 2. 使用file文件查看
 
- 总结
 
- 1) ,gz的包:
 - 2).bz2 的包
 - 3).tar.bz2的包
 - 4).bz的包
 - 5).Z的包
 - 6).rar的包
 
 文件的归档和压缩命令有很多,我们今天着重来介绍以下内容:
- tar命令进行文件的归档和压缩
 - zip管理压缩文件
 
归档和压缩文件的好处:节约硬盘的资源 ,加快文件传输速率
我们先来看tar命令
1 . tar: 作用打包压缩文件
用法: tar 【参数】 file
参数:
参数  | 作用  | 
-c  | create创建文件  | 
-x  | -extract [ˈekstrækt] 提取 解压还原文件  | 
-v  | –verbose显示执行详细过程  | 
-f  | –file指定备份文件  | 
-t  | –list 列出压缩包中包括哪些文件,不解包,查看包中的内容  | 
-C  | (大写)–directory 指定解压位置  | 
举例:
1) 把当前的路径下的文件打包,命名为loacl.tar
[root@zmgaosh ~]# tar -cvf local.tar ./
[root@zmgaosh ~]# ls
2     a.txt  b.txt.patch  ***local.tar***  passwd1  zmedu.txt
a.sh  b.txt  file         passwd     test     zmeduv2.txt解压缩:
[root@zmgaosh ~]# mkdir test1
[root@zmgaosh ~]# mv local.tar test1/
[root@zmgaosh ~]# cd test1
[root@zmgaosh test1]# tar xvf local.tar 
[root@zmgaosh test1]# ls
2     a.txt  b.txt.patch  file       passwd   test       zmeduv2.txt
a.sh  b.txt  etc.tar      local.tar  passwd1  zmedu.txt
[root@zmgaosh test1]#2) 指定解压位置 -C
[root@zmgaosh ~]# ls
2     a.txt  b.txt.patch  file       passwd   test   zmedu.txt
a.sh  b.txt  etc.tar      local.tar  passwd1  test1  zmeduv2.txt
[root@zmgaosh ~]# tar xvf local.tar  -C /opt/    #解压到/opt下
[root@zmgaosh ~]# ls /opt/   #查看是否解压成功
2  a.sh  a.txt  b.txt  b.txt.patch  etc.tar  file  passwd  passwd1  test  test1  xinsz1  zmedu.txt  zmeduv2.txt
[root@zmgaosh ~]#3) 不解包查看tar包中的内容
[root@zmgaosh ~]# tar tvf local.tar 
dr-xr-x--- root/root         0 2020-06-20 19:57 ./
-rw------- root/root      5461 2020-06-19 20:55 ./.viminfo
-rw-r--r-- root/root        91 2020-06-19 20:17 ./passwd
-rwxrwxrwx root/root       157 2020-06-17 23:22 ./a.sh
-rw-r--r-- root/root       176 2013-12-29 10:26 ./.bashrc2. tar 归档+压缩
语法: tar czvf newfile.tar.gz
常用参数:
-z, --gzip 以gzip方式压缩 扩展名: tar.gz
 -j : 以bz2方式压缩的 扩展名:tar.bz2
 -J : 以xz 方式压缩 扩展名:tar.xz
举例:
1.创建tar.gz的包
[root@zmedu16 ~]# tar cvf etc.tar /etc 
[root@localhost test]# tar zcvf etc.tar.gz /etc  #归档,注意备份的名字后缀
[root@localhost test]# tar zxvf etc.tar.gz   #解压缩2. 创建tar.bz2包
语法: #tar jcvf newfile.tar.bz2  SOURCE
[root@zmedu16 ~]#  tar -jcvf etc.tar.bz2 /etc   
[root@zmedu16 ~]#  tar -jxvf etc.tar.bz2 /etc    #解压缩
[root@zmedu16 ~]#  tar jxvf etc.tar.bz2 -C  /opt    #解压到opt目录下3. 创建.tar.xz的包
[root@zmedu16 ~]#  tar -Jcvf etc.tar.xz /etc
[root@zmedu16 ~]#  tar -xvf etc.tar.xz    #tar.xz 这类包,解压缩
或:
[root@zmedu16 ~]#  tar -Jxvf etc.tar.xz三种压缩方式中, tar.gz tar.bz2是比较常用的
 tar.xz 压缩比例最高,压缩时间最长,压缩完文件最小
3. zip 管理压缩文件
zip是压缩程序,unzip是解压程序
安装zip
 [root@zmgaosh zip]# yum install zip
压缩:
[root@zmgaosh zip]# zip passwd.zip /etc/passwd
  adding: etc/passwd (deflated 61%)
[root@zmgaosh zip]# ls
passwd.zip解压缩:
[root@zmgaosh zip]# unzip passwd.zip 
Archive:  passwd.zip
  inflating: etc/passwd              
[root@zmgaosh zip]# ls
etc  passwd.zip如果要指定目标解压目录可以加参数-d
[root@zmgaosh zip]# unzip passwd.zip  -d /opt/小技巧
如何查看看压缩文件的内容
1. 使用vim直接查看压缩包的内容
[root@zmgaosh zip]# vim passwd.zip
![在这里插入图片描述 [linux]循序渐进学运维-基础命令篇-文件的归档和压缩_unzip](https://file.cfanz.cn/uploads/png/2023/04/06/10/MF0c6A06KY.png)
2. 使用file文件查看
[root@zmgaosh zip]# file passwd.zip 
passwd.zip: Zip archive data, at least v2.0 to extract总结
Linux下压缩解压命令还有很多,但最常见的还是tar命令和zip命令
比如:
1) ,gz的包:
解压:gunzip filename.gz
2).bz2 的包
bzip2 -d filename.bz2
3).tar.bz2的包
tar jxvf filename.tar.bz2
4).bz的包
bzip2 filename.bz
5).Z的包
uncompress filename.Z
6).rar的包
rar x filename.rar
                










