1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行;

[root@localhost /]# grep "^[[:space:]]\+" /boot/grub/grub.conf   root (hd0,0)  kernel /vmlinuz-2.6.32-504.el6.x86_64 ro root=UUID=aa5cf6b3-e1b5-4eb2-95e6-e202f2890edf rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet	initrd /initramfs-2.6.32-504.el6.x86_64.img

2、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;

[root@localhost /]# grep "^#[[:space:]]\+[^[:space:]]" /etc/rc.d/rc.sysinit

3、打出netstat -tan命令执行结果中以‘LISTEN’,后或跟空白字符结尾的行;

[root@localhost /]# netstat -tan | grep "LISTEN[[:space:]]*$"tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      tcp        0      0 :::22                       :::*                        LISTEN      tcp        0      0 ::1:631                     :::*                        LISTEN      tcp        0      0 ::1:25                      :::*                        LISTEN      [root@localhost /]#

4、添加用户bash, testbash, basher, nologin (此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户的信息;

[root@localhost /]# grep -E '^(\<[a-z]+\>).*\1$' /etc/passwdsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltnologin:x:501:501::/home/nologin:/sbin/nologinbash:x:502:502::/home/bash:/bin/bash[root@localhost /]#

5、显示当前系统上root、fedora或user1用户的默认shell;

[root@localhost /]# grep -E '^(root|fedora|user1)' /etc/passwdroot:x:0:0:root:/root:/bin/bash[root@localhost /]#

6、找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如:hello();

[root@localhost /]# grep -E "[a-z]+\(\)" /etc/rc.d/init.d/functions

7、使用echo命令输出一个绝对路径,使用grep取出其基名;

 扩展:取出其路径名

[root@localhost network-scripts]# echo /etc/sysconfig/network-scripts | grep -o "[^/]*$"network-scripts
[root@localhost network-scripts]# echo /etc/sysconfig/network-scripts | grep -oP "^.*(?=/)"/etc/sysconfig
[root@localhost network-scripts]#

8、找出ifconfig命令结果中的1-255之间数字;

[root@localhost ~]# ifconfig | egrep -o "[1-9]{1,2}|2[0-5]{1,2}"

9、挑战题:写一个模式,能匹配合理的IP地址;

[root@localhost ~]# ifconfig | egrep -o "[1-9]{1,3}\.[1-9]{1,3}\.[1-9]{1,3}\.[1-9]{1,3}" 192.168.1.8192.168.1.255192.168.1.25192.168.253.135192.168.253.255192.168.253.25[root@localhost ~]# 备注:这写法会匹配888.888.888.888之类的数字,但是ifconfig的结果中以点分十进制数字中不会出现此类数字!!偷懒的写法

10、挑战题:写一个模式,能匹配出所有的邮件地址;

 [root@localhost ~]# cat mail.txt  | grep '[[:alnum:]]\+@[[:alnum:]]\+\.[[:alnum:]]\+$' mfchangs@126.com
mfchangs@sina.com
mfchangs@qq.com
mfchangs100@sina.com
[root@localhost ~]# cat mail.txt 123456@adadfmfchangs@126.com
mfchangs@sina.com
mfchangs@qq.com
akjdflajf@qqajdlaflkmfchangs100@sina.com
[root@localhost ~]#

11、查找/var目录下属主为root,且属组为mail的所有文件或目录;

[root@localhost ~]# find  /var -user root -group mail /var/spool/mail
/var/spool/mail/root
[root@localhost ~]#

12、查找当前系统上没有属主或属组的文件;

  进一步:查找当前系统上没有属主或属组,且最近3天内曾被访问过的文件或目录;

[root@localhost /]# find / -nouser -o -nogroup [root@localhost ~]# find /  -nouser -o -nogroup  -a -atime 3

13、查找/etc目录下所有用户都有写权限的文件;

[root@localhost ~]# find /etc -perm -222 -ls

14、查找/etc目录下大于1M,且类型为普通文件的所有文件;

[root@localhost ~]#  find /etc -type f -size +1M -ls668012 7892 -rw-r--r--   1 root     root      8080653 Jul  4 08:37 /etc/selinux/targeted/modules/active/policy.kern668019 7892 -rw-r--r--   1 root     root      8080653 Jul  4 08:37 /etc/selinux/targeted/policy/policy.24794284 1976 -rw-r--r--   1 root     root      2020885 Jul  4 08:34 /etc/gconf/gconf.xml.defaults/%gconf-tree.xml
[root@localhost ~]#

15、查找/etc/init.d/目录下,所有用户都有执行权限,且其它用户有写权限的文件;

[root@localhost ~]# find /etc/init.d/ -type f -perm -102 -ls663307    0 -rwxrwxrwx   1 root     root            0 Jul  9 17:20 /etc/init.d/test.txt

16、查找/usr目录下不属于root、bin或hadoop的文件;

[root@localhost ~]# touch /usr/2.txt[root@localhost ~]# chown test:test /usr/2.txt[root@localhost ~]# find /usr/ -type f !  \( -user  root -o -user bin -o -user hadoop \) -ls400099   12 -rwsr-xr-x   1 abrt     abrt        10296 Oct 16  2014 /usr/libexec/abrt-action-install-debuginfo-to-abrt-cache395439    0 -rw-r--r--   1 test     test            0 Jul  9 17:29 /usr/2.txt
[root@localhost ~]#

17、查找/etc/目录下至少有一类用户没有写权限的文件;

[root@localhost ~]# find /etc/ ! -perm +222 -ls

18、查找/etc目录下最近一周内其内容被修改过,且不属于root或hadoop的文件;

[root@localhost ~]# find /etc/ -type f -ctime -7  -a ! \( -user root -o -user hadoop \) -ls663346    4 -rw-r--r--   1 test     test            2 Jul  9 17:37 /etc/2.txt
[root@localhost ~]#