Linux的归档和压缩、file和sort命令的使用方法

  • tar命令进行文件的归档和压缩
  • zip管理压缩文件
  • 了解gzip、bzip2、xz管理压缩软件
  • file、sort命令的使用方法

1、tar命令进行文件的归档和压缩
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 指定解压位置
  • -r 追加内容到已经打包的tar包内
  • -P 保留打包时的/目录,不删除/,但是解压的时候会自动删除/目录

例1:给/boot/grub目录打包和解压缩

[root@xuegod130 ~]# tar -cvf grub.tar /boot/grub #参数前可不加“-”
tar: 从成员名中删除开头的“/”
/boot/grub/
/boot/grub/splash.xpm.gz
[root@xuegod130 ~]# ls grub.tar
grub.tar
[root@xuegod130 ~]# tar -xvf grub.tar -C /opt/ #-C 可以指定解压缩的目录
boot/grub/
boot/grub/splash.xpm.gz
[root@xuegod130 ~]# ls /opt/boot/grub
splash.xpm.gz

注意:
在使用绝对路径归档文件时,将默认从文件名中删除该路径中前面的 / 符号。这样解压时,会直接解压到当前目录。 如果不移除/压缩时,当解包时,直接按绝对路径来释放,会覆盖原系统中此路径的文件。
例2:把两个目录或目录+文件打包
#参数后跟上打包后的名称,后面直接跟上多个目录或文件名,中间用空格隔开

[root@xuegod130 ~]# mkdir back
[root@xuegod130 ~]# cp /etc/passwd back/
[root@xuegod130 ~]# tar -cvf back.tar /boot/grub back/ /etc/passwd #打包多个目录或文件为back.tar文件
tar: 从成员名中删除开头的“/”
/boot/grub/
/boot/grub/splash.xpm.gz
back/
back/passwd
/etc/passwd
[root@xuegod130 ~]# ls back.tar
back.tar
[root@xuegod130 ~]# tar -xvf back.tar -C /opt/ #解压缩文件back.tar到/opt目录下
boot/grub/
boot/grub/splash.xpm.gz
back/
back/passwd
etc/passwd
[root@xuegod130 ~]# ls /opt/
back boot etc rh

例3:不解包,直接查看tar中的内容

[root@xuegod130 ~]# ls back.tar
back.tar
[root@xuegod130 ~]# tar -tvf back.tar #加上-t参数,可以不解压,直接查看tar包中的内容
drwxr-xr-x root/root 0 2019-02-20 21:25 boot/grub/
-rw-r–r-- root/root 1350 2011-11-16 05:03 boot/grub/splash.xpm.gz
drwxr-xr-x root/root 0 2019-03-05 21:53 back/
-rw-r–r-- root/root 2239 2019-03-05 21:53 back/passwd
-rw-r–r-- root/root 2239 2019-02-20 22:35 etc/passwd

1.2 tar 归档和压缩
语法:tar -czvf newfile.tar.gz source
参数:

  • -z 以gzip方式压缩 扩展名:tar.gz
  • -j 以bz2方式压缩 扩展名:tar.bz2
  • -J 以xz方式压缩 扩展名:tar.xz

例1:创建tar.gz压缩包

[root@xuegod130 ~]# tar -zcvf etc.tar.gz /etc #-z参数压缩为gzip格式
[root@xuegod130 ~]# ls etc.tar.gz
etc.tar.gz
[root@xuegod130 ~]# tar -zxvf etc.tar.gz -C /opt #-x解压缩;-C 指定解压目录

例2:创建tar.bz2包

[root@xuegod130 ~]# tar -jcvf etc.tar.bz2 /etc #-j参数压缩为bz2格式
[root@xuegod130 ~]# ll etc.tar.*
-rw-r–r-- 1 root root 9346407 3月 5 22:12 etc.tar.bz2
-rw-r–r-- 1 root root 10752251 3月 5 22:04 etc.tar.gz
[root@xuegod130 ~]# tar -jxvf etc.tar.bz2 -C ./ #解压到当前目录下

例3:创建tar.xz包

[root@xuegod130 ~]# tar -Jcvf etc.tar.xz /etc #-J参数压缩为xz格式
[root@xuegod130 ~]# ls etc.tar.*
etc.tar.bz2 etc.tar.gz etc.tar.xz
[root@xuegod130 ~]# tar -Jxvf etc.tar.xz -C /opt/ #解压到/opt目录下
[root@xuegod130 ~]# ls /opt/
back boot etc rh

例4:对比三种压缩方式后压缩比例

