马哥教育网络班21期+第五周课程练习

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

~]# egrep ^[[:space:]] /boot/grub/grub.conf

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

~]# egrep ^#[[:space:]]+[^[:space:]]+ /etc/rc.d/rc.sysinit

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

~]# netstat -tan | egrep "LISTEN[[:space:]]+"

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

#!/bin/bash
#
for i in bash testbash basher nologin;do
      if ! id $i &> /dev/null;then
          if [ $i == nologin ];then
          useradd -s /sbin/nologin $i
          else
          useradd $i
          fi
     else
        echo "$i exist"
          if [ $i == nologin ];then
            userdel -r $i
            useradd -s /sbin/nologin $i
            echo "$i reinstitute"
          fi
       fi

done
  egrep "^([[:alnum:]]+\>).*\1$"  /etc/passwd

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

~]# egrep "^root\>|^fedora\>|^user1\>" /etc/passwd | cut -d: -f7

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

~]# grep  "\<[[:alpha:]]\+()" /etc/init.d/functions

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

    扩展:取出其路径名

取其基名
~]#  echo /etc/rc.d/rc3.d/K01smartd | grep -o "[^/]\+$"
取其路径名
~]# echo /etc/rc.d/rc3.d/K01smartd | grep -o "[/][^/]\+.*[/]"|cut -d"/" -f1-4

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

~]# ifconfig |egrep  "\<[1-9][0-9]\>|\<[0-9]\>|\<[1][0-9][0-9]\>|\<[2][0-4][0-9]\>|\<[2][5][0-5]\>"

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

~]# ifconfig |egrep -o "[1-2]?[0-9]{,2}\.[1-2]?[0-9]{,2}\.[1-2]?[0-9]{,2}\.[1-2]?[0-9]{,2}"
192.168.0.108
192.168.0.255
255.255.255.0
192.168.160.160
192.168.160.255
255.255.255.0
127.0.0.1
255.0.0.0

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

[root@linux tmp]# cat mail.txt 
zhangsan@163.com
lisi123@123.com
123456@yahoo.com
wangwusf.hiya
huohuo.huo.huo
huo99@mage.com
[root@linux tmp]# egrep  "^[[:alnum:]]+@[[:alnum:]]+\.[[:alnum:]]+$"  ./mail.txt 
zhangsan@163.com
lisi123@123.com
123456@yahoo.com
huo99@mage.com

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

~]# find /var -user root -a -group mail -ls

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

[root@linux tmp]# find / -type f -nouser -a -nogroup -ls 
261162    0 -rw-rw-r--   1 3007     3007            0 Aug  6 20:52 /tmp/test.txt
261169    0 -rw-rw-r--   1 3007     3007            0 Aug  6 20:52 /tmp/kkk

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

[root@linux tmp]# find / -nouser -a -nogroup -a -atime -3
/tmp/test.txt
/tmp/kkk

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

~]# find / -type f -perm -222 -ls

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

[root@linux ~]# find /etc -type f -size +1M |xargs ls -lh
-rw-r--r--. 1 root root 1.3M Aug  4 22:22 /etc/gconf/gconf.xml.defaults/%gconf-tree.xml
-rw-r--r--. 1 root root 1.1M Apr 24  2015 /etc/pki/tls/certs/ca-bundle.trust.crt
-rw-r--r--. 1 root root 8.0M Aug  4 08:54 /etc/selinux/targeted/modules/active/policy.kern
-rw-r--r--. 1 root root 8.0M Aug  4 08:54 /etc/selinux/targeted/policy/policy.24

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

~]# find /etc/init.d/ -perm -113 -ls
393283    0 -rwxr-xrwx   1 root     root            0 Aug  6 21:15 /etc/init.d/test.txt

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

~]# ~]# find /usr  -not -user root -a -not -user bin -a -not -user hadoop -ls

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

~]# find /etc -not -perm -222 -ls

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

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

原创文章,作者:微,如若转载,请注明出处:http://www.178linux.com/30301

(0)
微
上一篇 2016-08-08
下一篇 2016-08-08

相关推荐

  • linux路由转发(软路由)实验

        一。实验网络拓扑             (R1,R2由 Linux 虚拟机添加双网卡做软路由实现)              &n…

    Linux干货 2016-09-05
  • Linux基础知识之帮助命令的使用

    帮助命令的使用:   内部命令     help COMMAND   外部命令:     (1)命令自带简要格式的使用帮助        COMMOND –help     (2)使用手册 manual   &nbsp…

    Linux干货 2016-07-07
  • lvs的基本概念及基础配置

    一、知识整理 1、Session:在计算机中,尤其是在网络应用中,称为“会话控制、时域”。Session 对象存储特定用户会话所需的属性及配置信息。这样,当用户在应用程序的 Web 页之间跳转时,存储在 Session 对象中的变量将不会丢失,而是在整个用户会话中一直存在下去。当用户请求来自应用程序的 Web 页时,如果该用户还没有会话,则 Web 服务器将…

    Linux干货 2016-11-11
  • 文件管理初探(inode理解)及管道和IO重定向

    第一部分    文件管理初探   1、Linux系统上各主要目录的简介           /  根,所有文件的起点     ├── bin    存…

    Linux干货 2016-07-29
  • linux之/home目录转移分区。

    linux之/home目录转移分区。     I,基本思路,将/home目录的数据转移到新的分区,再将/home目录挂载到新的分区。     II,添加硬盘,进行分区,添加新硬盘不重启机器识别命令echo “- – -”  /sys/class/scsi_host/host#/scan,然后进行 …

    Linux干货 2017-06-19
  • shell 脚本基础作业

    1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小 #!/bin/bash :<<EOF 显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小 EOF Host_name=`hostna…

    Linux干货 2016-08-15

评论列表(1条)

  • 马哥教育
    马哥教育 2016-08-08 17:02

    写的很好,排版也很棒,加油,匹配ip地址不对