linux学习笔记整理(八)

第九章 文件的归档和压缩
本节所讲内容:
9.1 tar命令进行文件的归档和压缩
9.2 zip管理压缩文件
9.3 了解gzip-bzip2- xz管理压缩文件-file-sort查看文件

9.1 tar命令进行文件的归档和压缩
9.1.1 归档和压缩文件
归档和压缩文件的好处:节约硬盘的资源 ,加快文件传输速率
tar命令 作用:打包、压缩文件

作用:打包、压缩文件;tar 文件是把几个文件和(或)目录集合在一个文件里,该存档文件可以通过使用gzip、bzip2或xz等压缩工具进行行压缩后传输
查看man tar
用法:tar [OPTION...] [FILE]...
参数:
-c create创建文件
-x -extract [ˈekstrækt] 提取 解压还原文件
-v --verbose显示执行详细过程
-f --file指定备份文件
-t --list 列出压缩包中包括哪些文件,不解包,查看包中的内容
-C (大写)--directory 指定解压位置

例:给/boot/grub目录 打包
[root@xuegod63 ~]# tar -cvf grub.tar /boot/grub/ # tar的参数前可以不使用‘-’
或:
[root@xuegod63 ~]# tar cvf grub.tar /boot/grub/
tar: 从成员名中删除开头的“/”
/boot/grub/
/boot/grub/splash.xpm.gz
[root@xuegod63 ~]# ls gurb.tar
[root@xuegod63 ~]# tar xvf grub.tar #解压缩
boot/grub/
boot/grub/splash.xpm.gz
[root@xuegod63 ~]# ls boot #得到boot目录
注意:在使用绝对路径名归档文件时,将默认从文件名中删除该路径中前面的 / 符号。这样解压时,会直接解压到当前目录。 如果不移除/压缩时,当解包时,直接按绝对路径来释放,会覆盖原系统中此路径的文件。
例1:指定解压位置 -C
[root@xuegod63 ~]# tar xvf grub.tar.bz2 -C /opt/
tar: 从成员名中删除开头的“/”
/boot/grub/
/boot/grub/splash.xpm.gz
[root@xuegod63 ~]# ls /opt/
boot
例2:把两个目录或目录+文件打包成一个软件包:
[root@xuegod63 ~]# mkdir back
[root@xuegod63 ~]# cp /etc/passwd back/
[root@xuegod63 ~]# tar -cvf back.tar /boot/grub back/ /etc/passwd
tar: 从成员名中删除开头的“/”
/boot/grub/
/boot/grub/splash.xpm.gz
back/
back/passwd
/etc/passwd
例3:不解包,查看tar中的内容:
[root@xuegod63 ~]# tar -tvf grub.tar # List all files in archive.tar verbosely.
例4:对比加v的效果
[root@xuegod63 ~]# tar -xf grub.tar
[root@xuegod63 ~]# tar -xvf grub.tar
boot/grub/
boot/grub/splash.xpm.gz

9.1.2 tar 归档+压缩:
语法:tar czvf newfile.tar.gz SOURCE
常用参数:
-z, --gzip 以gzip方式压缩 扩展名: tar.gz
-j : 以bz2方式压缩的 扩展名:tar.bz2
-J : 以xz 方式压缩 扩展名:tar.xz

例1:创建.tar.gz 包
[root@xuegod63 ~]# tar cvf etc.tar /etc
[root@localhost test]# tar zcvf etc.tar.gz /etc #归档,注意备份的名字后缀
[root@localhost test]# tar zxvf etc.tar.gz #解压缩

例2:创建.tar.bz2包
语法: #tar jcvf newfile.tar.bz2 SOURCE
[root@xuegod63 ~]# tar -jcvf etc.tar.bz2 /etc
[root@xuegod63 ~]# tar -jxvf etc.tar.bz2 /etc #解压缩
[root@xuegod63 ~]# tar jxvf etc.tar.bz2 -C /opt #解压到opt目录下

