马哥教育网络班20期+第5周课程练习

马哥教育网络班20期+第5周课程练习

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

# grep -E "^[[:space:]]+" /boot/grub/grub.conf

实例演示
[root@C664BSLab ~]# grep -E "^[[:space:]]+" /boot/grub/grub.conf
   root (hd0,0)
   kernel /vmlinuz-2.6.32-573.el6.x86_64 ro root=/dev/mapper/vg_c664bslab-lv_root rd_NO_LUKS.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=vg_c664bslab/lv_swap  KEYBOARDTYPE=pc KEYTABLE=us rd_LVM_LV=vg_c664bslab/lv_root rd_NO_DM rhgb quiet
   initrd /initramfs-2.6.32-573.el6.x86_64.img

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

# grep -E "^#[[:space:]]+[^[:space:]]+" /etc/rc.d/rc.sysinit

实例演示
[root@C664BSLab ~]# grep -E "^#[[:space:]]+[^[:space:]]+" /etc/rc.d/rc.sysinit
# /etc/rc.d/rc.sysinit - run once at boot time
# Taken in part from Miquel van Smoorenburg's bcheckrc.
# Check SELinux status
# Print a text banner.
# Only read this once.

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

# netstat -tan | grep -E "LISTEN[[:space:]]*$"

实例演示
[root@C664BSLab ~]# netstat -tan | grep -E "LISTEN[[:space:]]*$"
tcp        0      0 0.0.0.0:50048               0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      
tcp        0      0 :::54890                    :::*                        LISTEN      
tcp        0      0 :::111                      :::*                        LISTEN      
tcp        0      0 :::80                       :::*                        LISTEN      
tcp        0      0 :::22                       :::*                        LISTEN      
tcp        0      0 ::1:631                     :::*                        LISTEN      
tcp        0      0 ::1:25                      :::*                        LISTEN

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

# useradd bash
# useradd testbash
# useradd basher
# useradd -s /sbin/nologin nologin
# grep -E "^([[:alpha:]]+\>).*\1$" /etc/passwd

实例演示
[root@C664BSLab ~]# useradd bash
[root@C664BSLab ~]# useradd testbash
[root@C664BSLab ~]# useradd basher
[root@C664BSLab ~]# grep -E "^([[:alpha:]]+\>).*\1$" /etc/passwd
sync: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:502:502::/home/bash:/bin/bash
nologin:x:505:505::/home/nologin:/sbin/nologin

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

# grep -E "^(root|fedora|user1)\>" /etc/passwd | cut -d: -f1,7

实例演示
[root@C664BSLab ~]# grep -E "^(root|fedora|user1)\>" /etc/passwd | cut -d: -f1,7
root:/bin/bash
fedora:/bin/bash
user1:/bin/bash

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

# grep -E "\<[[:alpha:]]+\>\(\)" /etc/rc.d/init.d/functions

实例演示
[root@C664BSLab ~]# grep -E -o "\<[[:alpha:]]+\>\(\)" /etc/rc.d/init.d/functions
checkpid()
daemon()
killproc()
pidfileofproc()
pidofproc()
status()

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

    扩展:取出其路径名

# echo "/etc/rc.d/init.d/functions" | grep -E -o "[^/][[:alpha:]]+/?$"
# echo "/etc/rc.d/init.d/functions" | grep -E -o "^(/.*/)"    

实例演示
[root@C664BSLab ~]# echo "/etc/rc.d/init.d/functions" | grep -E -o "[^/][[:alpha:]]+/?$"
functions
[root@C664BSLab ~]# echo "/etc/rc.d/init.d/functions" | grep -E -o "(/.*/)" 
/etc/rc.d/init.d/

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

# ifconfig | grep -E -o "\<([1-9][0-9]?|[1][0-9][0-9]|2[0-4][0-9]|25[0-5])\>"

实例演示
[root@C664BSLab ~]# ifconfig | grep -E -o "\<([1-9][0-9]?|[1][0-9][0-9]|2[0-4][0-9]|25[0-5])\>"
29
192
168
225
136
192
168
225

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

# ifconfig | grep -E -o "([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])"

实例演示
[root@C664BSLab ~]# ifconfig | grep -E -o "([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])"
192.168.225.136
192.168.225.255
255.255.255.0
127.0.0.1
255.0.0.0

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

# echo "This is a test e-mail address test2_Ecd@test.com" | grep -E -o "[[:alnum:]_]+\@[[:alpha:]]+\.[[:alpha:]]+"

实例演示
[root@C664BSLab ~]# echo "This is a test e-mail address test2_Ecd@test.com" | grep -E -o "[[:alnum:]_]+\@[[:alpha:]]+\.[[:alpha:]]+"
test2_Ecd@test.com

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

# find /var -user root -group mail -ls

实例演示
[root@C664BSLab ~]# find /var -user root -group mail -ls
1049287    4 drwxrwxr-x   2 root     mail         4096 Jul  5 08:45 /var/spool/mail

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

# find / \( -nouser -o -nogroup \) -ls

实例演示
[root@C664BSLab ~]# find / \( -nouser -o -nogroup \) -ls
262145    4 drwx------   2 508      508          4096 Jul  5 10:08 /home/tuser1
262149    0 -rw-rw-r--   1 508      508             0 Jul  5 10:08 /home/tuser1/abc.txt
1049732    0 -rw-rw----   1 508      mail            0 Jul  5 10:08 /var/spool/mail/tuser1

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

# find / \( -nouser -o -nogroup \) -a -atime -3 -ls

