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

答:需要找到3个字符串,需要用到“或”命令,所以使用egrep。找到后cut再次筛选出我们需要显示的用户默认的shell。

命令为:egrep "^(root|fedora|user1)" /etc/passwd | cut -d: -f1,7

结果如下:

马哥2016全新Linux+Python高端运维班第五周作业_linux

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

答:马哥2016全新Linux+Python高端运维班第五周作业_linux_02

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

答:首先我们要了解什么是路径的基名和路径名:

       基名:可以理解为路径名最右边的名称;

       路径名:除基名以外的路径;

       了解之后我们使用grep取出一个路径的基名

  马哥2016全新Linux+Python高端运维班第五周作业_命令_03

    注:"\<[^/]\+" 意思为匹配非"/"开头的单词,"\<"为词首锚定,"\+"为匹配前面的条件至少一次;

          "/\?$" 意思为匹配行尾"/"符号可有可无的行,"\?"匹配前面的字符0或1次,"$"行尾锚定。

          条件"\<[^/]\+/\?$" 即匹配非"/"开头的单词并且行尾的"/"可有可无

    命令扩展:

    grep的条件中使用了很多"\"来转义,为了方便简洁,还可以使用egrep来完成,如下图;

   马哥2016全新Linux+Python高端运维班第五周作业_命令_04

       此外,我们还可以使用basename命令直接获取一个路径的基名,如下图;      马哥2016全新Linux+Python高端运维班第五周作业_find_05

      也可以使用dirname命令直接获取一个路径的路径名,如下图;

        马哥2016全新Linux+Python高端运维班第五周作业_命令_06

   扩展:取出其路径名

马哥2016全新Linux+Python高端运维班第五周作业_find_07

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

答:此匹配模式仅仅只是找出1-255之间的数字,不考虑IP等因素‘

马哥2016全新Linux+Python高端运维班第五周作业_find_08

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

答:可编写一个脚本来实现,思路是先判断每个数字是否小于255,要合理且可用必须小于255;然后通过行首行尾锚定来确定给出的IP地址位数,大于或小于4位判断为非可用IP。脚本如下:

马哥2016全新Linux+Python高端运维班第五周作业_linux_09

测试结果如下:

[root@csd ~]# alias testip="./testip.sh"   ##定义脚本别名,要永久定义请修改配置文件
[root@csd ~]# testip 254.254.254                 ##给出的IP地址只有3个,不符合可用IP要求
The ip is not reasonable
[root@csd ~]# testip 2.111.111.111               ##正确可使用IP
The ip is reasonable
[root@csd ~]# testip 199.250.3.1
The ip is reasonable
[root@csd ~]# testip 255.3.2.1                   ##有一个不小于255,不可用
The ip is not reasonable
[root@csd ~]# testip 14.0.0.1
The ip is reasonable
[root@csd ~]# testip 14.0.0.0                    ##此IP理论上可用,但是若要配置给服务器使用
The ip is not reasonable                           是不行的,故不放在考虑范围
[root@csd ~]# testip 14.0.0.1.2                  ##大于4个,不可用
The ip is not reasonable

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

答:可编写一个脚本来实现;思路为匹配任意字母或数字开头且有@符号,后跟任意字母或数字且以.com或.cn结尾的字符串。

马哥2016全新Linux+Python高端运维班第五周作业_find_10

测试结果如下

[root@csd ~]# alias testm="./testmail.sh"       ##定义脚本别名,要永久定义请修改配置文件
[root@csd ~]# testm 1111@163.com.cn
This is a email address
[root@csd ~]# testm chenshunde@qq.com
This is a email address
[root@csd ~]# testm chenshunde@jlkjsdf.com
This is a email address
[root@csd ~]# testm 2322w177@163.com
This is a email address
[root@csd ~]# testm lkjsdfkljs
This is not a email address
[root@csd ~]# testm lkjsdfkljs@.com
This is not a email address 
[root@csd ~]# testm 2322w177@163.cn
This is a email address

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

答:

马哥2016全新Linux+Python高端运维班第五周作业_find_11

 注:-user root:查找指定属主为root的文件或目录;

        -group mail:查找指定属组为mail的文件或目录;

        -ls:相当与ls -l

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

答:find命令中查找没有属主的选项为"-nouser";查找没有属组的选项为"-nogroup"

马哥2016全新Linux+Python高端运维班第五周作业_find_12

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

       为了便于演示,我们先创建2个文件文件,修改其属主和属组以及访问时间:

[root@csd test]# touch testatime1.txt         ##创建文件1
[root@csd test]# touch testatime2.txt         ##创建文件2
[root@csd test]# touch -a -t 201609011342.34 testatime1.txt       ##修改文件1访问时间 
[root@csd test]# touch -a -t 201609021521.54 testatime2.txt       ##修改文件2访问时间
[root@csd test]# chown 1001:1001 /test/testatime1.txt           ##修改文件1属主和属组为无
[root@csd test]# chown 1001:1001 /test/testatime2.txt           ##修改文件2属主和属组为无

  ##说明:所谓无属组和无属主,就是在/etc/passwd文件中无有效的UID和GID,此次试验中我的/etc/passwd文件
没有UID号为1001的用户和GID号为1001的组,所以判定为无属主或无属组。

