1、取本機ip地址
[root@ali ~]# ifconfig |egrep 'Bcast' |tr -s ' ' '\n' |head -n3 |tail -n1 |cut -d: -f2
2、取各分區利用率的數值
[root@ali ~]# df |egrep '^/dev/sd'
3、統計/etc/init.d/functions 文件中每個單詞出現的次數,并按頻率從高到低
[root@ali ~]# cat /etc/rc.d/init.d/functions |tr -cs '[:alpha:]' '\n' |sort |uniq -c |sort -nr
4、/etc/rc.d/init.d/functions或/etc/rc.d/init.d/functions/" 取目錄名
[root@ali ~]# echo "/etc/rc.d/init.d/functions/" |grep -o "[/]\?.*[/]"
5、正則表達式表示身份證號
'[0-9]{15,18}' 134.txt
6、正則表達式表示手機號
^[1][0-9][0-9]{9}$
7、正則表達式表示郵箱
^[0-9]{9}@[[:alpha:]].*[[:alpha:]]$
8、正則表達式表示QQ號
[0-9]{9}
9、顯示/proc/meminfo文件中以大小s開頭的行; (要求:使用兩種方式)
[root@ali ~]# cat /proc/meminfo |grep -i '^[s]'
[root@ali ~]# cat /proc/meminfo |grep '^[s|S]'
10、顯示/etc/passwd文件中不以/bin/bash結尾的行
[root@ali ~]# cat /etc/passwd |grep -v '/bin/bash'
11、顯示用戶rpc默認的shell程序
[root@ali ~]# cat /etc/passwd |egrep '\<rpc\>' |cut -d: -f7
12、找出/etc/passwd中的兩位或三位數
[root@ali ~]# cat /etc/passwd |grep '\<[1-9][0-9][0-9]\?\>'
[root@ali ~]# cat /etc/passwd |grep '\<[0-9]\{2,3\}\>'
13、顯示/etc/grub2.cfg文件中,至少以一個空白字符開頭的且后面存非空白字符的行
[root@ali ~]# grep '^[[:space:]]*[^[:space:]]' /etc/rc.d/init.d/functions
14、 找出“netstat -tan”命令的結果中以‘LISTEN’后跟任意多個空白字符結尾的行
[root@ali ~]# netstat -tan |grep 'LISTEN[[:space:]]*$'
15、添加用戶bash、 testbash、 basher以及nologin(其shell為/sbin/nologin),而后找
出/etc/passwd文件中用戶名同shell名的行
[root@ali ~]# cat /etc/passwd |grep '^\(.*\).*/\1$'
[root@ali ~]# grep '^\(.*\).*/\1$' /etc/passwd
17、查出分區空間使用率的最大百分比值
[root@ali ~]# df |grep '/dev/sda' |grep '\<[0-9]*%'
18、查出用戶UID最大值的用戶名、 UID及shell類型
[root@ali ~]# getent passwd |sort -nt: -k3 |cut -d: -f1,3,7 |tail -n1
19、查出/tmp的權限,以數字方式顯示
[root@ali ~]# stat /tmp/ |cut -d: -f2 |head -n4 |tr -d '(' |cut -d/ -f1 |tail -n1
[root@ali ~]# stat /tmp/ |head -n4 |tail -n1 |tr ' ' '\n' |head -n2 |tr -cd '[:digit:]'
[root@ali ~]# stat -c %a /tmp/
20、統計當前連接本機的每個遠程主機IP的連接數,并按從大到小排序
[root@ali ~]# netstat -nt |grep 'tcp' |tr -s ' ' '|' |cut -d'|' -f4 |uniq -c |sort -n
21、 顯示三個用戶root、 mage、 wang的UID和默認shell
[root@ali ~]# egrep '^\<root|mage|wang\>' /etc/passwd |cut -d: -f1,3,7
22、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下劃線)后面跟一個小括號的行
[root@ali ~]# cat /etc/rc.d/init.d/functions |egrep '^[[:alpha:]_]+\(\)'
23、使用egrep取出/etc/rc.d/init.d/functions中其基名
[root@ali ~]# echo '/etc/rc.d/init.d/functions' |egrep -o '[^/]+/?$'
24、使用egrep取出上面路徑的目錄名
[root@ali ~]# echo '/etc/rc.d/init.d/functions' |egrep -o '(/.*/)'
25、統計以root身份登錄的每個遠程主機IP地址的登錄次數
[root@ali ~]# last |egrep "^root\>.*([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}" |tr -s ' ' ':' |cut -d: -f3 |sort |uniq -c
28 10.1.250.14
1 172.18.19.209
26、利用擴展正則表達式分別表示0-9、 10-99、 100-199、200-249、 250-255
'(([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])'
27、顯示ifconfig命令結果中所有IPv4地址
[root@ali ~]# 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])'
原創文章,作者:M20-1馬星,如若轉載,請注明出處:http://www.www58058.com/30236