# tar 命令进行文件的归档和压缩


**归档和压缩文件**

**归档和压缩文件的好处：<font color="red">节约硬盘的资源，加快文件传输速率</font>** 

![打包压缩.png](https://s2.loli.net/2022/01/10/1FUQipRrvMcSEhI.png)


## tar

    作用：打包、压缩文件,tar 文件是把几个文件和（或）目录集合在一个文件里，
    该存档文件可以通过使用gzip、 bzip2 或 xz 等压缩工具进行压缩后传输
    
    查看 man tar 或者 tar --help
    
    用法：tar [OPTION...] [FILE]... 
    参数：
    -c      create 创建压缩包
    -x      -extract [ˈekstrækt]   提取、解压压缩包
    -v      --verbose 显示执行详细过程
    -f      --file 指定文件
    -t      --list 列出压缩包中包括哪些文件，不解包，查看压缩包中的内容
    -r      递归打包，向压缩归档文件末尾追加文件   #需要注意的是这个参数不能与压缩参数一起使用
    -C  （大写）--directory     指定解压位置
    --exclude    不包含的意思
    --newer-mtime   仅备份比某个时刻还要新的文件

---
    例子1：
    
    [root@exercise1 ~]# tar -cvf /opt/grub.tar /boot/grub/   #tar的参数前可以不使用'-'
    或
    [root@exercise1 ~]# tar cvf /opt/grub.tar /boot/grub/   #包名前面加上路径可以指定打包位置，
    #不然默认在当前目录下
    tar: 从成员名中删除开头的“/”
    /boot/grub/
    /boot/grub/splash.xpm.gz
    [root@exercise1 ~]# ls /opt/
    1  1d  1e  1f  2  3  4  5  grub.tar  i.bak  id  j  log  log.bak  messages
    [root@exercise1 ~]# 



**注意：**

**在使用绝对路径名归档文件时，将默认从文件名中删除该路径中前面的/符号。这样解压时，会直接解压到当前目录。如果不移除/压缩时，当解包时，直接按绝对路径来释放，会覆盖原系统中此路径的文件。**


---
    例子2：指定解压位置   -C
    
    [root@exercise1 ~]# tar xvf /opt/grub.tar -C /opt/
    boot/grub/
    boot/grub/splash.xpm.gz
    [root@exercise1 ~]# ls /opt/
    1  1d  1e  1f  2  3  4  5  boot  grub.tar  i.bak  id  j  log  log.bak  messages
    [root@exercise1 ~]# 

---
    例子3：把两个目录或目录+文件打包成一个软件包
    
    [root@exercise1 ~]# mkdir /opt/back
    [root@exercise1 ~]# cp /etc/passwd /opt/b
    back/ boot/ 
    [root@exercise1 ~]# cp /etc/passwd /opt/back/
    [root@exercise1 ~]# tar cvf /opt/back.tar /boot/grub /opt/back/ /etc/passwd
    tar: 从成员名中删除开头的“/”
    /boot/grub/
    /boot/grub/splash.xpm.gz
    /opt/back/
    /opt/back/passwd
    /etc/passwd
    [root@exercise1 ~]# 

---
    例子4：不解包，查看tar中的内容   -t
    
    [root@exercise1 ~]# tar tvf /opt/back.tar 
    drwxr-xr-x root/root         0 2022-01-09 09:29 boot/grub/
    -rw-r--r-- root/root      1350 2011-11-16 05:03 boot/grub/splash.xpm.gz
    drwxr-xr-x root/root         0 2022-01-11 08:30 opt/back/
    -rw-r--r-- root/root       846 2022-01-11 08:30 opt/back/passwd
    -rw-r--r-- root/root       846 2022-01-09 15:10 etc/passwd
    [root@exercise1 ~]# 

---
    例子5：对比加v的效果
    
    [root@exercise1 ~]# tar xf /opt/back.tar 
    [root@exercise1 ~]# tar xvf /opt/grub.tar 
    boot/grub/
    boot/grub/splash.xpm.gz
    [root@exercise1 ~]# 

---
    例子6：将不需要打包的文件除去
    
    [root@exercise1 ~]# tar xvf /opt/grub.tar 
    boot/grub/
    boot/grub/splash.xpm.gz
    [root@exercise1 ~]# tar cvf /opt/grub2.tar /boot/grub --exclude=*xpm.gz 
    tar: 从成员名中删除开头的“/”
    /boot/grub/   #可以看出之前/boot/grub/下面是有.xpm.gz的文件，现在没有
    [root@exercise1 ~]# 


---
    例子7：备份修改时间更新的文件
    
    [root@exercise1 opt]# mkdir /opt/test
    [root@exercise1 opt]# cp /etc/passwd /opt/test/
    [root@exercise1 opt]# ls /opt/test/
    passwd
    [root@exercise1 opt]# vim /opt/test/passwd 
    [root@exercise1 opt]# tar cvf /opt/back2.tar /opt/test/ --newer-mtime "20221223 10:50"
    tar: 选项 --newer-mtime: 将日期 ‘20221223 10:50’ 当作 2022-12-23 10:50:00
    tar: 从成员名中删除开头的“/”
    /opt/test/
    tar: /opt/test/passwd: 文件未改变；未输出
    [root@exercise1 opt]# tar tf back2.tar 
    opt/test/
    
    ###可以看到不是在2022-12-23 10:50:00之前更新的不打包


---
## <font color='red'>tar 归档+压缩：</font>

语法：tar czvf newfile.tar.gz   文件或目录 

常用参数：

 -z   :           以 gzip 方式压缩   扩展名： tar.gz

 -j   ：         以 bz2 方式压缩的   扩展名：tar.bz2

 -J   ：         以 xz 方式压缩     扩展名：tar.xz

    例子1：创建.tar.gz包
    [root@exercise1 ~]# tar cvf /opt/etc.tar /etc
    [root@exercise1 ~]# tar zcvf /opt/etc.tar /etc   #归档,注意压缩包的名字后缀
    [root@exercise1 ~]# ls /opt/
    1   1e  2  4  back       back.tar  etc.tar     grub2.tar  i.bak  j    log.bak   test
    1d  1f  3  5  back2.tar  boot      etc.tar.gz  grub.tar   id     log  messages
    [root@exercise1 ~]# tar zxvf /opt/etc.tar.gz /etc   #解压缩


---
    例子2：创建.tar.bz2包
    语法： #tar jcvf newfile.tar.bz2   SOURCE
    
    [root@exercise1 ~]# tar -jcvf /opt/etc.tar.bz2 /etc/
    tar: 从成员名中删除开头的“/”
    /etc/
    /etc/fstab
    /etc/crypttab
    /etc/mtab
    /etc/resolv.conf
    /etc/grub.d/
    /etc/grub.d/00_header
    /etc/grub.d/01_users
    /etc/grub.d/10_linux
    ……
    tar (child): bzip2：无法 exec: 没有那个文件或目录
    tar (child): Error is not recoverable: exiting now
    [root@exercise1 ~]# 
    
    报错信息如下：
    tar (child): bzip2：无法 exec: 没有那个文件或目录
    tar (child): Error is not recoverable: exiting now
    原因：缺少bzip2包
    解决办法：
    yum install -y bzip2
    
    [root@exercise1 ~]# tar -jcvf /opt/etc.tar.bz2 /etc/
    
    [root@exercise1 ~]# tar -jxvf /opt/etc.tar.bz2   #解压缩到当前目录
    
    [root@exercise1 ~]# tar -jxvf /opt/etc.tar.bz2 -C /opt/   #指定解压到opt目录下


---
    例子3：创建.tar.xz 包
    [root@exercise1 ~]# tar -Jcvf /opt/etc.tar.xz /etc/
    
    [root@exercise1 ~]# tar -Jxvf /opt/etc.tar.xz -C /etc/   #tar.xz 这类包，解压缩
    
    或
    
    [root@exercise1 ~]# tar -Jxvf /opt/etc.tar.xz -C /etc/


---
**对比三种压缩方式后压缩比例：**
    
    [root@exercise1 ~]# ll -h /opt/*etc.tar*
    -rw-r--r--. 1 root root  27M 1月  12 23:43 /opt/etc.tar
    -rw-r--r--. 1 root root 8.3M 1月  12 23:50 /opt/etc.tar.bz2  #这个不常用bzip2 压缩生成的文件比gzip小，但使用不如gzip广； 
    -rw-r--r--. 1 root root 9.4M 1月  12 23:44 /opt/etc.tar.gz   #这个常用gzip压缩速度最快；
    -rw-r--r--. 1 root root 6.6M 1月  12 23:58 /opt/etc.tar.xz   #这个压缩比例最高，压缩的时间是最长xz
    #压缩工具相对较新，但是会提供最佳的压缩率
    [root@exercise1 ~]# 

---

---
## zip管理压缩文件
**zip软件包解压缩命令：**

**zip是压缩程序，unzip是解压程序。**

    [root@exercise1 ~]# zip /opt/a.zip /etc/passwd
    -bash: zip: 未找到命令
    [root@exercise1 ~]# 
    
    报错：未安装
    解决：yum -y install zip


​    
    例子1 ：压缩文件:
    [root@exercise1 ~]# zip /opt/a.zip /etc/passwd
      adding: etc/passwd (deflated 57%)
    [root@exercise1 ~]# 


​    
    例子2：将所有.jpg 的文件压缩成一个zip包
    [root@exercise1 ~]# zip /opt/all.zip *.jpg


​    
    例子3: 压缩一个目录
    [root@exercise1 ~]# zip -r /opt/grub.zip /boot/grub       #一般不用


​    
    解压缩：
    [root@exercise1 ~]# unzip /opt/grub.zip
    [root@exercise1 ~]# unzip /opt/grub.zip -d /opt/   #  -d   解压到指定的目标/opt


---

---
## (了解)压缩工具

压缩工具：gzip   bzip2   zip xz

常见的压缩格式： .gz   .bz2     .xz   .zip


**语法格式：**

**压缩**

gzip 文件  ====》   gzip a.txt  =====》 a.txt.gz

bzip2 文件    ===》 bzip2 b.txt  =====》 b.txt.bz2

xz 文件  ===》xz c.txt ===》c.txt.xz      

    [root@exercise1 ~]# mkdir /opt/abc
    
    [root@exercise1 ~]# touch /opt/abc/a.txt
    
    [root@exercise1 ~]# gzip /opt/abc/a.txt 
    
    [root@exercise1 ~]# ls /opt/abc/a*
    
    /opt/abc/a.txt.gz
    
    [root@exercise1 ~]# 


​    
    注：只能对文件进行压缩，且压缩后源文件会消失，一般不用。 
    （bzip2，xz 这两个工具可以通过添加参数-k 来保留下源文件）
    
    [root@exercise1 ~]# cp /etc/passwd /opt/1.txt
    
    [root@exercise1 ~]# bzip2 -k /opt/1.txt 
    
    [root@exercise1 ~]# ls /opt/1.txt.bz2 
    /opt/1.txt.bz2
    
    [root@exercise1 ~]# xz -k /opt/1.txt
    
    [root@exercise1 ~]# ls /opt/1.txt.xz
    /opt/1.txt.xz
    
    [root@exercise1 ~]# 



 

**解压：**  
gzip   -d   文件

bzip2 -d   文件 -k 保留源文件

xz      -d   文件 或     unxz   文件       -k 保留源文件

    例：
    [root@exercise1 ~]# gzip -d 1.txt.bz2    
    [root@exercise1 ~]# bzip2 -d 1.txt.bz2 
    [root@exercise1 ~]# xz -d 1.txt.bz2


​    




​    



​    

​    
