第7课预习内容

6.1 压缩打包介绍
6.2 gzip压缩工具
6.3 bzip2压缩工具 
6.4 xz压缩工具
6.5 zip压缩工具
6.6 tar打包
6.7 打包并压缩

1 压缩打包介绍

1.1 压缩打包其实我们在windows中经常看到.rar格式的文件,而在linux中有自已的特有的压缩工具和格式。我们使用压缩文件,不仅能查看磁盘空间,而且在传输时还能节省网络带宽。在linux中有以下几种格式:.gz .bz2 .tar .tar.gz .tar.bz2 .tar.xz

2 gzip压缩工具

2.1gzip 的用法为:

gzip [-d#] filename #为1-9数字 -d在解压时使用
默认压缩级别为6

2.2 gzip压缩:现在我们实操一下这个命令,

2.2.1我们先在/home目录下创建一个1目录,

[root@localhost home]# mkdir 1
[root@localhost home]# ls
1  knightlai  user1

2.2.2然后把/etc下面的配置文件拷贝一份到1目录下面。

[root@localhost home]# cp -r /etc/  /home/1
[root@localhost home]# ls
1  knightlai  user1
2.2.3查看一下两个文件大小是否一致

[root@localhost home]# du -sh 1
30M    1
[root@localhost home]# du -sh /etc
30M    /etc
2.2.4 现在压缩一下这个文件 ”-rw-r--r--.  1 root root  12288 Aug  2 09:01 aliases.db“

[root@localhost etc]# gzip aliases.db
[root@localhost etc]# ll
-rw-r--r--.  1 root root      0 Aug  2 09:01 AA.txt
-rw-r--r--.  1 root root     16 Aug  2 09:01 adjtime
-rw-r--r--.  1 root root   1518 Aug  2 09:01 aliases
-rw-r--r--.  1 root root    943 Aug  2 09:01 aliases.db.gz

2.2.5 现在可以查看这个解压文件内容”zcat aliases.db.gz

扫描二维码关注公众号,回复: 2689806 查看本文章

[root@localhost etc]# zcat aliases.db.gz
rootwebmasternewsusenetroottoorrootsshdrootsquidrootsmmsprootshutdownrootsecuritypostmastersalesrootrpcuserrootradiusdrootquaggarootpostmasterrootpcaprootoperatorrootntprootnscdrootnocrootnfsnobodynewsnewsadminrootmysqlpostmastermarketingrootmailnullrootlprootingrespostmasterinforootidentroothostmasterrootgdmftpftpadmftpftp-adminrootftprootdovecotrootdesktoprootdecoderootdbusrootdaemonrootapacherootamandabackuprootadmrootabuselocalhost.localdomain

2.2.6 解压文件:gzip -d aliases.db.gz

[root@localhost etc]# gzip -d aliases.db.gz
[root@localhost etc]# ls
AA.txt                   csh.login                grub2.cfg      locale.conf               passwd-         resolv.conf.save  sudo-ldap.conf
adjtime                  dbus-1                   grub.d         localtime                 pkcs11          rpc               sysconfig
aliases                  default                  gshadow        login.defs                pki             rpm               sysctl.conf
aliases.db    

注:gzip 不能压缩目录

3 bzip2压缩工具 

3.1 bzip2压缩用法:

bzip2 [-dz]filename -d解压缩文件 -z压缩文件
压缩级别为1-9,默认级别是9 ,加或不加-z选项也可以压缩文件

3.2 压缩文件:bzip2 aliases.gzip2

[root@localhost etc]# bzip2 aliases.db
[root@localhost etc]# ls
AA.txt                   csh.login                grub2.cfg      locale.conf               passwd-         resolv.conf.save  sudo-ldap.conf
adjtime                  dbus-1                   grub.d         localtime                 pkcs11          rpc               sysconfig
aliases                  default                  gshadow        login.defs                pki             rpm               sysctl.conf
aliases.db.bz2

3.3也可以用bzcat查看压缩文件内容:bzcat aliases.db.bz2

[root@localhost etc]# bzcat aliases.db.bz2
rootwebmasternewsusenetroottoorrootsshdrootsquidrootsmmsprootshutdownrootsecuritypostmastersalesrootrpcuserrootradiusdrootquaggarootpostmasterrootpcaprootoperatorrootntprootnscdrootnocrootnfsnobodynewsnewsadminrootmysqlpostmastermarketingrootmailnullrootlprootingrespostmasterinforootidentroothostmasterrootgdmftpftpadmftpftp-adminrootftprootdovecotrootdesktoprootdecoderootdbusrootdaemonrootapacherootamandabackuprootadmrootabuselocalhost.l

3.4解压文件:bzip2 -d aliases.db.bz2

[root@localhost etc]# bzip2 -d aliases.db.bz2
[root@localhost etc]# ls
AA.txt                   csh.login                grub2.cfg      locale.conf               passwd-         resolv.conf.save  sudo-ldap.conf
adjtime                  dbus-1                   grub.d         localtime                 pkcs11          rpc               sysconfig
aliases                  default                  gshadow        login.defs                pki             rpm               sysctl.conf
aliases.db

4 xz压缩工具(这个命令用的不多)

4.1 xz命令的格式为:

xz[-dz]filename,-z 压缩 -d 解压

4.2 压缩文件:xz aliases.db

[root@localhost etc]# xz aliases.db
[root@localhost etc]# ls
AA.txt                   csh.login                grub2.cfg      locale.conf               passwd-         resolv.conf.save  sudo-ldap.conf
adjtime                  dbus-1                   grub.d         localtime                 pkcs11          rpc               sysconfig
aliases                  default                  gshadow        login.defs                pki             rpm               sysctl.conf
aliases.db.xz 

4.3查看压缩文件内容:xzcat aliases.db.xz

[root@localhost etc]# xzcat aliases.db.xz
rootwebmasternewsusenetroottoorrootsshdrootsquidrootsmmsprootshutdownrootsecuritypostmastersalesrootrpcuserrootradiusdrootquaggarootpostmasterrootpcaprootoperatorrootntprootnscdrootnocrootnfsnobodynewsnewsadminrootmysqlpostmastermarketingrootmailnullrootlprootingrespostmasterinforootidentroothostmasterrootgdmftpftpadmftpftp-adminrootftprootdovecotrootdesktoprootdecoderootdbusrootdaemonrootapacherootamandabackuprootadmrootabuselocalhost.l

4.4 解压文件:unxz aliases.db.xz

[root@localhost etc]# unxz aliases.db.xz
[root@localhost etc]# ls
AA.txt                   csh.login                grub2.cfg      locale.conf               passwd-         resolv.conf.save  sudo-ldap.conf
adjtime                  dbus-1                   grub.d         localtime                 pkcs11          rpc               sysconfig
aliases                  default                  gshadow        login.defs                pki             rpm               sysctl.conf
aliases.db

注:不可以压缩目录

5zip压缩工具

5.1 zip压缩包在windows中我们经常可以看到,这个在linux中也比较常用。它不仅可以压缩目录和文件,压缩目录时,需要指定目录下的文件。

5.1.1 zip可能需要安装这个yum包:yum -install -y zip

5.2 zip后面先跟目标文件名,然后跟要压缩的文件或目录。

[root@localhost etc]# zip 2.zip aliases.db
  adding: aliases.db (deflated 93%)
[root@localhost etc]# ll
total 1048
-rw-r--r--.  1 root root   1084 Aug  2 10:46 2.zip

5.3解压文件:unzip 2.zip

[root@localhost etc]# unzip 2.zip
Archive:  2.zip
replace aliases.db? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
  inflating: aliases.db 

5.4 如果压缩整个目录的话,需要加上“-r”参数

[root@localhost /]# zip -r  1.zip  /home/1
 adding: home/1/etc/selinux/targeted/active/modules/100/logging/lang_ext (stored 0%)
  adding: home/1/etc/selinux/targeted/active/modules/100/logrotate/ (stored 0%)
  adding: home/1/etc/selinux/targeted/active/modules/100/logrotate/cil (stored 0%)
  adding: home/1/etc/selinux/targeted/active/modules/100/logrotate/hll (deflated 9%)
  adding: home/1/etc/selinux/targeted/active/modules/100/logrotate/lang_ext (stored 0%)
  adding: home/1/etc/selinux/targeted/active/modules/100/logwatch/ (stored 0%)
  adding: home/1/etc/selinux/targeted/active/modules/100/logwatch/cil (stored 0%)
[root@localhost /]# ls
1.zip

6 tar打包

6.1 tar本身是一个打包工具,可以把目录打包成一个文件,它把所有文件整合成一个大文件,方便复制和移动。

6.2 tar的用法,它有以下几个参数:

tar [zjxcvfpP]filename tar
-z 表示同时用gzip压缩
-j 表示同时用bzip2压缩
-J 表示同时用xz压缩
-x 表示解包或者解压缩
-t 表示查看tar包里的文件
-c 表示建立一个tar包或者压缩文件包
-v 表示可视化
-f 后面跟文件名,如果有多个参数,请把-f参数写在最后面
-p 表示使用原文件的属性(不常用)
-P 表示可以使用绝对路径(不常用)
--exclude filename:表示打包时不要将filename文件打包在内

6.3 打包文件:打包文件时原文件不会被删除

[root@localhost home]# tar -cvf 1.tar 1 
1/etc/selinux/targeted/active/modules/100/logging/lang_ext
1/etc/selinux/targeted/active/modules/100/logrotate/
1/etc/selinux/targeted/active/modules/100/logrotate/cil
1/etc/selinux/targeted/active/modules/100/logrotate/hll
1/etc/selinux/targeted/active/modules/100/logrotate/lang_ext
1/etc/selinux/targeted/active/modules/100/logwatch/
1/etc/selinux/targeted/active/modules/100/logwatch/cil
1/etc/selinux/targeted/active/modules/100/logwatch/hll

看一下原文件还在

[root@localhost home]# ls
1  1.tar 

6.4 查看打包文件内容列表: tar -tf 1.tar

[root@localhost home]# tar -tf 1.tar
1/etc/selinux/targeted/active/modules/100/logging/lang_ext
1/etc/selinux/targeted/active/modules/100/logrotate/
1/etc/selinux/targeted/active/modules/100/logrotate/cil
1/etc/selinux/targeted/active/modules/100/logrotate/hll
1/etc/selinux/targeted/active/modules/100/logrotate/lang_ext

6.5 解包:tar -xvf 1.tar

[root@localhost home]# tar -xvf 1.tar
1/etc/selinux/targeted/active/modules/100/logging/lang_ext
1/etc/selinux/targeted/active/modules/100/logrotate/
1/etc/selinux/targeted/active/modules/100/logrotate/cil
1/etc/selinux/targeted/active/modules/100/logrotate/hll
1/etc/selinux/targeted/active/modules/100/logrotate/lang_ext
1/etc/selinux/targeted/active/modules/100/logwatch/
 

7 打包并压缩

7.1 tar命令还有一个功能就是在打包时直接压缩,它支持gzip压缩,bzip压缩和xz压缩。

7.2 .1使用tar打包并用gzip压缩:

[root@localhost home]# tar -zcvf 1.tar.gz 1
1/etc/selinux/targeted/active/modules/100/logging/lang_ext
1/etc/selinux/targeted/active/modules/100/logrotate/
1/etc/selinux/targeted/active/modules/100/logrotate/cil
1/etc/selinux/targeted/active/modules/100/logrotate/hll
1/etc/selinux/targeted/active/modules/100/logrotate/lang_ext
1/etc/selinux/targeted/active/modules/100/logwatch/
查看一下打包出来的文件:

[root@localhost home]# ls
1  1.tar  1.tar.gz  knightlai  user1

7.2.2 这里也可以使用-tf选项查看打包中的内容列表:tar -tf 1.tar.gz

 [root@localhost home]# tar -tf 1.tar.gz
1/etc/selinux/targeted/active/modules/100/logrotate/
1/etc/selinux/targeted/active/modules/100/logrotate/cil
1/etc/selinux/targeted/active/modules/100/logrotate/hll
1/etc/selinux/targeted/active/modules/100/logrotate/lang_ext
1/etc/selinux/targeted/active/modules/100/logwatch/

7..2.3解压.tar.gz里的内容:tar -zxvf 1.tar.gz

[root@localhost home]# tar -zxvf 1.tar.gz

1/etc/selinux/targeted/active/modules/100/logging/lang_ext
1/etc/selinux/targeted/active/modules/100/logrotate/
1/etc/selinux/targeted/active/modules/100/logrotate/cil
1/etc/selinux/targeted/active/modules/100/logrotate/hll
1/etc/selinux/targeted/active/modules/100/logrotate/lang_ext
1/etc/selinux/targeted/active/modules/100/logwatch/

[root@localhost home]# ls
1  1.tar  1.tar.gz  knightlai  user1
 

7.3我们再看一下用bzip2这种压缩方式 

7.3.1  打包并用bzip2压缩是用这个命令:tar -jcvf 1.tar.bz2 1

[root@localhost home]# tar -jcvf 1.tar.bz2 1

1/etc/selinux/targeted/active/modules/100/logging/lang_ext
1/etc/selinux/targeted/active/modules/100/logrotate/
1/etc/selinux/targeted/active/modules/100/logrotate/cil
1/etc/selinux/targeted/active/modules/100/logrotate/hll
1/etc/selinux/targeted/active/modules/100/logrotate/lang_ext
1/etc/selinux/targeted/active/modules/100/logwatch/
[root@localhost home]# ls
1  1.tar  1.tar.bz2  1.tar.gz  knightlai  user1

7.3.2 这里也可以查看内容列表:tar -tf 1.tar.bz2

[root@localhost home]# tar -tf 1.tar.bz2

1/etc/selinux/targeted/active/modules/100/logging/lang_ext
1/etc/selinux/targeted/active/modules/100/logrotate/
1/etc/selinux/targeted/active/modules/100/logrotate/cil
1/etc/selinux/targeted/active/modules/100/logrotate/hll
1/etc/selinux/targeted/active/modules/100/logrotate/lang_ext
1/etc/selinux/targeted/active/modules/100/logwatch/
1/etc/selinux/targeted/active/modules/100/logwatch/cil
7.3.3 解压.tar.bz2里面的内容:tar -jxvf 1.tar.bz2

[root@localhost home]# tar -jxvf 1.tar.bz2

1/etc/selinux/targeted/active/modules/100/logging/lang_ext
1/etc/selinux/targeted/active/modules/100/logrotate/
1/etc/selinux/targeted/active/modules/100/logrotate/cil
1/etc/selinux/targeted/active/modules/100/logrotate/hll
1/etc/selinux/targeted/active/modules/100/logrotate/lang_ext
1/etc/selinux/targeted/active/modules/100/logwatch/
[root@localhost home]# ls
1  1.tar  1.tar.bz2  1.tar.gz  knightlai  user1
 

猜你喜欢

转载自blog.csdn.net/a1779078902/article/details/81506369
今日推荐