RHEL8--- 归档及压缩

本章主要介绍如何将Linux的文件进行归档和压缩。

  • 使用tar对多个文件进行归档
  • 使用gzip对文件进行压缩和解压
  • 使用bzip2对文件进行压缩和解压
  • 使用tar结合gzip 实现文件的归档和压缩
  • 使用tar结合bzip2 实现文件的归档和压缩

1、归档

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

tar的语法如下。

  • tar cvf aa.tar file1 file2 file3
  • c的意思是创建归档
  • v的意思是创建归档时显示被归档的文件,为了简化输出,可以不加v选项
  • f用于指定归档文件
  • 这里的意思是把file1、file2、file3归档到aa.tar中

归档文件一般使用tar后缀。

先做准备,拷贝几个文件到当前目录,命令如下。

​
[root@rhel03-23 a]# cp /etc/hosts /etc/passwd /etc/services .
[root@rhel03-23 a]# ls
hosts  passwd  services

然后把 hosts、passwd、services三个文件归档成一个文件aa.tar,命令如下。

[root@rhel03-23 a]# tar cf aa.tar hosts passwd services
[root@rhel03-23 a]# ls
aa.tar  hosts  passwd  services

 这里归档文件是aa.tar,查看它们的大小,命令如下。

[root@rhel03-23 a]# ls -lh passwd hosts services aa.tar
-rw-r--r--. 1 root root 690K 11月 30 06:44 aa.tar
-rw-r--r--. 1 root root  158 11月 30 06:42 hosts
-rw-r--r--. 1 root root 2.7K 11月 30 06:42 passwd
-rw-r--r--. 1 root root 677K 11月 30 06:42 services

  可以看到,aa.tar文件的大小比三个文件大小的总和还大,说明用tar只有归档功能,并没 有压缩功能。 

  从上面的操作可以看出,当创建好归档文件后,源文件还是在的,即把 hosts、 passwd,services归档到aa.tar中之后,这三个文件依然存在。如果希望归档之后同时删除源 文件,可以加上--remove-files选项。

删除aa.tar,然后再次归档,命令如下。

[root@rhel03-23 a]# rm -rf aa.tar
[root@rhel03-23 a]# tar cf aa.tar hosts passwd services --remove-files 
[root@rhel03-23 a]# ls
aa.tar

可以看到,三个文件归档到aa.tar 中之后就被删除了。

查看归档文件中有哪些文件,可以使用t选项,命令如下。

[root@rhel03-23 a]# tar -tf aa.tar
hosts
passwd
services

想在归档文件中追加一个文件用r选项,命令如下。

[root@rhel03-23 a]# cp /root/anaconda-ks.cfg .
[root@rhel03-23 a]# ls
aa.tar  anaconda-ks.cfg
[root@rhel03-23 a]# tar rf aa.tar anaconda-ks.cfg 
[root@rhel03-23 a]# tar -tf aa.tar
hosts
passwd
services
anaconda-ks.cfg

这里已经把anaconda-ks.cfg 写入aa.tar中了。 

对归档文件进行解档,解档的语法如下。

  • tar xvf aa.tar
  • 这里×表示解档
  • v显示解档出来的文件,为了简化输出,v选项可以不写
  • f用于指定归档文件

将aa.tar解档,命令如下。

[root@rhel03-23 a]# tar xf aa.tar
[root@rhel03-23 a]# ls
aa.tar  anaconda-ks.cfg  hosts  passwd  services

  可以看到,解档时默认为解档到当前目录。如果要解档到指定的目录,需要加上“-C指 定目录”,现在要把aa.tar解档到/opt目录中,命令如下。 

[root@rhel03-23 a]# ls /opt/
yy
[root@rhel03-23 a]# tar xf aa.tar -C /opt/
[root@rhel03-23 a]# ls /opt/
anaconda-ks.cfg  hosts  passwd  services  yy

然后清空/opt中的内容,命令如下。

[root@rhel03-23 a]# rm -rf /opt/*
[root@rhel03-23 a]# ls /opt/
[root@rhel03-23 a]# 

当然,也可以只解档aa.tar中的某一个文件,只要在正常解档命令后面加上要解档的文件即可。

把aa.tar中的hosts解档出来放在/opt中,命令如下。

[root@rhel03-23 a]# ls /opt/
[root@rhel03-23 a]# 
[root@rhel03-23 a]# tar xf aa.tar -C /opt/ hosts
[root@rhel03-23 a]# ls /opt/
hosts

需要注意的是,这里hosts要写在-C /opt的后面。

2、压缩 

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

先创建一个测试文件,命令如下。

[root@rhel03-23 a]# dd if=/dev/zero of=file bs=1M count=100
记录了100+0 的读入
记录了100+0 的写出
104857600 bytes (105 MB, 100 MiB) copied, 0.387568 s, 271 MB/s
  • f是读取哪里的内容,这里是/dev/zero。
  • of是组成的文件名。
  • count指定zero的数目。
  • bs指定每个zero的大小,如果没有指定单位则默认为B。

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

