一、顯示當前系統上root、fedora或user1用戶的默認shell;
[root@promote home]# grep -E ‘^(root|fedora|user1)’ /etc/passwd |
cut -d : -f7
/bin/bash
/bin/bash
/bin/bash
[root@promote home]#
注:僅egrep支持(a|b)這種模式
二、找出/etc/rc.d/init.d/functions文件中某單詞后面跟一組小括號的行,形如:hello();
[root@VM_221_40_centos ~]# grep -o
“\<[_[:alpha:]]\+\>(.*)” /etc/rc.d/init.d/functions
checkpid()
__pids_var_run()
__pids_pidof()
daemon()
killproc()
if($1!~/^[0-9.]+[smhd]?$/) exit 1;d=$1~/s$|^[0-9.]*$/?1:$1~/m$/?60:$1~/h$/?60*60:$1~/d$/?24*60*60:-1;if(d==-1)
exit 1;delay+=d*$1} END {printf(“%d”,delay+0.5)}’)
pidfileofproc()
pidofproc()
status()
echo_success()
echo_failure()
echo_passed()
echo_warning()
update_boot_stage()
success()
failure()
passed()
warning()
action()
strstr()
is_ignored_file()
is_true()
is_false()
apply_sysctl()
[root@VM_221_40_centos ~]#
三、使用echo命令輸出一個絕對路徑,使用grep取出其基名;
[root@VM_221_40_centos
init.d]# echo $PWD | grep -o “[^/]\+$”
init.d
[root@VM_221_40_centos
init.d]#
擴展:取出其路徑名
[root@VM_221_40_centos init.d]# echo $PWD | grep -o
“^/.*/”
/etc/rc.d/
[root@VM_221_40_centos init.d]#
四、找出ifconfig命令結果中的1-255之間數字;
[root@VM_221_40_centos init.d]# ifconfig | grep -E -o
“\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>”
五、挑戰題:寫一個模式,能匹配合理的IP地址;
grep -E
“\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){2}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])”
/tmp/ip
六、挑戰題:寫一個模式,能匹配出所有的郵件地址;
郵箱地址中用戶名可以是數字、字母(分大小寫)、下劃線。
grep -Eo “\<[a-z,A-Z,0-9,_-]+@[A-Z,a-z,0-9,-]+\.[A-Z,a-z,0-9]{2,}\>”
/tmp/mail
或者
grep -o
“\<[a-z,A-Z,0-9,_]\+@[A-Z,a-z,0-9,-]\+\.[A-Z,a-z,0-9]\{2,\}\>”
/tmp/mail
七、查找/var目錄下屬主為root,且屬組為mail的所有文件或目錄;
[root@VM_221_40_centos ~]# find /var -user root -group mail -ls
24631 4 drwxrwxr-x 2 root mail 4096 May 15 00:22
/var/spool/mail
[root@VM_221_40_centos ~]#
八、查找當前系統上沒有屬主或屬組的文件;
[root@VM_221_40_centos ~]# find /home -nouser -a -nogroup -ls | more
278530 4 drwx—— 3 1000 1000 4096 May 15 00:23
/home/gentoo
278534 4 -rw——- 1 1000 1000 126 May 15 02:02
/home/gentoo/.bash_hi
story
278535 4 drwxrwxr-x 2 1000 1000 4096 May 15 00:23
/home/gentoo/gen
278531 4 -rw-r–r– 1 1000 1000 231 Aug 3 2016
/home/gentoo/.bashrc
278532 4 -rw-r–r– 1 1000 1000 18 Aug 3 2016
/home/gentoo/.bash_lo
gout
278533 4 -rw-r–r– 1 1000 1000 193 Aug 3 2016
/home/gentoo/.bash_pr
ofile
[root@VM_221_40_centos ~]#
進一步:查找當前系統上沒有屬主或屬組,且最近3天內曾被訪問過的文件或目錄;
[root@VM_221_40_centos ~]# find /home
-nouser -a -nogroup -a -ctime -3 -ls | more
278530 4 drwx—— 3 1000 1000 4096 May 15 00:23 /home/gentoo
278534 4 -rw——- 1 1000 1000 126 May 15 02:02
/home/gentoo/.bash_hi
story
278535 4 drwxrwxr-x 2 1000 1000 4096 May 15 00:23 /home/gentoo/gen
278531 4 -rw-r–r– 1 1000 1000 231 Aug 3 2016
/home/gentoo/.bashrc
278532 4 -rw-r–r– 1 1000 1000 18 Aug 3 2016
/home/gentoo/.bash_lo
gout
278533 4 -rw-r–r– 1 1000 1000 193 Aug 3 2016
/home/gentoo/.bash_pr
ofile
[root@VM_221_40_centos ~]#
九、查找/etc目錄下所有用戶都有寫權限的文件;
[root@VM_221_40_centos ~]# find ./ -perm -222 –ls
十、查找/etc目錄下大于1M,且類型為普通文件的所有文件;
[root@VM_221_40_centos
~]# find /etc -size +1M -type f –ls
原創文章,作者:lixinkuan,如若轉載,請注明出處:http://www.www58058.com/75770
進度要跟上了,加油。