1、顯示當前系統上root、fedora或user1用戶的默認shell;
[root@localhost ~]# grep -E ‘^(root|fedora|user1)\>’ /etc/passwd
root:x:0:0:root:/root:/bin/bash
fedora:x:4002:4002:Fedora Core:/home/fedora:/bin/tcsh
user1:x:4005:4005::/home/user1:/bin/bash
2、找出/etc/rc.d/init.d/functions文件中某單詞后面跟一組小括號的行,形如:hello();
[root@localhost ~]# grep -E -o ‘^[_[:alpha:]]+()’ /etc/rc.d/init.d/functions
checkpid()
__pids_var_run()
__pids_pidof()
daemon()
3、使用echo命令輸出一個絕對路徑,使用grep取出其基名和路徑名;
[root@localhost init.d]# echo $PWD | grep -E -o “[^/]+/?$”
init.d
[root@localhost init.d]# echo $PWD | grep -E -o ‘^/.*/’
/etc/rc.d/
4、找出ifconfig命令結果中的1-255之間的數字;
[root@localhost ~]# ifconfig | grep -E -o ‘\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>’
5、寫一個模式,能匹配合理的IP地址;
[root@localhost ~]# ifconfig | grep “inet\b” |cut -d’ ‘ -f10
192.168.78.17
10.1.1.2
127.0.0.1
6、寫一個模式,能匹配出所有的郵件地址;
[root@localhost tmp]# grep -E ‘^[[:alnum:]]+\@[[:alnum:]]+.(com|cn)\>’ mail.test
abc@163.com
163@163.com
sys@qq.com
root@abc163.cn
7、查找/var目錄下屬主的root,且屬組為mail的所有文件或目錄;
[root@localhost tmp]# find /var/ -user root -group mail -ls
134321240 4 drwxrwxr-x 2 root mail 4096 3月 4 11:27 /var/spool/mail
8、查找當前系統上沒有屬主或屬組的文件;查找當前系統上沒有屬主或屬組,且最近三天內層被訪問過的文件或目錄;
[root@localhost tmp]# find / -nouser -nogroup -ls
131 0 drwx—— 2 4001 5002 59 2月 18 16:47 /home/gentoo
132 4 -rw-r–r– 1 4001 5002 18 11月 20 2015 /home/gentoo/.bash_logout
133 4 -rw-r–r– 1 4001 5002 193 11月 20 2015 /home/gentoo/.bash_profile
134 4 -rw-r–r– 1 4001 5002 231 11月 20 2015 /home/gentoo/.bashrc
459464 4 -rw——- 1 4001 5002 5 2月 28 10:28 /var/tmp/gentoo/.bash_history
[root@localhost tmp]# find / -nouser -nogroup -atime -3 -ls
131 0 drwx—— 2 4001 5002 59 2月 18 16:47 /home/gentoo
134 4 -rw-r–r– 1 4001 5002 231 11月 20 2015 /home/gentoo/.bashrc
[root@localhost tmp]# date
2017年 03月 13日 星期一 11:05:59 CST
[root@localhost tmp]# stat /home/gentoo/.bashrc
文件:”/home/gentoo/.bashrc”
大?。?31 塊:8 IO 塊:4096 普通文件
設備:fd02h/64770d Inode:134 硬鏈接:1
權限:(0644/-rw-r–r–) Uid:( 4001/ UNKNOWN) Gid:( 5002/ UNKNOWN)
環境:unconfined_u:object_r:user_home_t:s0
最近訪問:2017-03-13 11:04:36.236017660 +0800
最近更改:2015-11-20 13:02:30.000000000 +0800
最近改動:2017-02-18 16:47:47.342556415 +0800
創建時間:-
9、查找/etc目錄下所有用戶都有寫權限的文件;
[root@localhost tmp]# find /etc/ -perm -222 -ls
134320260 0 lrwxrwxrwx 1 root root 17 12月 30 03:07 /etc/mtab -> /proc/self/mounts
10、查找/etc目錄下大與1M,且類型為普通文件的所有文件;
[root@localhost tmp]# find /etc/ -size +1M -type f -ls
67866634 6824 -r–r–r– 1 root root 6984832 12月 30 03:17 /etc/udev/hwdb.bin
134926960 3772 -rw-r–r– 1 root root 3858924 11月 21 2015 /etc/selinux/targeted/policy/policy.29
原創文章,作者:徐 琦,如若轉載,請注明出處:http://www.www58058.com/71044
正則運用的不錯,正則掌握好了,今后會大大提高工作效率的。