实例演示
[root@C664BSLab ~]# find / \( -nouser -o -nogroup \) -a -atime -3 -ls
262145    4 drwx------   2 508      508          4096 Jul  5 10:08 /home/tuser1
262148    4 -rw-r--r--   1 508      508           176 May 11 07:21 /home/tuser1/.bash_profile
262150    4 -rw-------   1 508      508            25 Jul  5 10:08 /home/tuser1/.bash_history
262146    4 -rw-r--r--   1 508      508           124 May 11 07:21 /home/tuser1/.bashrc
262147    4 -rw-r--r--   1 508      508            18 May 11 07:21 /home/tuser1/.bash_logout
262149    0 -rw-rw-r--   1 508      508             0 Jul  5 10:08 /home/tuser1/abc.txt
1049732    0 -rw-rw----   1 508      mail            0 Jul  5 10:08 /var/spool/mail/tuser1

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

# find /etc -perm -222 -ls

实例演示
find /etc -perm -222 -ls
524956    0 lrwxrwxrwx   1 root     root           15 Jun 27 23:21 /etc/rc.sysinit -> rc.d/rc.sysinit
524545    0 lrwxrwxrwx   1 root     root           30 Jun 27 22:59 /etc/fonts/conf.d/40-nonlatin.conf -> ../conf.avail/40-nonlatin.conf
524552    0 lrwxrwxrwx   1 root     root           30 Jun 27 22:59 /etc/fonts/conf.d/65-nonlatin.conf -> ../conf.avail/65-nonlatin.conf

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

# find /etc -type f -size +1M -exec ls -lh {} \;

实例演示
[root@C664BSLab ~]# find /etc -type f -size +1M -exec ls -lh {} \;
-rw-r--r--. 1 root root 8.1M Jun 27 23:22 /etc/selinux/targeted/modules/active/policy.kern
-rw-r--r--. 1 root root 8.1M Jun 27 23:22 /etc/selinux/targeted/policy/policy.24

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

# find /etc/init.d -perm -113 -ls

实例演示
[root@C664BSLab ~]# find /etc/init.d -perm -113 -ls
526070    0 lrwxrwxrwx   1 root     root           11 Jun 27 23:20 /etc/init.d -> rc.d/init.d

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

# find /usr -not \( -user root -o -user bin -o -user hadoop \) -ls

实例演示
[root@C664BSLab ~]# find /usr -not \( -user root -o -user bin -o -user hadoop \) -ls
2628995   12 -rwsr-xr-x   1 abrt     abrt        10296 May 12 04:43 /usr/libexec/abrt-action-install-debuginfo-to-abrt-cache

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

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

实例演示
[root@C664BSLab ~]# find /etc -not -perm -222 -ls
524289   12 drwxr-xr-x 105 root     root        12288 Jul  5 10:08 /etc
525293    4 drwxr-xr-x   2 root     root         4096 Jun 27 23:21 /etc/postfix
525306    8 -rw-r--r--   1 root     root         5113 Nov 10  2015 /etc/postfix/master.cf
525302   12 -rw-r--r--   1 root     root         9904 Nov 10  2015 /etc/postfix/generic
525309   16 -rw-r--r--   1 root     root        12494 Nov 10  2015 /etc/postfix/virtual

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

# find /etc -mtime -7 -a -not \( -user root -o -user hadoop \) -ls

实例演示
[root@C664BSLab ~]# find /etc -mtime -7 -a -not \( -user root -o -user hadoop \) -ls
525528    0 -rw-r--r--   1 user1    user1           0 Jul  5 10:53 /etc/abc.txt

原创文章,作者:N20-背旅,如若转载,请注明出处:http://www.178linux.com/23433

(0)
上一篇 2016-07-12 11:17
下一篇 2016-07-12 11:17

相关推荐

  • python env seup

    ffffffffffffffffffffffffffffffffffffff

    2018-03-12
  • python 10第二周博客作业

    1.取磁盘利用率:cut -d命令 df|tr –s  ‘ ’   ‘%’ |cut –d “%” –f5正则表达式:df -h |grep “^/dev/sd” |grep -o “[[:digit:]]\+%”|grep -o “[[:digit:]]\+”2. paste 合并两个…

    Linux干货 2018-03-17
  • 马哥教育网络班第19期+第9周课程练习

    1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现; 2、写一个脚本     (1) 获取当前主机的主机名,保存于hostname变量中;     (2) 判断此变量的值是否为localhost,如果是…

    Linux干货 2016-09-19
  • Hadoop新增datanode与SecondaryNameNode

    无论是新增namenode还是SecondaryNameNode,操作方法大致相同 一、如果新增datanode,需要保证namenode能无密码ssh连接到新datanode 如果是添加SecondaryNameNode,则需保证其能无密码ssh连接至各datanode和namenode,namenode也需要能无密码连接到新SecondaryNameNo…

    Linux干货 2015-03-08
  • Linux的发展史和Linux的终端类型

    Linux的发展史和Linux的终端类型 前言:    随着时代的发展,Linux所占的市场份额越来越大,目前几乎百分之九十的服务器市场都被Linux占据。接下来我们来聊聊Linux的发展史。 简单的说:Linux操作系统是1998年的8月芬兰的一个叫Linus Torvalds的大学生写出来的一个类minix的系统。 具体分析: Lin…

    Linux干货 2016-10-17
  • 逻辑卷

    逻辑卷如果分区分区类型id8epvs查看现有的物理卷 或者pvdisplay(详细)pvcreate /dev/sdd1 /dev/sdb 把硬盘和分区编程物理卷vgs查看现有的卷组 或者vgdisplay (详细)vgcreate -s 16M vg0 /dev/sd{d1,b} 创建vg0卷组把sdd1和sdb加入卷组指定PE为16Mlvcreate -…

    Linux笔记 2018-04-30

评论列表(1条)

  • 马哥教育
    马哥教育 2016-07-12 13:45

    写的很好,排版也很棒,加油