tar压缩与归档

在Windows中,压缩工具是常常使用到的,将一些文本、软件等等放在一个文件夹下,进行压缩成一个压缩包。

这个工具在Linux世界中同样存在,只是Linux独有的三个压缩工具是Windows没有的,分别是gzip、bzip2、xz。在日常的数据备份中常常使用归档压缩。

压缩:

gzip压缩

[root@localhost ~]# cp /etc/passwd 1.txt  #将/etc/passwd的内容复制到1.txt文档中
[root@localhost ~]# gzip 1.txt            #将1.txt这个文档以gzip格式压缩
[root@localhost ~]# ls                    #查看压缩包
1.txt.gz                                  #gzip压缩的包对应以.gz结尾

bzip2压缩:

[root@localhost ~]# cp /etc/shadow 2.txt  #将/etc/shadow的内容复制到2.txt中
[root@localhost ~]# bzip2 2.txt           #将2.txt文档以bzip2格式压缩
[root@localhost ~]# ls                    #查看压缩包
2.txt.bz2                                 #bzip2格式压缩的包对应以.bz2结尾

xz压缩:

[root@localhost ~]# cp /etc/group 3.txt   #将/etc/group的内容复制到3.txt文档中
[root@localhost ~]# xz 3.txt              #将3.txt文档以xz格式压缩
[root@localhost ~]# ls                    #查看压缩包
3.txt.xz                                  #xz格式压缩的包对应以.xz结尾

 

tar工具的使用

格式:tar  [选项]  tar包的名字  被归档的文件1  被归档的文件2  被归档的文件3...

选项:c,创建归档;

x,释放归档;

f,指定归档文件名称;

t,显示归档中的文件清单;

P,保持归档文件的绝对路径;

C(大写),指定解压路径;

z,以.gz格式压缩或者解压;

j,以.bz2格式压缩或解压;

J,以.xz格式压缩或者解压;

tar的归档与压缩:

[root@localhost opt]# cd  /opt          #切换到/opt目录下
[root@localhost opt]# tar -cPf  file01.tar /etc/passwd  /home/  /boot/
#创建一个名为file01.tar的归档,其中包括了/etc/passwd,/home,/boot这三个目录下的内容
[root@localhost opt]# ls              #查看归档
file01.tar

[root@localhost opt]# gzip  file01.tar    #对file01归档文件进行gzip压缩
[root@localhost opt]# ls                #查看压缩包
file01.tar.gz                          #file01.tar文档经过gzip压缩的包


[root@localhost opt]# tar -zcPf /root/test01.tar.gz  /etc/passwd  /home/  /boot/
#在/root下创建一个名为/test01.tar的归档,包括/etc/passwd,/home,/boot的内容,并以gzip格式进行压缩,最终得到一个test01.tar.gz的压缩包

[root@localhost opt]# tar -jcPf /root/test02.tar.bz2  /etc/passwd  /home/  /boot/
#在/root下创建一个名为/test02.tar的归档,包括/etc/passwd,/home,/boot的内容,并以bzip2格式进行压缩,最终得到一个test02.tar.gz的压缩包

[root@localhost opt]# tar -JcPf /root/test03.tar.xz  /etc/passwd  /home/  /boot/ 
#在/root下创建一个名为/test03.tar的归档,包括/etc/passwd,/home,/boot的内容,并以xz格式进行压缩,最终得到一个test03.tar.gz的压缩包

[root@localhost opt]# ls  /root     #查看/root路径下的压缩包
test01.tar.gz
test02.tar.bz2
test03.tar.xz

tar查看压缩包的内容

[root@localhost ~]# tar -tf test01.tar.gzip    #查看test01.tar.gzip的内容
[root@localhost ~]# tar -tf test02.tar.gzip    #查看test02.tar.bzip2的内容
[root@localhost ~]# tar -tf test03.tar.gzip    #查看test03.tar.xz的内容

tar的解压

[root@localhost ~]# tar -xf /root/test01.tar.gz -C /mnt/ 
#将test01.tar.gz 指定解压的位置为/mnt
[root@localhost ~]# cd /mnt         #切换到/mnt目录下
[root@localhost mnt]# ls            #查看解压后的文件
test01.tar                          #解压后的归档文件

#注意:所有的操作都有f选项 必须放在所有选项的最后

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值