当前位置 : 主页 > 操作系统 > centos >

Linux云计算集群架构师->第九章 文件的归档和压缩

来源:互联网 收集:自由互联 发布时间:2022-09-29
第九章 文件的归档和压缩 (上课时间2021-06-26,笔记整理时间2021-07-2) 本节所讲内容: 9.1 tar 命令进行文件的归档和压缩 9.2 zip 管理压缩文件 9.3 了解 gzip-bzip2- xz 管理压缩文件-file-sort 查

第九章 文件的归档和压缩

(上课时间2021-06-26,笔记整理时间2021-07-2)

本节所讲内容:

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

9.2 zip 管理压缩文件

9.3 了解 gzip-bzip2- xz 管理压缩文件-file-sort 查看文件

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

9.1.1 归档和压缩文件

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

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

image-20210627132855636

这幅图的就是说 123456 这几个文件打包成了一个 a.tar 文件,但是这个 a.tar 还是很大,继续用gzip 进行压缩,变成了一个更小的压缩文件。

作用:打包、压缩文件;tar 文件是把几个文件和(或)目录集合在一个文件里,该存档文件可以通过使用 gzip、bzip2 或 xz 等压缩工具进行行压缩后传输

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

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

参数:

-c create 创建文件

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

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

-f --file 指定备份文件

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

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

[root@Centos83 ~]# tar -cvf grub.tar /boot/grub2/ # tar 的参数前可以不使用‘-’ [root@Centos83 ~]# tar cf grub.tar /boot/grub2/ tar: Removing leading `/' from member names

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

[root@Centos83 ~]# tar -cf grub.tar /boot/grub2/ [root@Centos83 ~]# ls grub.tar [root@Centos83 ~]# tar xvf grub.tar #解压缩 boot/grub2/ boot/grub2/device.map [root@Centos83 ~]# ls boot/ #解压出来的boot目录 grub2

把两个目录或目录+文件打包成一个归档包:

[root@Centos83 ~]# mkdir ./back [root@Centos83 ~]# cp /etc/passwd ./back [root@Centos83 ~]# tar -cvf back.tar /boot/grub2/ root/back/ /etc/passwd tar: 从成员名中删除开头的“/” /boot/grub2/ /boot/grub2/device.map tar: 从硬连接目标中删除开头的“/” /boot/grub2/i386-pc/ /boot/grub2/i386-pc/gcry_md5.mod

不解包,查看 tar 中的内容:

[root@Centos83 ~]# tar -tvf back.tar

对比加 v 的效果:

[root@Centos83 ~]# tar -xvf back.tar [root@Centos83 ~]# tar -xf back.tar

9.1.2 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@Centos83 ~]# tar cvf /root/etc.tar /etc # 文件归档 [root@Centos83 ~]# tar zcvf /root/etc.tar.gz /etc # 文件压缩 [root@Centos83 ~]# du -sh /etc # etc源目录大小 30M /etc [root@Centos83 ~]# du -sh ./etc* # 压缩后文件大小对比 27M ./etc.tar 6.0M ./etc.tar.gz [root@Centos83 ~]# tar zxvf /root/etc.tar.gz #也可以用xf解压

创建.tar.bz2 包

[root@Centos83 ~]# tar -jcvf /root/etc.tar.bz2 /etc #创建bz2格式的压缩包 [root@Centos83 ~]# du -sh /root/etc* #压缩后文件大小对比 27M /root/etc.tar 4.4M /root/etc.tar.bz2 6.0M /root/etc.tar.gz [root@Centos83 ~]# tar -jxvf /root/etc.tar.bz2 #可以用-jxvf解压.bz2格式文件 [root@Centos83 ~]# tar -xvf /root/etc.tar.bz2 #也可以自动识别压缩格式并解压 [root@Centos83 ~]# tar -jxvf /root/etc.tar.bz2 -C /opt/ #-C 可以指定解压路径

创建.tar.xz 包

[root@Centos83 ~]# tar -Jcvf /root/etc.tar.xz /etc #创建bz2格式的压缩包 [root@Centos83 ~]# du -sh /etc # etc源目录大小 30M /etc [root@Centos83 ~]# du -sh /root/etc* #压缩后文件大小对比 27M /root/etc.tar 4.4M /root/etc.tar.bz2 6.0M /root/etc.tar.gz 3.7M /root/etc.tar.xz #这个压缩比例最高,压缩的时间是最长 [root@Centos83 ~]# tar -Jxvf /root/etc.tar.xz #可以用-Jxvf解压.xz格式文件 [root@Centos83 ~]# tar -xvf /root/etc.tar.xz #也可以自动识别压缩格式并解压

9.2 zip 管理压缩文件

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

压缩文件:

[root@Centos83 ~]# zip test.zip /etc/passwd #将/etc/passwd 创建test.zip压缩包 adding: etc/passwd (deflated 62%) [root@Centos83 ~]# ls *.zip test.zip [root@Centos83 ~]# touch {1..4}.png #创建1~4.png文件 [root@Centos83 ~]# ls *.png 1.png 2.png 3.png 4.png [root@Centos83 ~]# zip png.zip *.png #将所有.png文件压缩到png.zip adding: 1.png (stored 0%) adding: 2.png (stored 0%) adding: 3.png (stored 0%) adding: 4.png (stored 0%) [root@Centos83 ~]# zip -r ./grub.zip /boot/grub2/ [root@Centos83 ~]# ls *.zip grub.zip png.zip test.zip [root@Centos83 ~]# unzip ./grub.zip #解压zip文件 [root@Centos83 ~]# unzip ./grub.zip -d /opt/ #解压zip文件 -d 参数指定解压目录

