文件的归档和压缩

文件的归档和压缩

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

归档和压缩文件的好处:节约硬盘的资源 ,加快文件传输速率

tar 命令 作用:打包、压缩文件

查看 man tar 这里 man 不是男人的意思,是 manual 手册的缩写

用法:tar [OPTION...] [FILE]...

参数:

-c create 创建文件

-x -extract [ˈekstrækt] 提取 解压还原文件

-v --verbose 显示执行详细过程

-f --file 指定备份文件

-t --list 列出压缩包中包括哪些文件,不解包,查看包中的内容

-C (大写)--directory 指定解压位置

压缩文件

[root@caicai ~]# tar -cvf grub.tar /boot/grub2/  # tar 的参数前可以不使用‘-’

[root@caicai ~]# tar cvf grub.tar /boot/grub2/

[root@caicai ~]# tar cf grub.tar /boot/grub2/

tar: 从成员名中删除开头的“/”(就是把/根路径删除掉了,就变成了 boot/grub2 相对路径了,解压时会解压到当前目录,如果不删除,那就是/boot/grub2,当解压的时候就是绝对路径了,就会覆盖

系统中此路径的文件)

[root@caicai ~]# tar -cf grub.tar /boot/grub2/

tar: 从成员名中删除开头的“/”

[root@caicai ~]# tar -tvf test.tar  # 查看包里面的文件,但不解包

解压test.tar

[root@caicai ~]# tar -xf  test.tar   # 解压后不返回文件列表

[root@caicai ~]# tar -xvf  test.tar  # 解压后返回文件列表

tar 归档+压缩

语法:tar czvf newfile.tar.gz SOURCE

语法:tar czvf 压缩后的文件名(tar.gz tar.bz2) 需要压缩的文件或目录

常用参数:

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

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

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

创建.tar.gz 包

[root@caicai ~]# tar cvf /root/etc.tar /etc

[root@caicai test]# tar zcvf /root/etc.tar.gz /etc #归档,注意备份的名字后缀

[root@caicai  test]# tar zxvf /root/etc.tar.gz #解压缩

[root@caicai  test]# tar xvf /root/etc.tar.gz #解压缩

创建.tar.bz2 包

语法: #tar jcvf newfile.tar.bz2 SOURCE

[root@caicai ~]# tar -jcvf ~/etc.tar.bz2 /etc

[root@caicai ~]# tar -jxvf ~/etc.tar.bz2 #解压缩

[root@caicai ~]# tar -xvf ~/etc.tar.bz2 #解压缩

[root@caicai ~]# tar jxvf ~/etc.tar.bz2 -C /opt #解压到 opt 目录下

创建.tar.xz 包

[root@caicai ~]# tar -Jcvf ~/etc.tar.xz /etc

[root@caicai ~]# tar -Jxvf ~/etc.tar.xz  #tar.xz 这类包,解压缩

[root@caicai ~]# tar -xvf  ~/etc.tar.xz

对比三种压缩方式后压缩比例:

[root@caicai ~]# time tar zcf /root/etc.tar.gz /etc

[root@caicai ~]# time tar jcf /root/etc.tar.bz2 /etc

[root@caicai ~]# time tar Jcf /root/etc.tar.xz /etc

[root@caicai ~]# ll -h etc.tar* (*为通配符,代表任意字符任意次)

-rw-r--r-- 1 0 root 28M 5 月 10 12:10 etc.tar

-rw-r--r-- 1 0 root 8.7M 5 月 10 12:14 etc.tar.bz2#常用

-rw-r--r-- 1 0 root 9.8M 5 月 10 12:11 etc.tar.gz #常用

-rw-r--r-- 1 0 root 7.0M 5 月 10 12:16 etc.tar.xz #这个压缩比例最高,压缩的时间是最长

zip 管理压缩文件

zip 是压缩程序,unzip 是解压程序。

压缩文件:

[root@caicai~]# zip a.zip /etc/passwd

将所有.jpg 文件压缩成一个 zip 包

[root@caicai~]# touch {1..4}.jpg

[root@caicai~]# ls

[root@caicai~]# zip all.zip *.jpg

压缩一个目录

[root@caicai~]# zip -r ~/grub.zip /boot/grub2 #一般不用

解压缩:

[root@caicai~]# unzip ~/grub.zip

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值