1、顯示/proc/meminfo文件中以大小s開頭的行; (要求:使用三種方式)
[root@wen-7 ~]# grep -i "^s" /proc/meminfo SwapCached: 40 kB SwapTotal: 2097148 kB SwapFree: 2096840 kB [root@wen-7 ~]# egrep "^(s|S)" /proc/meminfo SwapCached: 40 kB SwapTotal: 2097148 kB SwapFree: 2096840 kB [root@wen-7 ~]# egrep "^[sS]" /proc/meminfo SwapCached: 40 kB SwapTotal: 2097148 kB SwapFree: 2096840 kB
2、顯示/etc/passwd文件中不以/bin/bash結尾的行
[root@wen-7 ~]# grep -v "bash$" /etc/passwd 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
3、顯示用戶rpc默認的shell程序
[root@wen-7 ~]# getent passwd rpc| egrep -o "[^:]+$" /sbin/nologin
4、找出/etc/passwd中的兩位或三位數
[root@wen-7 ~]# cat /etc/passwd| grep "\<[[:digit:]]\{2,3\}\>" root:x:0:0:root,123,123,12345:/root:/bin/bash 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
5、顯示/etc/grub2.cfg文件中,至少以一個空白字符開頭的且后面存非空白字符的行
[root@wen-7 ~]# cat /etc/grub2.cfg | grep "^[[:space:]]\+[^[:space:]]" load_env set default="${next_entry}" set next_entry= save_env next_entry
6、找出"netstat -tan"命令的結果中以'LISTEN'后跟0、 1 或多個空白字符結尾的行
[root@wen-7 ~]# netstat -tan| grep "LISTEN[[:space:]]*" tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
7、添加用戶bash、 testbash、 basher以及nologin(其shell為/sbin/nologin),而后找出/etc/passwd文件中用戶名同shell名的行
[root@wen-7 ~]# cat /etc/passwd | egrep "^([^:]+\>).*\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 [root@wen-7 ~]# cat /etc/passwd | grep "\<\(.*\)\>.*/\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 [root@wen-7 ~]# cat /etc/passwd | egrep "^(.*)?:.*\1$" sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
9、顯示當前系統root、 mage或wang用戶的UID和默認shell
[root@wen-7 ~]# cat /etc/passwd |grep -E "(^root|wang|mage)\b"| cut -d: -f 1,3,7 root:0:/bin/bash mageia:1100:/bin/bash rooter:0:/bin/bash wangcai:6011:/bin/bash
10、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包 括下劃線)后面跟一個小括號的行
[root@wen-7 ~]# cat /etc/rc.d/init.d/functions | egrep "[[:alnum:]_]+\(\)" checkpid() { __pids_var_run() { __pids_pidof() {
11、使用egrep取出/etc/rc.d/init.d/functions中其基名
[root@wen-7 ~]# echo "/etc/rc.d/init.d/functions" | grep -so "[^/]*.$" functions
12、使用egrep取出上面路徑的目錄名
[root@wen-7 ~]# echo "/etc/rc.d/init.d/functions" | egrep -s "[^/]*./" /etc/rc.d/init.d/functions
13、統計以root身份登錄的每個遠程主機IP地址的登錄次數
[root@wen-7 ~]# last| egrep -o "^root\>.*"|tr -s " -r"|cut -d" " -f3| sort -n | egrep "([0-9]){2,5}"| uniq -c 13 10.1.250.47 83 172.18.19.1
14、利用擴展正則表達式分別表示0-9、 10-99、 100-199、200-249、 250-255
[0-9][1-9][0-9] 1[0-9][0-9] 2[0-9][0-9] 25[0-5]
15、顯示ifconfig命令結果中所有IPv4地址
[root@wen-7 ~]# ifconfig | grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" 172.18.19.219 255.255.255.0 172.18.19.255 [root@wen-7 ~]# ifconfig | egrep -o '([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9]{2} |2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\. ([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])' 172.18.19.219 255.255.255.0 172.18.19.255 127.0.0.1
16、取本機ip地址
[root@wen-7 ~]# ifconfig eno16777736| grep "inet\>"|tr -s "[[:space:]]" ":"| cut -d: -f3
172.18.19.219
17、取各分區利用率的數值
[root@wen-7 ~]# df -h|grep "^/dev/"| tr -s " " ":"| cut -d: -f1,5 /dev/mapper/centos-root:29% /dev/sda1:32% /dev/sr0:100%
18、統計/etc/init.d/functions 文件中每個單詞出現的次數,并按頻率從高到低顯示
[root@wen-7 ~]# cat /etc/rc.d/init.d/functions |tr -sc "(^[[:alpha:]].*)" "\n"| sort|uniq -c| sort -n 50 echo 52 file 53 if 59 pid
19、正則表達式表示身份證號
[root@wen-7 ~]# echo "13040619950709187x"| egrep -o "[1-9][0-9]{14}|[1-9][0-9]{17}|[1-9][0-9]{16}([0-9]|x)" 13040619950709187x [root@wen-7 ~]# echo "130406199507091870"| egrep -o "[1-9][0-9]{14}|[1-9][0-9]{17}|[1-9][0-9]{16}([0-9]|x)" 130406199507091870 [root@wen-7 ~]# echo "130406199507091870"| egrep -o "[1-9][0-9]{14}|[1-9][0-9]{17}|[1-9][0-9]{16}([0-9]|x)"
20、正則表達式表示手機號
[root@wen-7 ~]# echo "15010570755" | grep "1[0-9]\{10\}" 15010570755
21、正則表達式表示郵箱
[root@wen-7 ~]# echo "1434421421qqsasf@wew.com" | grep "[[:alnum:]].*\@[[:alnum:]].*\.[[:alnum:]].*" 1434421421qqsasf@wew.com [root@wen-7 ~]# echo "143294343432QQ@163.com" | grep "[[:alnum:]].*\@[[:alnum:]].*\.[[:alnum:]].*" 143294343432QQ@163.com
22、正則表達式表示QQ號
[root@wen-7 ~]# echo "43243243243" | grep "[0-9]\{5,11\}" 43243243243
23、查出分區空間使用率的最大百分比值
[root@wen-7 ~]# df -h | tr -s "[:space:]"| cut -d' ' -f1,5| grep "/dev" |sort -n| tail -n1 /dev/sr0 100%
24、查出用戶UID最大值的用戶名、 UID及shell類型
[root@wen-7 ~]# getent passwd| sort -n -t: -k3|tail -n1| cut -d: -f1,3,7 nfsnobody:65534:/sbin/nologin
25、查出/tmp的權限,以數字方式顯示
[root@wen-7 ~]# stat -c %a /tmp/ 777 [root@wen-7 ~]# stat /tmp/| head -4| tail -1| cut -d" " -f1 | grep -o [0-9]|tr "\n" " " 0 7 7 7
26、統計當前連接本機的每個遠程主機IP的連接數,并按從大 到小排序
[root@wen-7 ~]# nets6t -nt|tr -s " "| cut -d" " -f5|grep "[0-9]"| cut -d: -f1| uniq -c| sort 1 172.18.19.1
原創文章,作者:wencx,如若轉載,請注明出處:http://www.www58058.com/30145