9.3 了解gzip-bzip2- xz 管理压缩文件-file-sort 查看文件

Linux 中有gzip bzip2 xz 等单独的命令,linux 中一件事可能有很多种方法能实现,下面的几种方法,tar 都能做到,所以必须记住tar 的方法,其他方法了解下就可以。

我们创建压缩的TAR 存档,TAR 命令它支持三种不同的压缩方式。

gzip 压缩速度最快;

bzip2 压缩生成的文件比gzip 小,但使用不如gzip 快;

xz 压缩工具相对较新,但是会提供最佳的压缩率(速度最慢)

9.3.1 压缩工具

压缩工具:gzip bzip2 zip xz

常见的压缩格式: .gz .bz2 .xz .zip

语法格式:

压缩

gzip 文件 -->> gzip a.txt -->> a.txt.gz

bzip2 文件 -->> bzip2 b.txt -->> b.txt.bz2

xz 文件 -->> xz c.txt -->> c.txt.xz

[root@Centos83 test]# ls test.txt [root@Centos83 test]# gzip test.gz test.txt gzip: test.gz: No such file or directory [root@Centos83 test]# ll #压缩完后源文件被删除掉了 总用量 4 -rw-r--r--. 1 root bin 29 6月 26 17:17 test.txt.gz

注:只能对文件进行压缩,且压缩后源文件会消失,我们很少希望源文件会消失,所以我们一般不用(了解)。

bzip2,xz 这两个工具可以通过添加参数-k 来保留下源文件

解压:

gzip -d 文件

bzip2 -d 文件 -k 保留源文件

xz -d 文件 -k 保留源文件

[root@Centos83 ~]# cd passwd [root@Centos83 passwd]# ls passwd shadow [root@Centos83 passwd]# bzip2 -k passwd shadow # 以bz2方式压缩 passwd 和 shadow 文件 [root@Centos83 passwd]# ls passwd passwd.bz2 shadow shadow.bz2 [root@Centos83 passwd]# xz -k passwd #以 xz 方式压缩 passwd 文件 [root@Centos83 passwd]# ls passwd passwd.bz2 passwd.xz shadow shadow.bz2 [root@Centos83 passwd]# bzip2 -d passwd.bz2 #解压 passwd.bz2 文件 [root@Centos83 passwd]# xz -d passwd.xz #解压 passwd.xz 文件

9.3.2 file 查看文件

file 命令

作用: file - determine file type #确定文件类型

用法: file /etc/passwd

注:linux 系统不根据后缀名识别文件类型

用 file 命令查看文件的类型:

[root@Centos83 ~]# vim music1.mp3 [root@Centos83 ~]# touch music2.mp3 [root@Centos83 ~]# file music1.mp3 music2.mp3 music1.mp3: ASCII text # ASCII码 (美国信息交换标准码) music2.mp3: empty # 空文件 [root@Centos83 ~]# file /dev/sda /dev/sda: block special (8/0) # 块特殊设备文件 [root@Centos83 ~]# file /etc/ /etc/: directory # 目录 [root@Centos83 ~]# file /dev/fd /dev/fd: symbolic link to /proc/self/fd # 符号链接 [root@Centos83 ~]# file /etc/passwd /etc/passwd: ASCII text # ASCII码 (美国信息交换标准码) [root@Centos83 ~]# alias # 别名 alias ll='ls -l --color=auto' # ll = ls -l [root@Centos83 ~]# ls -l # -l 显示详细信息 [root@Centos83 ~]# ls -lt # -t 按时间排序 [root@Centos83 ~]# ls -r # -r 从小到大,不加 r 参数由大到小 [root@Centos83 ~]# ls -lSr # -l 显示详细信息 -S 按文件大小排序 -r 从小到大 [root@Centos83 ~]# ls -lSrh # -h 参数,看大小 [root@Centos83 ~]# du -h /etc # –h 以人类可读的方式打印文件大小 [root@Centos83 ~]# du -sh /etc # 看某个目录大小 –s 只显示每个参数的总数 [root@Centos83 ~]# du -hd 1 /etc # -d 查看目录层及,0 级,1 级,2 级,3 级等 [root@Centos83 ~]# df -h # 可以快速查看磁盘大小的存储空间 [root@Centos83 ~]# lsblk # 查看磁盘及分区情况

9.3.4 排序:处理大量数据时会用到的命令 sort

[root@Centos83 ~]# cat /etc/passwd | sort | more [root@Centos83 ~]# sort -n /etc/passwd #-n 默认从小到大(根据字符串数值比较) [root@Centos83 ~]# sort -r /etc/passwd #-r 反序排序(升序变成降序进行排序)从大到小 [root@Centos83 ~]# sort -nr /etc/passwd #-r 按字符串数值反序排序 从大到小 [root@Centos83 ~]# sort -t ":" -k3 -nr /etc/passwd | more #按:做分隔符,以第 3列,也就是用户 UID,按数值来从大到小排序 [root@Centos83 ~]# du /etc | sort -nr |more # 根据字符串大小反向排序 [root@Centos83 ~]# du -h /etc | sort -hr |more #把 etc 目录下所有文件,按由大到小排序 sort 其它参数: #-t 使用指定的分隔符 #-k 后面跟数字,指定按第几列进行排序 #-r 反序排序(升序变成降序进行排序)计算机编码排序 #-n 根据字符串数值比较排序

总结:

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

9.2 zip 管理压缩文件

9.3 了解 gzip-bzip2- xz 管理压缩文件-file-sort 查看文件

【文章转自:防御服务器 http://www.558idc.com/aqt.html提供,感恩】
网友评论