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

答:

[root@mageeduCT7 ~]# egrep '^(root|user1)\>' /etc/passwd | cut -d: -f7
/bin/bash
/bin/bash

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

答:

[root@mageeduCT7 ~]# grep '\<[[:alpha:]]\+\>()' /etc/rc.d/init.d/functions
checkpid() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
success() {
failure() {
passed() {
warning() {
action() {
strstr() {


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

   扩展:取出其路径名

答:

#基名
[root@mageeduCT7 ~]# echo "/tmp/log/txt" | egrep -o "[^/]+/?$"
txt
#路径名
[root@mageeduCT7 ~]# echo "/tmp/log/txt" | egrep -o ".*/"
/tmp/log/

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

答:

[root@mageeduCT7 ~]# ifconfig | egrep -ow '\<[1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5]\>'
10
11
255
255
255
10
255
64
29
4
171
73
127
1
255
1
128

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

答:

egrep "(\<([0,1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\>\.){3}\<([0,1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\>"
注释:
[0,1]?[0-9]?[0-9]  :匹配0-199
2[0-4][0-9]  :匹配200-249
25[0-5]  :匹配250-255
([0,1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])  :匹配0-255

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

答:

[root@mageeduCT7 ~]# egrep "^([[:alpha:]]|[0-9]|\_)*@([[:alpha:]]|[0-9])*.com$"

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

答:

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

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

答:

[root@mageeduCT7 ~]# find / -nouser -a -nogroup
find: ‘/proc/2883/task/2883/fd/6’: 没有那个文件或目录
find: ‘/proc/2883/task/2883/fdinfo/6’: 没有那个文件或目录
find: ‘/proc/2883/fd/6’: 没有那个文件或目录
find: ‘/proc/2883/fdinfo/6’: 没有那个文件或目录

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

[root@mageeduCT7 ~]# find / -nouser -nogroup -mtime -3
find: ‘/proc/2890/task/2890/fd/6’: 没有那个文件或目录
find: ‘/proc/2890/task/2890/fdinfo/6’: 没有那个文件或目录
find: ‘/proc/2890/fd/6’: 没有那个文件或目录
find: ‘/proc/2890/fdinfo/6’: 没有那个文件或目录

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

答:

[root@mageeduCT7 ~]# find /etc -perm -222 -ls
134320260    0 lrwxrwxrwx   1 root     root           17 8月 14 16:45 /etc/mtab -> /proc/self/mounts
67332427    0 lrwxrwxrwx   1 root     root           49 8月 14 16:45 /etc/pki/tls/certs/ca-bundle.crt -> /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
67332428    0 lrwxrwxrwx   1 root     root           55 8月 14 16:45 /etc/pki/tls
....略

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

答:

[root@mageeduCT7 ~]# find /etc -size +1M -type f -exec ls -lh {} \;
-r--r--r--. 1 root root 6.7M 8月  14 16:50 /etc/udev/hwdb.bin
-rw-r--r--. 1 root root 3.7M 11月 21 2015 /etc/selinux/targeted/policy/policy.29

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

答:

[root@mageeduCT7 ~]# find /etc/init.d -perm -113 -ls
134356480    0 lrwxrwxrwx   1 root     root           11 8月 14 16:45 /etc/init.d -> rc.d/init.d

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

答:

[root@mageeduCT7 ~]# find /usr -not \( -user root -o -user bin -o -user hadoop \) -ls
134672278    0 drwx------   2 polkitd  root            6 6月 10  2014 /usr/share/polkit-1/rules.d

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

答:

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

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

答:

[root@mageeduCT7 ~]# find /etc -mtime -7 -a -not -user root -a -not -user hadoop