[root@xuegod130 ~]# ll -h etc.tar*
-rw-r–r-- 1 root root 32M 3月 5 22:18 etc.tar
-rw-r–r-- 1 root root 9.0M 3月 5 22:12 etc.tar.bz2
-rw-r–r-- 1 root root 11M 3月 5 22:04 etc.tar.gz
-rw-r–r-- 1 root root 7.3M 3月 5 22:15 etc.tar.xz

总结:三种压缩比例xz格式的压缩比例最高,但是耗时比较旧;gzip和bz2这两种压缩比例差不多,耗时也差不多。
日常中gzip和bz2这两种压缩方式是比较常用的

例5:将b.txt追加到a.txt.tar包

[root@xuegod140 opt]# ls
1.txt 2.txt 3.txt 4.txt 5.txt
[root@xuegod140 opt]# tar cvf a.txt.tar 1.txt 2.txt 3.txt 4.txt
1.txt
2.txt
3.txt
4.txt
[root@xuegod140 opt]# tar tvf a.txt.tar
-rw-r–r-- root/root 239 2019-03-30 21:40 1.txt
-rw-r–r-- root/root 0 2019-03-27 21:31 2.txt
-rw-r–r-- root/root 0 2019-03-27 21:31 3.txt
-rw-r–r-- root/root 0 2019-03-27 21:31 4.txt
[root@xuegod140 opt]# tar -rf a.txt.tar 5.txt
[root@xuegod140 opt]# tar tvf a.txt.tar
-rw-r–r-- root/root 239 2019-03-30 21:40 1.txt
-rw-r–r-- root/root 0 2019-03-27 21:31 2.txt
-rw-r–r-- root/root 0 2019-03-27 21:31 3.txt
-rw-r–r-- root/root 0 2019-03-27 21:31 4.txt
-rw-r–r-- root/root 0 2019-03-27 21:31 5.txt

例6:打包时保留/目录,不删除