[root@rhel03-23 a]# ls -lh file
-rw-r--r--. 1 root root 100M 11月 30 06:54 file

2.1、gzip 的用法 

gzip的用法如下

  • 压缩:gzip file,压缩出来的文件后缀为gz
  •  解压:gzip ‐d file.gz

使用gzip对file进行压缩,命令如下。

[root@rhel03-23 a]# gzip file
[root@rhel03-23 a]# ls -lh file.gz 
-rw-r--r--. 1 root root 100K 11月 30 06:54 file.gz

  可以看到,file原来100M,压缩成file.gz的大小只有100K。因为file文件过于简单,所以 压缩率很高。 

用gzip把 file.gz解压,命令如下。

[root@rhel03-23 a]# gzip -d file.gz

一般情况下,tar 和 gzip结合在一起使用,用法如下。

  •  tar zcf aa.tar.gz file1 file2 file3

  在tar选项中加z表示调用gzip,z和c一起使用就表示压缩,后缀一般为tar.gz。这句话的 意思是把file1、file2、file3归档并压缩到aa.tar.gz 中。 

把file归档并压缩到aa.tar.gz 中,命令如下。

[root@rhel03-23 a]# tar zcf aa.tar.gz file
[root@rhel03-23 a]# ls -lh aa.tar.gz file
-rw-r--r--. 1 root root 100K 11月 30 07:00 aa.tar.gz
-rw-r--r--. 1 root root 100M 11月 30 06:54 file

解压 tar.gz格式的文件用tar zxf aa.tar.gz。

这里z表示调用gzip,与x一起使用就表示解压。

[root@rhel03-23 a]# rm -rf file
[root@rhel03-23 a]# ls
aa.tar  aa.tar.gz  anaconda-ks.cfg  hosts  passwd  services
[root@rhel03-23 a]# tar zxf aa.tar.gz 
[root@rhel03-23 a]# ls
aa.tar  aa.tar.gz  anaconda-ks.cfg  file  hosts  passwd  services

2.2、bzip2的用法 

bzip2也是常用的压缩工具,用法如下。

  • 压缩:bzip2 file,压缩出来的文件后缀为bz2
  • 解压:bzip2 ‐d fle.bz2 

使用bzip2对file进行压缩,命令如下。 

[root@rhel03-23 a]# bzip2 file 
[root@rhel03-23 a]# ls -lh file.bz2 
-rw-r--r--. 1 root root 113 11月 30 06:54 file.bz2

这里把100M的file压缩到1K以下。因为file文件过于简单,所以压缩率很高。 

使用bzip2进行解压,命令如下。

[root@rhel03-23 a]# bzip2 -d file.bz2

一般情况下,tar 和 bzip2结合在一起使用,用法如下。

  • tar jcf aa.tar.gz file1 file2 file3 

  在tar选项中加j表示调用bzip2,f和c一起使用就表示压缩,后缀一般为tar.bz2。这句话 的意思是把file1、file2、file3 归档并压缩到aa.tar.bz2中。 

[root@rhel03-23 a]# tar jcf aa.tar.bz2 file
[root@rhel03-23 a]# ls -lh aa.tar.bz2 
-rw-r--r--. 1 root root 192 11月 30 07:07 aa.tar.bz2

解压tar.bz2格式的文件用tar jxf aa.tar.bz2。

这里 j 表示调用bzip2,与x一起使用就表示解压。

[root@rhel03-23 a]# rm -rf file
[root@rhel03-23 a]# ls
aa.tar  aa.tar.bz2  aa.tar.gz  anaconda-ks.cfg  hosts  passwd  services
[root@rhel03-23 a]# tar jxf aa.tar.bz2 
[root@rhel03-23 a]# ls
aa.tar  aa.tar.bz2  aa.tar.gz  anaconda-ks.cfg  file  hosts  passwd  services

2.3、zip 的用法 

zip是常用的压缩工具,在 Windows 中也经常使用。zip的用法如下。

  • 压缩文件: zip aa.zip file,把file压缩到aa.zip中
  • 压缩目录:zip ‐r bb.zip dir,把目录dir压缩到bb.zip中 

如果解压zip格式的压缩文件,则用unzip命令,用法如下。

  • unzip aa.zip 

把file压缩到aa.zip中,命令如下

[root@rhel03-23 a]# zip aa.zip file
  adding: file (deflated 100%)
[root@rhel03-23 a]# ls -lh aa.zip
-rw-r--r--. 1 root root 100K 11月 30 07:10 aa.zip

解压zip格式的压缩文件,命令如下

[root@rhel03-23 a]# rm -rf file
[root@rhel03-23 a]# ls
aa.tar  aa.tar.bz2  aa.tar.gz  aa.zip  anaconda-ks.cfg  hosts  passwd  services
[root@rhel03-23 a]# unzip aa.zip
Archive:  aa.zip
  inflating: file                    
[root@rhel03-23 a]# ls
aa.tar  aa.tar.bz2  aa.tar.gz  aa.zip  anaconda-ks.cfg  file  hosts  passwd  services

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值