[root@csd test]# stat testatime1.txt          ##查看文件1的状态
  File: "testatime1.txt"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空文件
Device: 802h/2050d	Inode: 395343      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-09-01 13:42:34.000000000 +0800      ##此为访问时间
Modify: 2016-09-03 01:58:41.270874816 +0800      ##此为修改时间
Change: 2016-09-03 01:59:09.409420634 +0800      
[root@csd test]# stat testatime2.txt          ##查看文件2的状态    
  File: "testatime2.txt"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空文件
Device: 802h/2050d	Inode: 395769      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-09-02 15:21:54.000000000 +0800
Modify: 2016-09-03 01:58:44.166927110 +0800
Change: 2016-09-03 01:59:28.165787554 +0800
[root@csd test]# find / \( -nouser -o -nogroup \) -atime -3 -ls   ##find可使用组合条件            
                                        "-o"表示为条件或,
                                        "-a"指明访问时间,
                                        "-ls"同等于"ls -l"
                                        使用条件组合时必须使用
                                        括号且括号需转义

结果图如下:

马哥2016全新Linux+Python高端运维班第五周作业_find_13

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

答:find命令可以根据权限来查找,使用选项为"-perm"。

[root@csd test]# find /etc -perm -222 -ls;

效果如下

马哥2016全新Linux+Python高端运维班第五周作业_find_14

"-222"匹配的权限为属主拥有写权限,属组拥有写权限,其他用户拥有写权限;

说明:find使用权限查找时有三种用法;

find /etc -perm /MODE;此用法表示任何一类(u,g,o)对象权限中的只要能一位匹配即可;

②find /etc -perm -MODE;此用法表示每一类对象都必须同时拥有为其指定的权限标准;

③find /etc -perm MODE;此用法表示精确权限匹配;

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

答:find命令可根据文件大小来查找,使用选项"-size"

      说明:    

       -size [+|-]#UNIT

       常用单位:k,M,G

       #UNIT:不加+-号,大于#-1,小于或等于#

       -#UNIT:大于0,小于或等于#-1

       +#NUIT:大于#

效果如下:

马哥2016全新Linux+Python高端运维班第五周作业_linux_15

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

答:

马哥2016全新Linux+Python高端运维班第五周作业_命令_16

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

答:为了便于演示,我们首先创建两个test文件,并修改属主;

[root@csd usr]# touch test1.txt            ##创建文件
[root@csd usr]# touch test2.txt
[root@csd usr]# chown bash: test1.txt         ##修改文件属主
[root@csd usr]# chown testbash: test2.txt
[root@csd ~]# find /usr -not \( -user root -o -user bin -o -user hadoop \) -ls   ##find支持条件查询,"-o"为或;"-not"为非。
133800    0 -rw-r--r--   1 bash     bash            0 9月  3 14:18 /usr/test1.txt
133804    0 -rw-r--r--   1 testbash testbash        0 9月  3 14:18 /usr/test2.txt
137096   12 -rwsr-xr-x   1 abrt     abrt        10096 8月 14  2013 /usr/libexec/abrt-action-install-debuginfo-to-abrt-cache

结果如下:

马哥2016全新Linux+Python高端运维班第五周作业_命令_17

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

答:

[root@csd ~]# find /etc ! -perm -222 -ls
 说明:"-222"表示查找所有权限中都有写权限的文件和目录,要查找至少有一类用户没有写权限的,我们进行取反即可;
    "!"表示非,等同于"-not"

效果如下:

马哥2016全新Linux+Python高端运维班第五周作业_linux_18

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

答:

[root@csd etc]# touch test.txt                ##新建文件
[root@csd etc]# touch test1.txt  
[root@csd etc]# chown bash: test.txt          ##修改文件属主
[root@csd etc]# chown basher: test1.txt 
[root@csd etc]# vi test.txt                   ##编辑文件,满足一周内被修改过的条件
[root@csd etc]# vi test1.txt 
[root@csd ~]# find /etc \( ! \( -user root -o -user hadoop \) \) -a \( -mtime -7 \) -ls
  3781    4 -rw-r--r--   1 basher   basher         16 9月  3 14:46 /etc/test1.txt
  3782    4 -rw-r--r--   1 bash     bash           17 9月  3 14:46 /etc/test.txt
  
[root@csd ~]# touch -m -t 201608021423.42 /etc/test1.txt   ##修改/etc/text1.txt的modify time
[root@csd ~]# stat /etc/test1.txt                          ##查看/etc/text1.txt文件状态
  File: "/etc/test1.txt"
  Size: 16        	Blocks: 8          IO Block: 4096   普通文件
Device: 802h/2050d	Inode: 3781        Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 3006/  basher)   Gid: ( 3008/  basher)
Access: 2016-09-03 14:46:38.969278255 +0800
Modify: 2016-08-02 14:23:42.000000000 +0800
Change: 2016-09-03 14:52:48.479680378 +0800

[root@csd ~]# find /etc \( ! \( -user root -o -user hadoop \) \) -a \( -mtime -7 \) -ls
  3782    4 -rw-r--r--   1 bash     bash           17 9月  3 14:46 /etc/test.txt

效果如下:

马哥2016全新Linux+Python高端运维班第五周作业_命令_19

为了更好的看出效果,我们修改下test1的modify time

马哥2016全新Linux+Python高端运维班第五周作业_linux_20