例3:创建.tar.xz包
[root@xuegod63 ~]# tar -Jcvf etc.tar.xz /etc
[root@xuegod63 ~]# tar -xvf etc.tar.xz #tar.xz 这类包,解压缩
或:
[root@xuegod63 ~]# tar -Jxvf etc.tar.xz #
对比三种压缩方式后压缩比例:
[root@xuegod63 ~]# ll -h etc.tar*
-rw-r--r-- 1 0 root 36M 5月 10 12:10 etc.tar
-rw-r--r-- 1 0 root 9.6M 5月 10 12:14 etc.tar.bz2 #这个常用
-rw-r--r-- 1 0 root 12M 5月 10 12:11 etc.tar.gz #这个常用
-rw-r--r-- 1 0 root 7.7M 5月 10 12:16 etc.tar.xz #这个压缩比例最高,压缩的时间是最长


9.2 zip管理压缩文件
zip软件包解压缩命令:
zip是压缩程序,unzip是解压程序。
例1:压缩文件:
[root@xuegod63 ~]# zip a.zip /etc/passwd
例2:将所有.jpg的文件压缩成一个zip包
[root@xuegod63 ~]# zip all.zip *.jpg
例3:压缩一个目录
[root@xuegod63 ~]# zip -r grub.zip /boot/grub #一般不用

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


9.3 了解gzip-bzip2- xz管理压缩文件-file-sort查看文件
我们创建压缩的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@xuegod63 ~]# mkdir xuegod
[root@xuegod63 ~]# touch xuegod/a.txt
[root@xuegod63 ~]# gzip xuegod/a.txt
[root@xuegod63 ~]# ls xuegod/a*
xuegod/a.txt.gz
注:只能对文件进行压缩,且压缩后源文件会消失,一般不用。
(bzip2,xz这两个工具可以通过添加参数-k来保留下源文件)
[root@xuegod63 ~]# cp /etc/passwd 1.txt
[root@xuegod63 ~]# bzip2 -k 1.txt
[root@xuegod63 ~]# ls 1.txt.bz2

[root@xuegod63 ~]# xz -k 1.txt
[root@xuegod63 ~]# ls 1.txt.xz

解压:
gzip -d 文件
bzip2 -d 文件 -k 保留源文件
xz -d 文件 或 unxz 文件 -k 保留源文件
例:
[root@panda mnt]# gzip -d 1.txt.bz2
[root@xuegod63 ~]# bzip2 -d 1.txt.bz2
[root@panda mnt]# xz -d 1.txt.bz2


9.3.2 file查看文件
file命令
作用: file - determine file type #确定文件类型
用法: file /etc/passwd
注:linux系统不根据后缀名识别文件类型
用file命令查看文件的类型。
[root@xuegod63 ~]# file /etc/passwd
/etc/passwd: ASCII text

9.3.3 按一定规则排序查看文件
查看文件:
[root@xuegod63 ~]# ls -ltr 按时间排序 t 表示时间, -r 从小到大,不加r参数由大到小

[root@xuegod63 ~]# ls -lSr 按大小排序 -r 从小到大
[root@xuegod63 ~]# ls -lSrh 按大小排序 -r 从小到大 ,加-h 参数,看大小,更清楚
[root@xuegod63 ~]# ls -lS 从大到小
查看目录:
[root@xuegod63 ~]# du -sh /etc 看某个目录大小
查看分区大小:
[root@xuegod63 ~]# df -h 可以快速查看磁盘大小的存储空间

9.3.4 排序:处理大量数据时会用到的命令sort
例1:默认按字母规则进行排序
[root@xuegod63 ~]# cat /etc/passwd | sort | more
例2: 按数据排序
[root@xuegod63 ~]# vim file2 #每行随意写一些数字
例2: 按数据排序,默认从小到大
2
23
231
[root@panda mnt]# sort -n file2 #-n默认从小到大
[root@xuegod63 ~]# sort -r file2 #-r 反序排序(升序变成降序进行排序) 从大小到

例3:支持按月份排序
[root@xuegod63 ~]# vim file3 #写入以下内容
January
March
April
February
[root@xuegod63 ~]# sort -M file3
April
February
January
March
例4:组合使用
-t 指定一个用来区分键位置字符
-k 后面跟数字,指定按第几列进行排序
-r 反序排序(升序变成降序进行排序)

[root@xuegod63 ~]# sort -t ":" -k3 -r /etc/passwd | more #按: 做分隔符,以第3列,也就是用户UID,来从大到小排序
[root@xuegod63 ~]# du -h /etc | sort -r | more #把etc目录下所有文件,按从大到小排序

原文地址:https://www.cnblogs.com/apesplan/p/10497623.html