文本处理工具
找出ifconfig “网卡名” 命令结果中本机的IPv4地址
[21:11:32 root@centos7 data]# ifconfig|head -2|tail -n1|tr -s " "|cut -d" " -f3
10.0.0.154
查出分区空间使用率的最大百分比值
[21:14:00 root@centos7 data]# df|tail -n +2|tr -s " " "%"|cut -d% -f5|sort -n|tail -n1
15
查出用户UID最大值的用户名、UID及shell类型
[21:17:18 root@centos7 data]# getent passwd| sort -t: -k3 -nr|head -n1|cut -d: -f1,3,7
xiaoming:1001:/bin/bash
查出/tmp的权限,以数字方式显示
[21:26:14 root@centos7 data]# stat /tmp|head -n 4|tail -n 1|cut -c10-13
1777
统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序
[21:34:30 root@centos7 data]# ss -nt|tr -s " " ":"|cut -d: -f6|tail -n +2|sort|uniq -c| sort -nr
2 10.0.0.150
1 10.0.0.151
1 10.0.0.1
正则表达式
显示/proc/meminfo文件中以大小s开头的行(要求:使用两种方法)
[21:39:26 root@centos7 data]# cat /proc/meminfo | grep -E "^[Ss].*"
SwapCached: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Shmem: 9736 kB
Slab: 65244 kB
SReclaimable: 28272 kB
SUnreclaim: 36972 kB
[21:39:30 root@centos7 data]#
显示/etc/passwd文件中不以/bin/bash结尾的行
[01:21:42 root@localhost ~]# getent passwd|grep -vE ".*/bin/bash$"
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
[01:21:59 root@localhost ~]#
显示用户rpc默认的shell程序
[01:28:12 root@localhost ~]# getent passwd rpc|grep -Eo "[[:alpha:]/]+$"
/bin/bash
[01:28:22 root@localhost ~]#
找出/etc/passwd中的两位或三位数
[01:30:16 root@localhost ~]# getent passwd| grep -Ewo "[[:digit:]]{2,3}"
12
11
12
100
14
50
99
99
192
192
81
81
999
998
74
74
89
89
998
996
[01:30:19 root@localhost ~]#
显示CentOS7的/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面有非空白字符的行
[01:43:13 root@localhost ~]# cat /etc/grub2.cfg | grep -Eo "^[[:space:]]+[^[:space:]]+.*"
[01:43:35 root@localhost ~]#
找出“netstat -tan”命令结果中以LISTEN后跟任意多个空白字符结尾的行
[02:17:47 root@localhost ~]# netstat -tan|grep "LISTEN[[:space:]]*$"
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN
[02:21:58 root@localhost ~]#
显示CentOS7上所有UID小于1000以内的用户名和UID
[02:30:00 root@localhost ~]# getent passwd|grep -E "([^:]+:){2}\<[0-9]{1,3}\>"|cut -d: -f1,3
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
shutdown:6
halt:7
mail:8
operator:11
games:12
ftp:14
nobody:99
systemd-network:192
dbus:81
polkitd:999
sshd:74
postfix:89
chrony:998
添加用户bash、testbash、basher、sh、nologin(其shell为/sbin/nologin),找出/etc/passwd用户名和shell同名的行
[02:44:10 root@localhost ~]# getent passwd | grep -E "^([^:]+):.*\1$"
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bash:x:1002:1002::/home/bash:/bin/bash
[02:44:19 root@localhost ~]#
利用df和grep,取出磁盘各分区利用率,并从大到小排序
[02:48:39 root@localhost ~]# df|grep -Eo "[0-9]+%" |sort -nr
12%
2%
1%
1%
0%
0%
0%
0%
[02:48:53 root@localhost ~]#