[root@xuegod140 opt]# tar -Pcvf c.tar /etc/passwd /etc/hosts #保留/目录,一起打包
/etc/passwd
/etc/hosts
[root@xuegod140 opt]# tar -tvf c.tar
tar: Removing leading /from member names
-rw-r–r-- root/root 2325 2019-03-30 19:05 /etc/passwd
-rw-r–r-- root/root 158 2013-06-07 22:31 /etc/hosts
[root@xuegod140 opt]# tar -cvf d.tar /etc/passwd /etc/hosts #删除/目录,不进行打包
tar: Removing leading `/’ from member names
/etc/passwd
/etc/hosts
[root@xuegod140 opt]# tar -tvf d.tar
-rw-r–r-- root/root 2325 2019-03-30 19:05 etc/passwd
-rw-r–r-- root/root 158 2013-06-07 22:31 etc/hosts

例7:排除一个文件,不打包到tar包内,追加一个文件重复打包

[root@xuegod140 opt]# tar cvf a.txt.tar *.txt --exclude=3.txt 1.txt
1.txt
2.txt
4.txt
5.txt
1.txt
[root@xuegod140 opt]# tar tvf a.txt.tar
-rw-r–r-- root/root 239 2019-03-30 21:40 1.txt
-rw-r–r-- root/root 0 2019-03-27 21:31 2.txt
-rw-r–r-- root/root 0 2019-03-27 21:31 4.txt
-rw-r–r-- root/root 0 2019-03-27 21:31 5.txt
hrw-r–r-- root/root 0 2019-03-30 21:40 1.txt link to 1.txt

2、 zip管理压缩文件
zip软件包解压缩命令:
zip是压缩程序,unzip是解压缩程序。
例1:压缩文件

[root@xuegod130 ~]# zip a.zip /etc/passwd #命令后直接跟上压缩后的文件名,后跟需要压缩的文件或者目录
adding: etc/passwd (deflated 61%)
[root@xuegod130 ~]# ll -h a.zip /etc/passwd
-rw-r–r-- 1 root root 1.1K 3月 5 22:22 a.zip
-rw-r–r-- 1 root root 2.2K 2月 20 22:35 /etc/passwd

例2:将所有的.jpg的文件压缩成一个zip包

[root@xuegod130 ~]# zip all.zip *.jpg #所有的jpg文件需要在当前目录下,或者后面跟上文件的位置

例3:压缩一个目录

[root@xuegod130 ~]# zip -r grup.zip /boot/grub #-r参数可以压缩目录
adding: boot/grub/ (stored 0%)
adding: boot/grub/splash.xpm.gz (deflated 87%)
[root@xuegod130 ~]# ll -h grup.zip /boot/grub
-rw-r–r-- 1 root root 522 3月 5 22:26 grup.zip
/boot/grub:
总用量 4.0K
-rw-r–r--. 1 root root 1.4K 11月 16 2011 splash.xpm.gz

例4:解压缩文件
-d 可以指定解压缩后的目录

[root@xuegod130 ~]# ls *.zip
a.zip grup.zip
[root@xuegod130 ~]# unzip a.zip
[root@xuegod130 ~]# tree etc #如果没有tree命令,直接使用yum -y install tree进行安装
etc
└── passwd
0 directories, 1 file
[root@xuegod130 ~]# unzip grup.zip -d /opt/ #-d 参数指定解压缩后存的目录
Archive: grup.zip
creating: /opt/boot/grub/
inflating: /opt/boot/grub/splash.xpm.gz
[root@xuegod130 ~]# tree /opt/
/opt/
└── boot
└── grub
└── splash.xpm.gz
2 directories, 1 file

3、了解gzip-bzip2- xz管理压缩文件
TAR命令它支持三种不同的压缩方式。

  • gzip压缩速度最快;
  • bzip2压缩生成的文件比gzip小,但使用不如gzip广;
  • xz压缩工具相对较新,但是会提供最佳的压缩率
    压缩命令
    压缩工具:gzip bzip2 zip xz
    常见的压缩格式: .gz .bz2 .xz .zip

例1:gzip压缩

[root@xuegod130 ~]# mkdir xuegod
[root@xuegod130 ~]# touch xuegod/a.txt
[root@xuegod130 ~]# gzip xuegod/a.txt #gzip压缩会直接删除原文件
[root@xuegod130 ~]# ls xuegod/a.txt.gz
xuegod/a.txt.gz
[root@xuegod130 ~]# ls xuegod/a.txt
ls: 无法访问xuegod/a.txt: 没有那个文件或目录

例2:bzip2和xz压缩
参数:-k 压缩后保留源文件

[root@xuegod130 ~]# bzip2 -k xuegod/a.txt
[root@xuegod130 ~]# ls xuegod/a.txt
xuegod/a.txt
[root@xuegod130 ~]# ls xuegod/
a.txt a.txt.bz2
[root@xuegod130 ~]# xz -k xuegod/a.txt
[root@xuegod130 ~]# ls xuegod/
a.txt a.txt.bz2 a.txt.xz

例4:gzip、bzip2、xz解压缩

[root@xuegod130 ~]# gzip -d xuegod/a.txt.gz
[root@xuegod130 ~]# gzip -d ^Cegod/a.txt.gz
[root@xuegod130 ~]# bzip2 -d xuegod/a.txt.bz2
bzip2: Output file xuegod/a.txt already exists.
[root@xuegod130 ~]# xz -d xuegod/a.txt.xz
[root@xuegod130 ~]# unxz xuegod/a.txt.xz #xz和unxz作用相同

4、file、sort命令的使用方法
4.1 file查看文件
flie命令
作用:查看文件类型
用法:flie /etc/passwd
注:Linux系统不根据后缀名识别文件类型

[root@xuegod130 ~]# file /etc/passwd
/etc/passwd: ASCII text

4.2 按一定规则排序查看文件和目录
参数:

  • -t 按照时间排序
  • -r 按照从小到达排序,不加r由大到小排序
  • -S 按照数据大小进行排序,加r按照从小到达排序
  • -h 文件数据大小显示单位,方面计算,比如1.5k、2M

例1:按照时间排序,从小到大显示文件

[root@xuegod130 ~]# ls -ltr
总用量 8
-rw-------. 1 root root 1929 2月 20 21:42 anaconda-ks.cfg
-rw-r–r--. 1 root root 1977 2月 20 21:52 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 2月 20 21:53 桌面
drwxr-xr-x. 2 root root 6 2月 20 21:53 音乐
drwxr-xr-x. 2 root root 6 2月 20 21:53 下载
drwxr-xr-x. 2 root root 6 2月 20 21:53 文档
drwxr-xr-x. 2 root root 6 2月 20 21:53 图片
drwxr-xr-x. 2 root root 6 2月 20 21:53 模板
drwxr-xr-x. 2 root root 6 2月 20 21:53 公共
drwxr-xr-x. 2 root root 6 2月 20 21:53 视频

例2:按照数据大小排序,从小到大显示文件

[root@xuegod130 ~]# ls -lSr
总用量 8
drwxr-xr-x. 2 root root 6 2月 20 21:53 桌面
drwxr-xr-x. 2 root root 6 2月 20 21:53 音乐
drwxr-xr-x. 2 root root 6 2月 20 21:53 下载
drwxr-xr-x. 2 root root 6 2月 20 21:53 文档
drwxr-xr-x. 2 root root 6 2月 20 21:53 图片
drwxr-xr-x. 2 root root 6 2月 20 21:53 视频
drwxr-xr-x. 2 root root 6 2月 20 21:53 模板
drwxr-xr-x. 2 root root 6 2月 20 21:53 公共
-rw-------. 1 root root 1929 2月 20 21:42 anaconda-ks.cfg
-rw-r–r--. 1 root root 1977 2月 20 21:52 initial-setup-ks.cfg

例3:按照数据大小排序,从小到大,显示数据单位

[root@xuegod130 ~]# ls -lSrh
总用量 8.0K
drwxr-xr-x. 2 root root 6 2月 20 21:53 桌面
drwxr-xr-x. 2 root root 6 2月 20 21:53 音乐
drwxr-xr-x. 2 root root 6 2月 20 21:53 下载
drwxr-xr-x. 2 root root 6 2月 20 21:53 文档
drwxr-xr-x. 2 root root 6 2月 20 21:53 图片
drwxr-xr-x. 2 root root 6 2月 20 21:53 视频
drwxr-xr-x. 2 root root 6 2月 20 21:53 模板
drwxr-xr-x. 2 root root 6 2月 20 21:53 公共
-rw-------. 1 root root 1.9K 2月 20 21:42 anaconda-ks.cfg
-rw-r–r--. 1 root root 2.0K 2月 20 21:52 initial-setup-ks.cfg

例4:查看目录和磁盘大小

[root@xuegod130 ~]# du -sh /etc
37M /etc
[root@xuegod130 ~]# df -Th
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/sda3 xfs 10G 3.8G 6.3G 38% /
devtmpfs devtmpfs 896M 0 896M 0% /dev
tmpfs tmpfs 911M 0 911M 0% /dev/shm
tmpfs tmpfs 911M 11M 901M 2% /run
tmpfs tmpfs 911M 0 911M 0% /sys/fs/cgroup
/dev/sda1 xfs 197M 143M 55M 73% /boot
tmpfs tmpfs 183M 36K 183M 1% /run/user/0
/dev/sr0 iso9660 4.2G 4.2G 0 100% /mnt.

4.2 sort命令用法
例1:默认按照字母规则进行排序

[root@xuegod130 ~]# cat /etc/passwd | sort | more
在这里插入图片描述

例2:按照数字大小,默认从小到大显示

[root@xuegod130 ~]# vim a.txt
在这里插入图片描述
[root@xuegod130 ~]# cat a.txt | sort
[root@xuegod130 ~]# sort a.txt
[root@xuegod130 ~]# sort -r a.txt #-r 从小到大排序,不加默认从大到小
在这里插入图片描述

例3:支持按照月份排序

[root@xuegod130 ~]# vim file3
在这里插入图片描述
[root@xuegod130 ~]# sort -M file3
April
February
January
March

例4:组合使用
参数:
-t 指定一个用来区分键位置字符
-k 后面跟数字,指定第几列进行排序
-r 反序排序(升序变降序排序)

[root@xuegod130 ~]# sort -t “:” -k3 -r /etc/passwd #按:作为分隔符,以第3列用户UID来从大到小排序
在这里插入图片描述
[root@xuegod130 ~]# du -h /etc | sort -r | more # 查看/etc目录下文件的大小,并以从大到小的方式输出到more里面显示
在这里插入图片描述

例5:以第3列为主,第4列为辅,进行排序

[root@xuegod140 opt]# cat 2.txt
1:2:3:4:5
2:3:4:5:6
4:6:6:6:3
5:6:3:6:7
9:8:5:5:3
1:2:5:7:3
[root@xuegod140 opt]# sort -t “:” -k3 -k4 -n 2.txt
1:2:3:4:5
5:6:3:6:7
2:3:4:5:6
9:8:5:5:3
1:2:5:7:3
4:6:6:6:3
[root@xuegod140 opt]# sort -t “:” -k3 -n 2.txt
1:2:3:4:5
5:6:3:6:7
2:3:4:5:6
1:2:5:7:3
9:8:5:5:3
4:6:6:6:3

例6:查看etc目录下从大到小进行排序

[root@xuegod140 ~]# du -h /etc/ | sort -r | more
8.9M /etc/selinux/targeted/active/modules/100
8.9M /etc/selinux/targeted/active/modules
88K /etc/profile.d
84K /etc/selinux/targeted/active/modules/100/sysadm
84K /etc/libreport/events.d

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值