五 文件管理之:字符处理命令
5.1 sort命令
用于将文件内容加以排序
-n # 依照数值的大小排序
-r # 以相反的顺序来排序
-k # 以某列进行排序
-t # 指定分割符,默认是以空格为分隔符
准备文件,写入一段无序的内容
[root@localhost ~]# cat >> file.txt <<EOF
b:3
c:2
a:4
e:5
d:1
f:11
EOF
例1
[root@localhost ~]# sort file.txt
a:4
b:3
c:2
d:1
e:5
f:11
例2
[root@localhost ~]# sort -t ":" -n -k2 file.txt
d:1
c:2
b:3
a:4
e:5
f:11
例3
[root@localhost ~]# sort -t ":" -n -r -k2 file.txt
f:11
e:5
a:4
b:3
c:2
d:1
5.2 uniq 命令
用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用
-c # 在每列旁边显示该行重复出现的次数。
-d # 仅显示重复出现的行列。
-u # 仅显示出一次的行列。
准备文件,写入一段无序的内容
[root@localhost ~]# cat > file.txt <<EOF
hello
123
hello
123
func
EOF
例1
[root@localhost ~]# sort file.txt
123
123
func
hello
hello
例2
[root@localhost ~]# sort file.txt | uniq
123
func
hello
例3
[root@localhost ~]# sort file.txt | uniq -c
2 123
1 func
2 hello
例4
[root@localhost ~]# sort file.txt | uniq -d
123
hello
5.3 cut 命令
cut命令用来显示行中的指定部分,删除文件中指定字段
-d # 指定字段的分隔符,默认的字段分隔符为"TAB";
-f # 显示指定字段的内容;
[root@localhost ~]# head -1 /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]# head -1 /etc/passwd | cut -d ":" -f1,3,4,6
root:0:0:/root
5.4 tr命令
替换或删除命令
-d # 删除字符
例1
[root@localhost ~]# head -1 /etc/passwd |tr "root" "ROOT"
ROOT:x:0:0:ROOT:/ROOT:/bin/bash
[root@localhost ~]#
[root@localhost ~]# head -1 /etc/passwd |tr -d "root"
:x:0:0::/:/bin/bash
例2
[root@localhost ~]# echo "hello egon qq:378533872" > a.txt
[root@localhost ~]# tr "egon" "EGON" < a.txt
hEllO EGON qq:378533872
5.5 wc命令
统计,计算数字
-c # 统计文件的Bytes数;
-l # 统计文件的行数;
-w # 统计文件中单词的个数,默认以空白字符做为分隔符
例1
[root@localhost ~]# ll file.txt
-rw-r--r--. 1 root root 25 8月 12 20:09 file.txt
[root@localhost ~]# wc -c file.txt
25 file.txt
例2
[root@localhost ~]# cat file.txt
hello
123
hello
123
func
[root@localhost ~]# wc -l file.txt
5 file.txt
[root@localhost ~]# grep "hello" file.txt |wc -l
2
例3
[root@localhost ~]# cat file.txt
hello
123
hello
123
func
[root@localhost ~]# wc -w file.txt
5 file.txt
六 文件管理之:打包压缩
1. 什么是打包压缩
打包指的是将多个文件和目录合并为一个特殊文件
然后将该特殊文件进行压缩
最终得到一个压缩包
2. 为什么使用压缩包
1.减少占用的体积
2.加快网络的传输
3. Windows的压缩和Linux的有什么不同
windows: zip rar(linux不支持)
linux: zip tar.gz tar.bz2 .gz
如果希望windows的软件能被linux解压,或者linux的软件包被windows能识别,选择zip.
PS: 压缩包的后缀不重要,但一定要携带.
4. Linux下常见的压缩包类型
格式 压缩工具
.zip zip压缩工具
.gz gzip压缩工具,只能压缩文件,会删除源文件(通常配合tar使用)
.bz2 bzip2压缩工具,只能压缩文件,会删除源文件(通常配合tar使用)
.tar.gz 先使用tar命令归档打包,然后使用gzip压缩
.tar.bz2 先使用tar命令归档打包,然后使用bzip压缩
ps:windows下支持.rar,linux不支持.rar
5. 打包压缩方法
方法一:
# 1、打包
[root@localhost test]# tar cvf etc_bak.tar /etc/ # c创建 v详细 f打包后文件路径
ps:
打包的目标路径如果是绝对路径,会提示:tar: 从成员名中删除开头的“/”,不影响打包,
添加-P选项便不再提示:tar cvPf ...
可以cd 到 /etc下然后tar cvf etc_bak.tar *打包,这样去掉了一层文件夹
# 2、压缩
[root@localhost test]# gzip etc_bak.tar # 文件体积变小,并且加上后缀.gz
ps:
gzip -> gunzip
bzip2-> bunzip2
# 3、上述两步可以合二为一
[root@localhost test]# tar czvf etc1_bak.tar.gz /etc/ # 选项z代表gzip压缩算法
[root@localhost test]# tar cjvf etc1_bak.tar.bz2 /etc/ # 选项j代表bzip2压缩算法
方法二:
#zip压缩
选项:
-r #递归压缩 压缩目录
-q #静默输出
# 示例1、
[root@localhost ~]# zip /test/bak.zip a.txt b.txt c.txt # zip后的第一个参数是压缩包
路径,其余为被压缩的文件
adding: a.txt (stored 0%)
adding: b.txt (stored 0%)
adding: c.txt (stored 0%)
[root@localhost ~]# ls /test/
bak.zip
# 示例1、
[root@localhost ~]# zip -rq etc.zip /etc # 加上-q后压缩过程不再提示
6. 解压缩
#1、针对xxx.tar.gz 或者 xxx.tar.bz2,统一使用
[root@localhost test]# tar xvf 压缩包 -C 解压到的目录 # 无需指定解压算法,tar会自动判断
#2、针对xxx.zip,用unzip
选项:
-l #显示压缩包的列表信息
-q #静默输出
-d #解压到指定的目录
[root@localhost test]# unzip -q xxx.zip -d /opt
7. 拓展
打包压缩通常用于备份文件,文件的名字必须见名知意且应该带上时间、主机名之类
时间命令date
#选项
-d #根据你的描述显示日期
-s #修改日期
%H #小时,24小时制(00~23)
%M #分钟(00~59)
%s #从1970年1月1日00:00:00到目前经历的秒数
%S #显示秒(00~59)
%T #显示时间,24小时制(hh:mm:ss)
%d #一个月的第几天(01~31)
%j #一年的第几天(001~366)
%m #月份(01~12)
%w #一个星期的第几天(0代表星期天)
%W #一年的第几个星期(00~53,星期一为第一天)
%y #年的最后两个数字(1999则是99)
%Y #年,实际
%F #显示日期(%Y-%m-%d)
示例
[root@localhost ~]# date
2020年 08月 12日 星期三 20:55:48 CST
[root@localhost ~]# date +%F
2020-08-12
[root@localhost ~]# date +%Y-%m-%d
2020-08-12
[root@localhost ~]#
[root@localhost ~]# date +%y-%m-%d
20-08-12
[root@localhost ~]# date +%T
00:01:03
[root@localhost ~]# date +%H:%M:%S
00:01:11
[root@localhost ~]#
[root@localhost ~]# date +%w
3
[root@localhost ~]# date +%s
1597236988
[root@localhost ~]# date +%d
12
[root@localhost ~]# date +%W
32
[root@localhost ~]# date +%j
225
[root@localhost ~]# date -d "-1 day" +%F
2020-08-11
[root@localhost ~]# date -d "1 day" +%F