1、找出ifconfig命令结果中本机的所有IPv4地址。
[root@liang~]#ifconfig#centos6下 eth0Linkencap:EthernetHWaddr00:0C:29:BA:F9:36 inetaddr:192.168.99.99Bcast:192.168.99.255Mask:255.255.255.0 inet6addr:fe80::20c:29ff:feba:f936/64Scope:Link UPBROADCASTRUNNINGMULTICASTMTU:1500Metric:1 RXpackets:3670errors:0dropped:0overruns:0frame:0 TXpackets:2963errors:0dropped:0overruns:0carrier:0 collisions:0txqueuelen:1000 RXbytes:341489(333.4KiB)TXbytes:354417(346.1KiB) loLinkencap:LocalLoopback inetaddr:127.0.0.1Mask:255.0.0.0 inet6addr:::1/128Scope:Host UPLOOPBACKRUNNINGMTU:65536Metric:1 RXpackets:8errors:0dropped:0overruns:0frame:0 TXpackets:8errors:0dropped:0overruns:0carrier:0 collisions:0txqueuelen:0 RXbytes:600(600.0b)TXbytes:600(600.0b) [root@fengl~]#ifconfig#centos7下 eno16777736:flags=4163<UP,BROADCAST,RUNNING,MULTICAST>mtu1500 inet192.168.99.100netmask255.255.255.0broadcast192.168.99.255 inet6fe80::20c:29ff:fe01:b00cprefixlen64scopeid0x20<link> ether00:0c:29:01:b0:0ctxqueuelen1000(Ethernet) RXpackets2804bytes242061(236.3KiB) RXerrors0dropped0overruns0frame0 TXpackets1855bytes201673(196.9KiB) TXerrors0dropped0overruns0carrier0collisions0 lo:flags=73<UP,LOOPBACK,RUNNING>mtu65536 inet127.0.0.1netmask255.0.0.0 inet6::1prefixlen128scopeid0x10<host> looptxqueuelen0(LocalLoopback) RXpackets852bytes68852(67.2KiB) RXerrors0dropped0overruns0frame0 TXpackets852bytes68852(67.2KiB) TXerrors0dropped0overruns0carrier0collisions0 方法一: [root@liang~]#ifconfig|grepMask|tr-s''|cut-d''-f3|cut-d':'-f2#centos6下 [root@fengl~]#ifconfig|grepnetmask|tr-s''|cut-d''-f3#centos7下 方法二(通用): [root@fengl~]#ifconfig|grep-E-o'(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'
2、查出分区空间使用率的最大百分比值。
[root@liang~]#df Filesystem1K-blocksUsedAvailableUse%Mountedon /dev/sda2411538562146548369101566%/ tmpfs95314009531400%/dev/shm /dev/sda119986724026018535563%/boot [root@liang~]#df|tr-s''|cut-d''-f5|tr-d%|sort-n|tail-1
[root@fengl~]#getentpasswd|sort-t:-k3-n|cut-d:-f1,3,7|tail-1
4、查出/tmp的权限,以数字方式显示
[root@fengl~]#stat/tmp File:‘/tmp’ Size:4096Blocks:8IOBlock:4096directory Device:802h/2050dInode:786433Links:7 Access:(1777/drwxrwxrwt)Uid:(0/root)Gid:(0/root) Context:system_u:object_r:tmp_t:s0 Access:2016-07-1119:00:12.017347722+0800 Modify:2016-08-0818:58:01.445117085+0800 Change:2016-08-0818:58:01.445117085+0800 Birth:- [root@fengl~]#stat/tmp|head-4|tail-1|cut-d'('-f2|cut-d'/'-f1
5、统计当前连接本机的每个远程主机IP的连接数,并按大到小排序。
[root@fengl~]#netstat-nt|greptcp|tr-s''|cut-d''-f5|cut-d:-f1|uniq-c|sort-nr
6、显示/proc/meminfo文件中以大小s开头的行(要求:使用两种方式)
[root@fengl~]#cat/proc/meminfo|grep-i^s#方法1 [root@fengl~]#cat/proc/meminfo|grep-E'^s|^S'#方法2 [root@fengl~]#cat/proc/meminfo|grep'^[sS]'#方法3
7、显示/etc/passwd文件中不以/bin/bash结尾的行
[root@fengl~]#cat/etc/passwd|grep-v'/bin/bash$'
[root@fengl~]#cat/etc/passwd|grep'^rpc\>'|cut-d:-f7
9、找出/etc/passwd中的两位或者三位数
[root@fengl~]#grep-E'\<[1-9][0-9]\>|\<[1-9][0-9]{2}\>'/etc/passwd
10、显示/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面存非空白字符的行
[root@fengl~]#grep'^[[:space:]]\+[^[:space:]]'/etc/grub2.cfg [root@fengl~]#grep-E'^[[:space:]]+[^[:space:]]'/etc/grub2.cfg
11、找出“netstat -tan”命令的结果中以“LISTEN”后跟任意多个空白字符结尾的行
[root@fengl~]#netstat-tan|grep'LISTEN[[:space:]]*$'
12、添加用户bash、testbash、basher以及nologin(其shell为/sbin/nologin),而后找出/etc/passwd文件中用户名同shell名的行
[root@fenglhome]#cat/etc/passwd|grep'^\<\(.*\)\>.*\b\1\b$'
13、显示三个用户root、mage、wang的UID和默认shell
[root@fenglhome]#grep-E'^(root|mage|wang)'/etc/passwd|cut-d:-f3,7
14、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行
[root@fenglhome]#grep'^\<[[:alpha:]_]\+\>()'/etc/rc.d/init.d/functions
15、使用egrep取出/etc/rc.d/init.d/functions中的基名
[root@fenglhome]#echo"/etc/rc.d/init.d/functions"|grep-E-o'[^/]+/?$'
16、使用egrep取出/etc/rc.d/init.d/functions的目录名
[root@fenglhome]#echo"/etc/rc.d/init.d/functions"|grep-E-o'^/.*/'
17、统计以root身份登录的每个远程主机IP地址的登录次数
[root@fengl~]#last|grep-E-o"^root\>.*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))"|tr-s''|cut-d''-f3|uniq-c
18、统计/etc/init.d/functions 文件中每个单词出现的次数,并按频率从高到低显示
[root@liang6/]#cat/etc/init.d/functions|tr-cs'[:alpha:]''\n'|sort|uniq-c|sort-t''-k1-nr
19、从文件haoma里正则表达式表示身份证号
[root@liang6/]#grep-E'\b[1-9][0-9]{5}(18|19|20)[0-9]{2}(0[1-9]|1(0-2))(0[1-9]|[12][0-9]|3[0-1])[0-9]{3}[0-9X]\b'haoma
20、从文件haoma里使用正则表达式表示手机号
[root@liang6/]#grep-E"\b(\+86)?1[38][0-9]{9}|14[57][0-9]{8}|15[0-35-9][0-9]{8}|17[0678][0-9]{8}\b"haoma
21、从文件haoma里正则表达式表示邮箱
[root@liang6/]#grep-E'\b[[:alnum:]][[:alnum:]_-]*@[[:alnum:]][[:alnum:]\._-]*\b'haoma
22、从文件haoma里正则表达式表示QQ号
[root@liang6/]#grep-E'\b[1-9][0-9]{4,11}\b'haoma