第五周着重练习扩展正则元字符及find命令

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

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

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

    [root@centos7 /]# grep  -E -o "[_[:alnum:]]+\(\)"  /etc/rc.d/init.d/functions
    checkpid()
    __pids_var_run()
    __pids_pidof()
    daemon()
    killproc()
    pidfileofproc()
    pidofproc()
    status()
    echo_success()
    echo_failure()
    echo_passed()
    echo_warning()
    update_boot_stage()
    success()
    failure()
    passed()
    warning()
    action()
    strstr()
    is_ignored_file()
    is_true()
    is_false()
    apply_sysctl()

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

[root@centos7 /]#  echo "/etc/rc.d/init.d/functions" |grep -E -o "[^/]+$"
functions

[root@centos7 /]#  echo "/etc/rc.d/init.d/functions" |grep -E -o  "^/.*/"
/etc/rc.d/init.d/

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

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

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

grep -E -o "\<([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-5])\>.\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>"

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

grep -E -o “\<[a-z0-9A-Z._%+-]+@[a-z0-9A-Z.-]+\.[a-zA-Z]{2,6}\>”

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

[root@centos7 /]# find /var/ -user root -a -group mail
/var/spool/mail
/var/spool/mail/root

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

find / -nouser -o -nogroup -atime 3

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

find /etc/ -perm -020

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

find /etc/ -size +1M -type f

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

find /etc/init.d/ -perm -111 -perm -002

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

[root@centos7 /]# find /usr ! \( -user root -o -user bin -o -user hadoop \)
/usr/share/polkit-1/rules.d
/usr/libexec/abrt-action-install-debuginfo-to-abrt-cache

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

[root@centos7 /]# find /usr ! \( -user root -o -user bin -o -user hadoop \)
/usr/share/polkit-1/rules.d
/usr/libexec/abrt-action-install-debuginfo-to-abrt-cache
[root@centos7 /]# find /usr -not \( -user root -o -user bin -o -user hadoop \)
/usr/share/polkit-1/rules.d
/usr/libexec/abrt-action-install-debuginfo-to-abrt-cache
[root@centos7 /]# find /usr ! \( -user root -o -user bin -o -user hadoop \)
/usr/share/polkit-1/rules.d
/usr/libexec/abrt-action-install-debuginfo-to-abrt-cache
[root@centos7 /]# find /etc/ -not -perm /222
/etc/pki/ca-trust/extracted/java/cacerts
/etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
/etc/pki/ca-trust/extracted/pem/email-ca-bundle.pem
/etc/pki/ca-trust/extracted/pem/objsign-ca-bundle.pem
/etc/gshadow
/etc/openldap/certs/password
/etc/shadow
/etc/ld.so.conf.d/kernel-3.10.0-327.el7.x86_64.conf
/etc/ld.so.conf.d/kernel-3.10.0-327.36.3.el7.x86_64.conf
/etc/udev/hwdb.bin
/etc/gshadow-
/etc/dbus-1/system.d/cups.conf
/etc/shadow-
/etc/lvm/profile/cache-mq.profile
/etc/lvm/profile/cache-smq.profile
/etc/lvm/profile/command_profile_template.profile
/etc/lvm/profile/metadata_profile_template.profile
/etc/lvm/profile/thin-generic.profile
/etc/lvm/profile/thin-performance.profile
/etc/pam.d/cups
/etc/machine-id
/etc/sudoers

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

find /etc -mtime -7 ! \( -user root -o -user hadoop \)

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

(0)
N24_JerryN24_Jerry
上一篇 2016-12-13
下一篇 2016-12-13

相关推荐

  • shell脚本编写-1练习题

    1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小 #!/bin/bash echo “the hostname is:`hostname`” echo “the ip address is:`ifconfig | sed –n ‘2p’ |sed…

    Linux干货 2016-08-15
  • 苦上半年时间

    这是一种享受

    Linux干货 2016-08-08
  • httpd服务归纳:httpd简介与基本安装

    一、httpd 简介  1. httpd 历史: httpd daemon 最早诞生于1995年二月, 伊利诺伊大学香槟分校的美国国家超级计算应用中心(NCSA),由Rob McCool开发。当这个项目完成后,Rob 老兄离开RCSA以后,这个项目组就解散了,这个项目也随之终止。 但是,由Brian Behlendorf 和 Cliff …

    Linux干货 2015-05-07
  • N24 第6周作业

    请详细总结vim编辑器的使用并完成以下练习题 1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; 2、复制/boot/grub/grub.conf至/tmp目录中,删除/tmp/grub.conf文件中的行首的空白字符; 3、删除/tmp/rc.sysinit文件…

    Linux干货 2016-11-30
  • 测试Markdown

    测试markdown是否可用

    Linux干货 2018-03-10
  • N26-第六周作业

    vim编辑器的使用 编辑模式:默认模式编辑模式–>输入模式;i:insert,在光标所在处输入;a:append,在光标所在处后方输入o:在光标所在处下方打开的一个新行;光标处在行首;I:在光标所在行的行首输入A:在光标所在处的行尾输入;O:在光标所在处的上方打开一个新行; 输入模式–>编辑模式ESC 编辑模式&#8211…

    Linux干货 2017-03-13

评论列表(1条)

  • 马哥教育
    马哥教育 2016-12-23 00:48

    赞~几个例子都不错,继续加油~