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

1
2
3
4
5
6
[[email protected] ~]# useradd fedora              #新建用户fedora
[[email protected] ~]# useradd user1               #新建用户user1
[[email protected] ~]# egrep '^(root|fedora|user1)\>' /etc/passwd | cut -d: -f1,7   #由于(root|fedora|user1)是扩展表达式,所以要使用egrep或grep -E
root:/bin/bash
fedora:/bin/bash
user1:/bin/bash


注:^(root|fedora|user1)\>表示以这三个用户开头的行。cut -d: -f1,7表示以:为分隔符,取第1和第7字段,这二个字段是用户名和shell的。

图示:

马哥运维学习作业(五)


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

1
2
3
4
[[email protected] ~]# egrep -o "^[_[:alpha:]]+\(\)" /etc/rc.d/init.d/functions
checkpid()
__pids_var_run()
....略


注:根据题目要求,文件中__pids_var_run()也是符合要求的,^[_[:alpha:]]+中,_不能省掉,代表以_大小写字母开头,+表示前面字符可以出现一次或多次,\(\)是使用转义符来代表小括号()

图示:

马哥运维学习作业(五)


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

1
2
3
4
5
6
[[email protected]cat ~]# echo "/mnt/sdc" | grep -E "[^/]+$"        #行尾的字符串[^/]除了/斜线的任意内容,+代表这个/至少出现1次
/mnt/sdc
[[email protected]cat ~]# echo "/mnt/sdc/" | grep -E "[^/]+/?$"     #如果sdc后还带一个/,就用/?表示可以出现1次或不出现
/mnt/sdc/
[[email protected]cat ~]# echo "/mnt/sdc" | grep -E -o "[^/]+/?$" | cut -d"/" -f1   #-o只显示匹配的,以/作为分隔符,取第1字段,取出sdc
sdc

扩展:取出其路径名

1
2
[[email protected]cat ~]# echo "/mnt/sdc/test" | grep -o "/.*/"     #取出路径名,"/.*/"表示以/开头后面可出现任意长度任意字符,以/结尾,这样就符合路径名的规则。           
/mnt/sdc/

图示:

马哥运维学习作业(五)


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

1
2
3
4
5
6
[[email protected] ~]# ifconfig | grep -E -o "\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>"
192
168
2
127
...略

图示:

马哥运维学习作业(五)


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

1
2
3
4
5
6
7
8
[[email protected]cat ~]# cat ip             #手动写了几个IP地址,有对有错,用下面的条件来筛选
980.168.88.66
192.168.88.255
253.252.251.0
192.168.2.60
255.0.0.690
[[email protected]cat ~]# grep -E "\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-3][0-9])\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-4])\>" ip 
192.168.2.60


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[[email protected] ~]# cat /tmp/mail          #编写了6个邮箱地址,其中4、5不符合邮箱命名规范
[[email protected] ~]# cat mail
[[email protected] ~]# egrep -o "[[:alnum:]]+[[:punct:]]*[[:alnum:]]*@[[:alnum:]]+.[[:alnum:]]*.?[[:alnum:]]*" mail


注:首先,咱们要了解邮箱地址的构成,简单来说,是由:用户名@域名 构成。
用户名命名要求:可使用字母、数字、下划线,开头可以使用数字或字母(QQ邮箱就是数字开头)
域名命名要求:可使用字母、数字、下划线组成,不能使用特殊字母开头等
详细的介绍请看下图:分别是新浪163邮箱命令介绍。阿里域名的命名介绍

图示:

马哥运维学习作业(五)马哥运维学习作业(五)马哥运维学习作业(五)

马哥运维学习作业(五)


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

1
2
3
4
[[email protected] ~]# find /var -user root -group mail
/var/spool/mail
[[email protected] ~]# find /var -user root -group mail -ls           #-ls是将查找到的文件显示出来
134321208    0 drwxrwxr-x   2 root     mail           16 7月 31 05:06 /var/spool/mail

图示:

马哥运维学习作业(五)


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

