1、显示当前系统上root、fedora或user1用户的默认shell;
1. egrep "^(root|user1|fedora)" /etc/passwd|cut –d: –f7
2、找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如:hello();
1. egrep "^[[:alpha:]]+\(\)" /etc/rc.d/init.d/functions
3、使用echo命令输出一个绝对路径,使用grep取出其基名;
1. ~]#echo "/etc/init.d/" | grep –oE "[^/]+/?$"
2. init.d/
3. 扩展:取出其路径名
4. ~]#echo "/etc/init.d/" | grep –o "/[A-Za-z0-9]\+.*/"
5. /etc/init.d/
4、找出ifconfig命令结果中的1-255之间数字;
1. ifconfig | egrep –o "\<[1-9]\>|\<[1-9][0-9]\>|\<[1][0-9][0-9]\>|\<[2][0-4][0-9]\>|\<[2][5][0-5]\>"
5、挑战题:写一个模式,能匹配合理的IP地址;
1. ifconfig | egrep –o "(\<[1-9]\>|\<[1-9][0-9]\>|\<[1][0-9][0-9]\>|\<[2][0-4][0-9]\>|\<[2][5][0-5]\>)\.(\<[0-9]\>|\<[1-9][0-9]\>|\<[1][0-9][0-9]\>|\<[2][0-4][0-9]\>|\<[2][5][0-5]\>)\.(\<[0-9]\>|\<[1-9][0-9]\>|\<[1][0-9][0-9]\>|\<[2][0-4][0-9]\>|\<[2][5][0-5]\>).(\<[0-9]\>|\<[1-9][0-9]\>|\<[1][0-9][0-9]\>|\<[2][0-4][0-9]\>|\<[2][5][0-5]\>)"
6、挑战题:写一个模式,能匹配出所有的邮件地址;
1. ~]# echo "My Email:jinjianping@qq.com" | grep –o "\<[[:alpha:]]\+.*@[0-9a-z]\+\.[[:alpha:]]\+\>"
2. My Email:jinjianping@qq.com
7、查找/var目录下属主为root,且属组为mail的所有文件或目录;
1. ~]# find /var –user root –group mail –ls
2. 134311219 4 drwxrwxr–x 2 root mail 4096 Nov 24 15:32 /var/spool/mail
3. 135420759 4 –rw——- 1 root mail 1355 Nov 9 15:45 /var/spool/mail/root
8、查找当前系统上没有属主或属组的文件;
1. ~]# find / \( –nouser –o –nogroup \) –ls
2. find: ‘/proc/5594/task/5594/fd/6’: No such file or directory
3. find: ‘/proc/5594/task/5594/fdinfo/6’: No such file or directory
4. find: ‘/proc/5594/fd/6’: No such file or directory
5. find: ‘/proc/5594/fdinfo/6’: No such file or directory
6. 135441653 0 –rw–rw—- 1 3005 mail 0 Nov 5 18:21 /var/spool/mail/gentoo
7. find: File system loop detected; ‘/home1’ is part of the same file system loop as ‘/’.
进一步:查找当前系统上没有属主或属组,且最近3天内曾被访问过的文件或目录;
1. ~]# find / –atime –3 \( –nouser –o –nogroup \) –ls
2. find: ‘/proc/5601/task/5601/fd/6’: No such file or directory
3. find: ‘/proc/5601/task/5601/fdinfo/6’: No such file or directory
4. find: ‘/proc/5601/fd/6’: No such file or directory
5. find: ‘/proc/5601/fdinfo/6’: No such file or directory
6. find: File system loop detected; ‘/home1’ is part of the same file system loop as ‘/’.
9、查找/etc目录下所有用户都有写权限的文件;
1. find /etc –perm –222 –ls
10、查找/etc目录下大于1M,且类型为普通文件的所有文件;
1. find /etc –type f –size +1M –ls
11、查找/etc/init.d/目录下,所有用户都有执行权限,且其它用户有写权限的文件;
1. find /etc/init.d/ –type f –perm –113
12、查找/usr目录下不属于root、bin或hadoop的文件;
1. find /usr ! \( –user root –o –user bin –o –user hadoop \) –ls
13、查找/etc/目录下至少有一类用户没有写权限的文件;
1. find /etc/ \( ! –perm +200 –o ! –perm +020 –o ! –perm +002 \) –ls
14、查找/etc目录下最近一周内其内容被修改过,且不属于root或hadoop的文件;
1. find /etc \( –mtime –7 ! –user root –a ! –user hadoop \) –ls
原创文章,作者:N24_JJP,如若转载,请注明出处:http://www.178linux.com/61304
评论列表(1条)
grep与find是基础命令,希望你能牢记,正则表达式主要是需要多练习的,加油!