第五周着重练习扩展正则元字符及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 12:29
下一篇 2016-12-13 14:09

相关推荐

  • Linux任务计划,周期性任务执行详解

    Linux任务计划,周期性任务执行 概述 本章将为大家介绍一些任务计划和周期性任务计划执行相关内容任务计划的分类主要有由两种    未来的某时间点执行一次某任务:at,batch    周期性运行某任务:crontab具体分为三个方面来说明:    1、at计划任务    2、c…

    Linux干货 2016-09-27
  • linux系统启动之破坏与修复

    linux系统启动之破坏与修复:      通过这些实验,了解linux启动流程和相关文件,同时也预防系统发生问题能更快解决,而不用重装或者耽误时间:      实验:破坏MBR 446     dd if=/dev/zero of=/dev/sda bs=1 count=446…

    Linux干货 2017-03-30
  • 计划任务

    任务计划Linux任务计划、周期性任务执行未来的某时间点执行一次任务:atbatch:系统自行选择空闲时间去执行此处指定的任务周期性运行某任务:cron     at任务at命令:at [option] TIME常用选项:-V 显示版本信息:-l: 列出指定队列中等待运行的作业;相当于atq-d: 删除指定的作业;相当于atrm-c: 查…

    Linux干货 2017-08-28
  • LINUX-用户和组管理

    LINUX用户和组管理 linux用户分类:Username/UID      管理员:root,UID 0号,拥有最高权限;负责系统的启功、停止,安装新软件,               增加、删除用户,保证系统正常运转;     &nb…

    2017-05-30
  • 显示列表和复制文件的各种实例

    显示/var目录下所有以l开头,以一个小写字母结尾,且中间至少出现一位数字(可以有其它字符)的文件或目录。 [root@localhost tmp]# ls -d /var/l*[[:lower:]] 显示/etc目录下,以任意一个数字开头,且以非数字结尾的文件或目录。 [root@localhost tmp]# ls -d /etc/[0-9]*[^0-9…

    Linux干货 2018-03-01
  • linux cluster—-lvs

    一. Linux Cluster:           Cluster:计算机集合,为解决某个特定问题组合起来形成的单个系统;      Linux Cluster类型:      &nbs…

    Linux干货 2016-10-29

评论列表(1条)

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

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