1、顯示當前系統上root、fedora或user1用戶的默認shell。
grep -E "^(root|fedora|user1)\>" /etc/passwd | cut -d: -f7
[root@localhost ~]# grep -E “^(root|fedora|user1)\>” /etc/passwd | cut -d: -f7 /bin/bash /bin/bash /bin/bash |
2、找出/etc/rc.d/init.d/functions文件中某單詞后面跟一組小括號的行,形如:hell()。
grep -E -o "[[:alpha:]]+\(\)" /etc/rc.d/init.d/functions
[root@localhost ~]# grep -E -o “[[:alpha:]]+\(\)” /etc/rc.d/init.d/functions str() checkpid() readlink() fgrep() loop() loop() run() pidof() daemon() killproc() pidfileofproc() pidofproc() status() success() failure() passed() warning() stage() success() failure() passed() warning() action() strstr() confirm() dev() file() true() false() sysctl() random() point() crypto() |
3、使用echo命令輸出一個絕對路徑,使用grep取出其基名,擴展:取出其路徑名。
echo "/var/log/messages"|grep -Eo "[^/]+\/?$"
[root@localhost ~]# echo “/var/log/messages”|grep -Eo “[^/]+\/?$” messages |
echo "/var/log/messages"|grep -Eo "^/.*/"
[root@localhost ~]# echo “/var/log/messages”|grep -Eo “^/.*/” /var/log/ |
4、找出ifconfig命令結果中的1-255之間數字。
ifconfig |grep -Eo "\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>"
[root@localhost ~]# ifconfig |grep -Eo “\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>” 29 192 168 3 35 192 168 3 255 255 255 255 64 1 7 6 127 1 255 1 128 1 4 4 240 240 240 240 |
5、挑戰題:寫一個模式,能匹配合理的IP地址。
[root@localhost ~]# ifconfig |grep -Eo "([1-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])"
[root@localhost ~]# ifconfig |grep -Eo “([1-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])” 192.168.3.35 192.168.3.255 255.255.255.0 127.0.0.1 255.0.0.0 |
6、挑戰題:寫一個模式,能匹配出所有的郵件地址。
grep -E "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+/.[A-Za-z]{2,6}"
7、查找/var目錄下屬主為root,且屬組為mail的所有文件或目錄。
find /var -user root -group mail -ls
[root@localhost ~]# find /var -user root -group mail -ls 1440148 4 drwxrwxr-x 2 root mail 4096 7月 31 01:54 /var/spool/mail |
8、查找當前系統上沒有屬主或屬組的文件,進一步:查找當前系統上沒有屬主或屬組,且最近3天內曾被訪問過的文件或目錄。
find / -nouser -o -nogroup -ls
find / ( -nouser -o -nogroup ) -atime -3 -ls
9、查找/etc目錄下所有用戶都有寫權限的文件。
find /etc -perm -222 -ls
結果全部為鏈接文件。
10、查找/etc目錄下大于1M,且類型為普通文件的所有文件。
find /etc -size +1M -type f -ls
[root@localhost ~]# find /etc -size +1M -type f -ls 1320064 2148 -rw-r–r– 1 root root 2198778 7月 21 18:12 /etc/gconf/gconf.xml.defaults/%gconf-tree.xml 1191199 7124 -rw-r–r– 1 root root 7292689 7月 21 18:11 /etc/selinux/targeted/policy/policy.24 1191196 7124 -rw-r–r– 1 root root 7292689 7月 21 18:11 /etc/selinux/targeted/modules/active/policy.kern |
原創文章,作者:N27_huiyezhicheng,如若轉載,請注明出處:http://www.www58058.com/83350
看來grep和find已經很好掌握了,如果再加上sed和awk就理完美了,加油。