内容:
9.1 tar命令进行文件的归档和压缩
9.2 zip管理压缩文件
9.3 了解gzip bzip2 xz管理压缩文件 file-sort查看文件
9.1 tar命令进行文件的归档和压缩
9.1.1 归档和压缩文件
归档和压缩文件的好处:节约硬盘资源,加快文件传输速率
tar命令:打包、压缩文件。把几个文件或目录集合在一个文件中,该存档文件可以通过使用gzip、bzip2或xz等压缩工具进行压缩后传输。
# man tar
# tar [option] [file]
# option:
# -c create 创建文件
# -x -extract 提取 解压还原文件
# -v --verbose 显示执行详细过程
# -f --file 指定备份文件
# -t --list 列出压缩包中包括哪些文件,不解包,查看包中内容
# -C --directory 指定解压位置
# 给/boot/grub 目录打包
[root@localhost ~]# tar -cvf grub.tar /boot/grub
tar: Removing leading ‘/’ from member names
/boot/grub/
/boot/grub/splash.xpm.gz
# 解压缩
[root@localhost ~]# tar xvf grub.tar
boot/grub/
boot/grub/splash.xpm.gz
[root@localhost ~]# ls boot/
grub
# 注意:在使用绝对路径归档文件时,将默认从文件名中删除该路径中前面的"/"。这样解压时,会 # 直接解压至当前目录。如果不移出"/"压缩时,当解压缩时,直接会按绝对路径来释放,会覆盖原 # 系统中此文件的路径
# 指定解压位置
[root@localhost ~]# tar -xvf grub.tar -C /opt/
boot/grub/
boot/grub/splash.xpm.gz
[root@localhost ~]# ls /opt/
boot
# 把多个目录或目录+文件进行压缩
[root@localhost ~]# tar -cvf back.tar /boot/grub back/ /etc/passwd
tar: Removing leading `/' from member names
/boot/grub/
/boot/grub/splash.xpm.gz
back/
back/passwd
/etc/passwd
# 不解压,查看包中内容
[root@localhost ~]# tar -tvf grub.tar
drwxr-xr-x root/root 0 2020-07-22 01:51 boot/grub/
-rw-r--r-- root/root 1350 2011-11-16 05:03 boot/grub/splash.xpm.gz
9.1.2 tar归档和压缩
使用方法:tar -zcvf newfile.tar.gz SOURCE
常用参数:
-z --gzip 以gzip方式压缩,文件后缀: .tar.gz
-j --bzip2 以bz2方式压缩,文件后缀: .tar.bz2
-J --xz 以xz方式压缩,文件后缀: .tar.xz
# 创建tar包
[root@localhost ~]# tar -cvf etc.tar /etc/
[root@localhost ~]# tar -zcvf etc.tar.gz /etc/ #归档
[root@localhost ~]# tar -zxvf etc.tar.gz #解压缩
# 创建bz2包
[root@localhost ~]# tar -jcvf etc.tar.bz2 /etc #压缩
[root@localhost ~]# tar -jxvf etc.tar.bz2 #解压
[root@localhost ~]# tar -jxvf etc.tar.bz2 -C /opt/ #指定解压目录
# 创建xz压缩包
[root@localhost ~]# tar -Jcvf etc.tar.xz /etc #压缩
[root@localhost ~]# tar -xvf etc.tar.xz #解压
[root@localhost ~]# tar -Jxvf etc.tar.xz #解压
# 三种压缩方式后压缩比例
[root@localhost ~]# ll -h etc.tar*
-rw-r--r-- 1 root root 28M Aug 18 14:27 etc.tar
-rw-r--r-- 1 root root 8.7M Aug 18 14:34 etc.tar.bz2 #常用
-rw-r--r-- 1 root root 9.8M Aug 18 14:28 etc.tar.gz #常用
-rw-r--r-- 1 root root 7.0M Aug 18 14:36 etc.tar.xz #压缩比例最高,但时间最久
9.2 zip管理压缩文件
zip压缩、解压软件
[root@localhost ~]# zip a.zip /etc/passwd #压缩
adding: etc/passwd (deflated 57%)
[root@localhost ~]# unzip a.zip -d /opt/ 解压缩并指定解压目录
Archive: a.zip
inflating: /opt/etc/passwd
9.3 了解gzip bzip2 xz管理压缩文件 file-sort查看文件
创建压缩的tar存档,tar命令支持三种压缩方式
gzip压缩速度最快
bzip2压缩生成的文件比gzip小,但使用不如gzip广
xz 提供最佳的压缩率
9.3.1 压缩工具
常见压缩格式: .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@localhost ~]# mkdir test1
[root@localhost ~]# touch test1/a.txt
[root@localhost ~]# gzip test1/a.txt # 压缩
[root@localhost ~]# ls test1/
a.txt.gz
# 注:对文件进行压缩,压缩后源文件会消失,一般不用,bzip、xz可以使用-k参数来保留源文件,gzip不可以
[root@localhost ~]# bzip2 -d test1/a.txt.bz2 -k # 保留源文件压缩
[root@localhost ~]# ll test1/
total 4
-rw-r--r-- 1 root root 0 Aug 18 17:56 a.txt
-rw-r--r-- 1 root root 14 Aug 18 17:56 a.txt.bz2
# 解压:
# gzip -d 文件
# bzip2 -d 文件 -k 保留源文件
# xz -d 文件 或 unxz 文件 -k 保留源文件
9.3.2 file查看文件
# 使用方法:file 文件
# 作用:确定文件类型
[root@localhost ~]# file /etc/passwd
/etc/passwd: ASCII text
9.3.3 按规则排序查看文件
# 查看文件
[root@localhost ~]# ls -ltr # 按时间排序 t 表示时间,-r从小到大,不加r参数则为从大到小
[root@localhost ~]# ls -lSr # 按大小排序
[root@localhost ~]# ls -lSrh # 按大小排序,-h 参数,大小体现更明显
[root@localhost ~]# ls -lS # 从大到小
# 查看目录
[root@localhost ~]# du -sh /etc
31M /etc
#查看分区大小
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 47G 1.9G 46G 4% /
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.6M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 1014M 133M 882M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/sr0 4.2G 4.2G 0 100% /mnt
9.3.4 sort命令
# 默认按字母进行排序
[root@localhost ~]# cat /etc/passwd | sort | more
# 按数据排序
[root@localhost ~]# vim file2 # 每行随意写一些数字
[root@localhost ~]# sort -n file2 # -n 默认从小到大
[root@localhost ~]# sort -r file2 # -r 反序排序(升序编程降序进行排序),从大到小
# 支持按月份排序
[root@localhost ~]# vim file3 # 写入一下内容
March
August
April
February
January
[root@localhost ~]# sort -M file3
January
February
March
April
August
# 组合使用
# -t 指定一个用来区分键位置字符
# -k 后面跟数字,指定按第几列进行排序
# -r 反序排序(升序编程降序进行排序)计算机编码排序
# -n 以用户习惯的数字进行排序
# 按 : 做分隔符,以第三列,也就是用用户UID,来从到小排序
[root@localhost ~]# sort -t ':' -k 3 -r /etc/passwd | more
# 把 /etc目录下所有文件,按从大大小排序
[root@localhost ~]# du -h /etc | sort -r | more