練習1:
1、找出ifconfig “網卡名” 命令結果中本機的IPv4地址
ifconfig |head -n 2 |tail -n 1|tr -s ” ” : |cut -d: -f4
2、查出分區空間使用率的最大百分比值
df|tr -s ‘ ‘ %|sort -t% -k5 -n|tail -n 1|cut -d% -f5
3、查出用戶UID最大值的用戶名、UID及shell類型
cat /etc/passwd |cut -d: -f1,3,7|sort -n -t : -k 2|tail -n 1
4、查出/tmp的權限,以數字方式顯示
stat /tmp| head -n 4|tail -n 1|cut -d/ -f1|cut -d'(‘ -f2
stat -c %a /tmp/(目前還沒學到)
5、統計當前連接本機的每個遠程主機IP的連接數,并按從大到小排序
netstat -tun | grep ESTAB |tr -s ” ” : |cut -d: -f6 |sort -nr |uniq -c
(本機就一個遠程連接所以不用統計排序)
練習2
1、顯示/proc/meminfo文件中以大小s開頭的行(要求:使用兩種方法)
cat /proc/meminfo|grep “^[Ss]”
cat /proc/meminfo|grep -i “^s”
cat /proc/meminfo|grep -e ^s -e ^S
cat /proc/meminfo|grep “^s\|^S”
cat /proc/meminfo|grep “^[s\|S]”
2、顯示/etc/passwd文件中不以/bin/bash結尾的行
grep -v “/bin/bash$” /etc/passwd
3、顯示用戶rpc默認的shell程序
grep “^rpc\>” /etc/passwd | cut -d : -f7
grep -w “^rpc” /etc/passwd | cut -d : -f7
4、找出/etc/passwd中的兩位或三位數
cat /etc/passwd |grep -o “\<[0-9]\{2,3\}\>”
,,,
5、顯示CentOS7的/etc/grub2.cfg文件中,至少以一個空白字符開頭的且后面存非空白字符的行
cat /etc/grub2.cfg |grep “^[[:space:]]\+[^[:space:]]”
,,,
6、找出“netstat -tan”命令的結果中以‘LISTEN’后跟任意多
個空白字符結尾的行
netstat -tan|grep “\<LISTEN\>[[:space:]]*$”
7、顯示CentOS7上所有系統用戶的用戶名和UID
cat /etc/passwd |cut -d: -f1,3 |grep “\<[[:digit:]]\{1,3\}\>”$
,,,
8、添加用戶bash、testbash、basher、sh、nologin(其shell為/sbin/nologin),找出/etc/passwd用戶名同shell名的行
cat /etc/passwd | grep “\(^.*\)\>.*\/\1$”
9、僅利用df和grep和sort,取出磁盤各分區利用率,并從大到小排序
df |grep ^/dev/sd |grep -o “\b[[:digit:]]\{1,3\}\b%”|sort -rn
作業:
1、顯示三個用戶root、mage、wang的UID和默認shell
cat /etc/passwd|grep -E “^(root|wang|mage)\>”|cut -d : -f3,7
cat /etc/passwd|grep -E -w “^(root|wang|mage)”|cut -d : -f3,7
2、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下劃線)后面跟一個小括號的行
cat /etc/rc.d/init.d/functions |grep -E “^.*\(\)”
3、使用egrep取出/etc/rc.d/init.d/functions中其基名
echo /etc/rc.d/init.d/functions | egrep -o “[[:alpha:]]+$”
4、使用egrep取出上面路徑的目錄名
echo /etc/rc.d/init.d/functions | egrep -o “^(/).*\1”
5、統計last命令中以root登錄的每個主機IP地址登錄次數
last |grep -w root|tr -s ” ” %|cut -d% -f3|sort -n|uniq -c
6、利用擴展正則表達式分別表示0-9、10-99、100-199、200-249、250-255
echo {0..255}|egrep -o “\<[0-9]{1}\>”
echo {0..255}|egrep -o “\<[0-9]{2}\>”
,,,,
echo {0..255}|egrep -o “\<[0-9]{3}\>”|egrep “^1”
,,,,
,,,
echo {0..255}|egrep -o “\<[0-9]{3}\>”|egrep “^2[0-4]”
,,,
echo {0..255}|egrep -o “\<[0-9]{3}\>”|egrep “^25”
7、顯示ifconfig命令結果中所有IPv4地址
ifconfig | grep “netmask”|tr -s ” “|cut -d ” ” -f3,5,7|tr ” ” “\n”
8、將此字符串:welcome to magedu linux 中的每個字符去重并排序,重復次數多的排到前面
echo welcom to magedu linux|tr -d ” “|grep -o “.”|sort -n|uniq -c|sort -r
原創文章,作者:fuming,如若轉載,請注明出處:http://www.www58058.com/83091