1
2
3
4
5
[[email protected] ~]# find / -nouser -o -nogroup -ls            #用-nouser -o -nogroup表示没有属
find: ‘/proc/16897/task/16897/fd/6’: 没有那个文件或目录
find: ‘/proc/16897/task/16897/fdinfo/6’: 没有那个文件或目录
find: ‘/proc/16897/fd/6’: 没有那个文件或目录
find: ‘/proc/16897/fdinfo/6’: 没有那个文件或目录

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
为了方便演示,创建了一个user1的用户,并用user1创建了一个文件,然后删除user1
[[email protected] ~]$ touch /tmp/b             #在user1用户下创建/tmp/b文件
[[email protected] ~]$ ll /tmp/b                #查看属主和属组,属于user1
-rw-rw-r--. 1 user1 user1 0 9月   5 21:24 /tmp/b
[[email protected] ~]$ 登出
[[email protected] ~]# 登出
[[email protected] test]# 登出
[[email protected] ~]# 登出
Last login: Mon Sep  5 21:23:45 2016
[[email protected] ~]# userdel user1             #退出后,删除user1用户
[[email protected] ~]# id user1                  #user1用户已删除
id: user1: no such user
[[email protected] ~]# ls -l /tmp/               #b文件属主和属组已变为数字
总用量 4
-rw-r--r--. 1 root root 28 9月   2 01:27 12
-rw-rw-r--. 1 2023 2023  0 9月   5 21:24 b
方法一:
[[email protected] ~]# find / -nouser -a -nogroup -a -atime -3      #-atime -3表示3天没有访问过的文件,查找到了b和user1用户下的文件
find: ‘/proc/16901/task/16901/fd/6’: 没有那个文件或目录
find: ‘/proc/16901/task/16901/fdinfo/6’: 没有那个文件或目录
find: ‘/proc/16901/fd/6’: 没有那个文件或目录
find: ‘/proc/16901/fdinfo/6’: 没有那个文件或目录
/tmp/b
/home/tom
/home/user1
/home/user1/.bash_logout
/home/user1/.bash_profile
/home/user1/.bashrc
/home/user1/.bash_history
方法二:
[[email protected] ~]# find / \( -nouser -o -nogroup \) -a -atime -3 #也可以这样用
find: ‘/proc/16901/task/16901/fd/6’: 没有那个文件或目录
find: ‘/proc/16901/task/16901/fdinfo/6’: 没有那个文件或目录
find: ‘/proc/16901/fd/6’: 没有那个文件或目录
find: ‘/proc/16901/fdinfo/6’: 没有那个文件或目录
/tmp/b
/home/tom
/home/user1
/home/user1/.bash_logout
/home/user1/.bash_profile
/home/user1/.bashrc
/home/user1/.bash_history

图示:

马哥运维学习作业(五)


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

1
2
3
4
5
[[email protected] ~]# find /etc -perm -222 -ls       #所有用户都有写权限用-perm -222表示
33554564    0 lrwxrwxrwx   1 root     root           17 9月  5 00:00 /etc/mtab -> /proc/self/mounts
33689712    0 lrwxrwxrwx   1 root     root           49 9月  5 00:01 /etc/pki/tls/certs/ca-bundle.crt -> /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
33689713    0 lrwxrwxrwx   1 root     root           55 9月  5 00:01 /etc/pki/tls/certs/ca-bundle.trust.crt -> /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
...略


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

1
2
3
[[email protected] ~]# find /etc -size +1M -type f -ls        #大于1MB用-size +1M表示
34288441 6824 -r--r--r--   1 root     root      6984832 9月  5 00:05 /etc/udev/hwdb.bin
67796802 3772 -rw-r--r--   1 root     root      3858924 11月 21  2015 /etc/selinux/targeted/policy/policy.29


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

1
2
[[email protected] ~]# find /etc/init.d -perm -113 -ls        #用户都有执行,其它用户有写权限用-perm -113表示
33688451    0 lrwxrwxrwx   1 root     root           11 9月  5 00:01 /etc/init.d -> rc.d/init.d


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

1
2
3
4
[[email protected] ~]# find /usr -not \( -user root -o -user bin -o -user hadoop \) -ls  #-not放在()外面,对里面的内容,即不也不又不,做条件
67424701    0 drwx------   2 polkitd  root            6 6月 10  2014 /usr/share/polkit-1/rules.d
[[email protected] ~]# find /usr ! \( -user root -o -user bin -o -user hadoop \) -ls     #-not和!是同样的意思
67424701    0 drwx------   2 polkitd  root            6 6月 10  2014 /usr/share/polkit-1/rules.d


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

1
2
3
4
[[email protected] ~]# find /etc -not -perm -222 -ls       #至少一类用户没有写权限用-perm -222表示
33554561   12 drwxr-xr-x  76 root     root         8192 9月  5 04:04 /etc
33554562    4 -rw-r--r--   1 root     root          465 9月  5 00:00 /etc/fstab
....略


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

1
2
3
4
5
6
7
8
9
10
[[email protected] ~]# ll /etc/test123                      #默认没有满足条件的文件,所以先在/etc下创建一个test123的文件,查看其属主和属组都是root
-rw-r--r-- 1 root root 0 Sep  1 16:00 /etc/test123
[[email protected] ~]# useradd test                         #创建一个test用户,用来把上面文件属主属组都改为test
[[email protected] ~]# chown test.test /etc/test123         #把/etc下的test123改为属主属组root
[[email protected] ~]# ll /etc/test123                      #已改为root
-rw-r--r-- 1 test test 0 Sep  1 16:00 /etc/test123
[[email protected] ~]# find /etc -mtime -7 -a -not -user root -not -user hadoop    #根据题目要求查看,-mtime -7表示7天没修改过
/etc/test123
[[email protected] ~]# find /etc -mtime -7 -not \( -user root -o -user hadoop \) -ls   #-not放外面对里面来做判断,不属于root和hadoop。注意-not放外面要把()里的-a换成-o
397495    0 -rw-r--r--   1 test     test            0 Sep  1 16:00 /etc/test123

图示:

马哥运维学习作业(五)