1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行;
]# grep -E '^[[:space:]]+.*' /boot/grub/grub.conf2、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;
]# grep -E '^#[[:space:]]+[^[:space:]]+' /etc/rc.d/rc.sysinit3、打出netstat -tan命令执行结果中以‘LISTEN’,后或跟空白字符结尾的行;
]# netstat -nat | grep -E '.*LISTEN[[:space:]]*'4、添加用户bash, testbash, basher, nologin (此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户的信息;
]# grep -E '^(.*)\>.*\1$' /etc/passwdsync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bash:x:4007:4007::/home/bash:/bin/bash
nologin:x:4010:4015::/home/nologin:/sbin/nologin
5、显示当前系统上root、fedora或user1用户的默认shell;
]# grep -e ^root -e ^fedora -e ^user1 /etc/passwd | cut -d':' -f1,7root:/bin/bash
user1:/bin/bash
fedora:/bin/bash
6、找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如:hello();
]# grep -E '\<[[:alpha:]]*\>\(\)' /etc/rc.d/init.d/functions7、使用echo命令输出一个绝对路径,使用grep取出其基名;
]# echo /etc/sysconfig/network-scripts/ifcfg-eth0 | grep -Eo '[^/]*/?$'ifcfg-eth0
扩展:取出其路径名
]# echo /etc/sysconfig/network-scripts/ifcfg-eth0 | grep -oP '/.*/(?=.?\b)'/etc/sysconfig/network-scripts/
8、找出ifconfig命令结果中的1-255之间数字;
ifconfig | grep -E '\<([0-9]|([0-9][0-9])|(1[0-9][0-9])|(2[1-4][0-9])|(25[1-5]))\>'9、挑战题:写一个模式,能匹配合理的IP地址;
]# ifconfig | grep -A 2 eth0 | grep -Eo '([0-9]{1,3}\.?){4}' | grep -v 255172.20.51.128
10、挑战题:写一个模式,能匹配出所有的邮件地址;
]# grep -o '[[:alnum:]_.]*@[[:alnum:].]*'11、查找/var目录下属主为root,且属组为mail的所有文件或目录;
]# find /var -user root -a -group mail/var/spool/mail
/var/spool/mail/root
12、查找当前系统上没有属主或属组的文件;
]# find / -nouser -o -nogroup -type f/var/spool/mail/mandriva
find: “/proc/10154/task/10154/fd/5”: 没有那个文件或目录
find: “/proc/10154/task/10154/fd/5”: 没有那个文件或目录
find: “/proc/10154/task/10154/fdinfo/5”: 没有那个文件或目录
find: “/proc/10154/task/10154/fdinfo/5”: 没有那个文件或目录
find: “/proc/10154/fd/5”: 没有那个文件或目录
find: “/proc/10154/fd/5”: 没有那个文件或目录
find: “/proc/10154/fdinfo/5”: 没有那个文件或目录
find: “/proc/10154/fdinfo/5”: 没有那个文件或目录
/home/mandriva
/home/mandriva/.bash_logout
/home/mandriva/.bash_profile
/home/mandriva/.bashrc
/home/mandriva/.gnome2
进一步:查找当前系统上没有属主或属组,且最近3天内曾被访问过的文件或目录;
]# find / -type f -a -atime -3 -a \( -nouser -o -nogroup \)13、查找/etc目录下所有用户都有写权限的文件;
]# find /etc -perm -22214、查找/etc目录下大于1M,且类型为普通文件的所有文件;
]# find /etc -size +1M -a -type f | xargs ls -lh-rw-r--r--. 1 root root 7.0M 11月 21 17:22 /etc/selinux/targeted/modules/active/policy.kern
-rw-r--r--. 1 root root 7.0M 11月 21 17:22 /etc/selinux/targeted/policy/policy.24
15、查找/etc/init.d/目录下,所有用户都有执行权限,且其它用户有写权限的文件;
[root@director ~]# touch /etc/init.d/test.txt[root@director ~]# chmod 113 /etc/init.d/test.txt
[root@director ~]# find /etc/init.d/ -perm
[root@director ~]# find /etc/init.d/ -perm -113 | xargs ls -lh
---x--x-wx 1 root root 0 12月 30 03:59 /etc/init.d/test.txt
16、查找/usr目录下不属于root、bin或hadoop的文件;
[root@director ~]# find /usr/ ! \( -user root -o -user bin -o -user hadoop \) -a -type f17、查找/etc/目录下至少有一类用户没有写权限的文件;
]# find /etc/ ! -perm -222 -a -type f18、查找/etc目录下最近一周内其内容被修改过,且不属于root或hadoop的文件;
]# find /etc/ -mtime -7 -a ! \( -user root -o -user hadoop \) -a -type f