第六章 归档及压缩

目录

一、归档和解档

1.归档。

2.解档。

二、压缩和解压

1.创建测试文件。

2.gzip的用法。

3.bzip2的用法

4.zip的用法

一、归档和解档

1.归档。

  • 为了方便文件在网络上传输,我们需要把多个文件打包成一个文件(归档),常见的归档命令包括tar和cpio。

(1)tar的语法,这里的意思是把file1file2file3归档到aa.tar中 。

[root@rhel1 ~]# mkdir file1 file2 file3                    创建三个文件夹
[root@rhel1 ~]# tar cvf aa.tar file1 file2 file3           归档
  • c:意思是创建归档
  • v:意思是创建归档时显示被归档的文件,为了简化输出,可以不加v选项。
  • f:用于指定归档文件。

(2)把hosts、passwd、services三个文件拷贝到root目录。

[root@rhel1 ~]# cp /etc/hosts /etc/passwd /etc/services /root/

(3)然后把 hosts、passwd、services三个文件归档成一个文件bb.tar。

[root@rhel1 ~]# tar cvf bb.tar hosts passwd services       归档
[root@rhel1 ~]# ls -lh                                     查看大小
[root@rhel1 ~]# tar -tf bb.tar                             查看归档文件中有哪些文件
hosts
passwd
services

2.解档。

(1)对bb.tar归档文件进行解档。

[root@rhel1 ~]# tar xvf bb.tar
  • x:这里×表示解档。
  • v:v显示解档出来的文件,为了简化输出,v选项可以不写
  • f:f用于指定归档文件。

(2)现在要把bb.tar解档到/opt目录中。

[root@rhel1 ~]# tar xvf bb.tar -C /opt/
[root@rhel1 ~]# ls /opt/
  • -C:意思是指定目录。

(3)把bb.tar中的hosts解档出来放在/opt目录中。

[root@rhel1 ~]# rm -rf /opt/*                        清空/opt目录
[root@rhel1 ~]# tar xvf bb.tar -C /opt/ hosts
[root@rhel1 ~]# ls /opt/                             查看结果

二、压缩和解压

1.创建测试文件。

  • 前面讲tar只是归档,并没有压缩功能。要是想压缩,有专门的工具,如 gzip、bzip2、
    zip。

(1)先创建一个测试文件。

[root@rhel1 ~]# dd if=/dev/zero of=file bs=1M count=100

记录了100+0 的读入
记录了100+0 的写出
104857600 bytes (105 MB, 100 MiB) copied, 0.150366 s, 697 MB/s
  • if:是读取哪里的内容,这里是/dev/zero。
  • of:of是组成的文件名。
  • bs:指定每个zero的大小,如果没有指定单位则默认为B。
  • count:指定zero的数目。

(2)这里的意思是拿100个大小为1M的/dev/zero组成一个名称叫file的文件,此file的大小应该是100M。

[root@rhel1 ~]# ls -lh file
-rw-r--r--. 1 root root 100M 12月  4 22:51 file

2.gzip的用法。

(1)使用gzip对file进行压缩。

[root@rhel1 ~]# gzip file
[root@rhel1 ~]# ls -lh file.gz 
-rw-r--r--. 1 root root 100K 12月  4 22:51 file.gz
  • 可以看到,file原来100M,压缩成file.gz的大小只有100K。因为file文件过于简单,所以压缩率很高。

(2)用gzip解压的方法。

[root@rhel1 ~]# gzip -d file.gz

(3)一般情况下,tar 和 gzip结合在一起使用。

[root@rhel1 ~]# tar zcf aa.tar.gz file1 file2 file3
  • 在tar选项中加 z 表示调用gzip,z和c一起使用就表示压缩,后缀一般为tar.gz。

(4)把file归档并压缩到aa.tar.gz中。

[root@rhel1 ~]# tar zcf aa.tar.gz file                归档并压缩
[root@rhel1 ~]# ls -lh aa.tar.gz file                 查看结果

3.bzip2的用法

(1)使用bzip2对file进行压缩。

[root@rhel1 ~]# bzip2 file
[root@rhel1 ~]# ls -lh file.bz2 
-rw-r--r--. 1 root root 113 12月  4 22:51 file.bz2

(2)使用bzip2进行解压。

[root@rhel1 ~]# bzip2 -d file.bz2

(3)一般情况下,tar 和 bzip2结合在一起使用。

[root@rhel1 ~]# tar jcf aa.tar.bz2 file1 file2 file3
  • 在tar选项中加 j 表示调用bzip2,j和c一起使用就表示压缩,后缀一般为tar.bz2。

(4)把file归档并压缩到aa.tar.bz2中。

[root@rhel1 ~]# tar jcf aa.tar.bz2 file
[root@rhel1 ~]# ls -lh aa.tar.bz2 
-rw-r--r--. 1 root root 192 12月  4 23:29 aa.tar.bz2

4.zip的用法

(1)把file压缩到aa.zip中。

[root@rhel1 ~]# zip aa.zip file                            压缩
  adding: file (deflated 100%)
[root@rhel1 ~]# ls -lh file aa.zip                         查看
-rw-r--r--. 1 root root 100K 12月  5 21:57 aa.zip
-rw-r--r--. 1 root root 100M 12月  4 22:51 file

(2)解压zip格式的压缩文件。

[root@rhel1 ~]# rm -rf file                        删除file
[root@rhel1 ~]# unzip aa.zip                       把file解压出来
Archive:  aa.zip
  inflating: file
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值