Linux中正则表达式及find指令的使用

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

[root@centos6 ~]# grep -E "^\<(root|fedora|user1)\>" /etc/passwd
root:x:0:0:root:/root:/bin/bash
fedora:x:501:501::/users/fedora:/bin/bash

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

[root@centos6 ~]# grep -E "\<[[:alpha:]]+\(\)" /etc/rc.d/init.d/functions 
checkpid() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
success() {
failure() {
passed() {
warning() {
action() {
strstr() {
confirm() {

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

[root@centos6 ~]# echo "/etc/rc.d/init.d/functions" | grep -o "[^/]\+/\?$"
functions
[root@centos6 ~]# echo "/etc/rc.d/init.d/functions" | grep -o ".*/"
/etc/rc.d/init.d/

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

[root@centos6 ~]# ifconfig | grep -oE "([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
29
85
5
192
168
153
137
192
168
153
255
255
...

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

[root@centos6 ~]# ifconfig | grep -oE "\<([1-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])\>\.){2}\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-4])\>"
192.168.153.137
127.0.0.1

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

[root@centos6 ~]# grep -E "\<[[:alnum:]][[:alnum:]_]+[[:alnum:]]\>@[[:alnum:]]+(\.[[:alpha:]]+)+" /tmp/mail.txt 
lucklyme@163.com
liubin3@snpec.com.cn
313124417@qq.com

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

[root@centos6 ~]# find /var -user root -group mail -ls
3015344    4 drwxrwxr-x   2 root     mail         4096 Oct 24 02:41 /var/spool/mail

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

[root@centos6 ~]# find / \( -nouser -o -nogroup \) -atime -3
find: `/proc/4412/task/4412/fd/5': No such file or directory
find: `/proc/4412/task/4412/fdinfo/5': No such file or directory
find: `/proc/4412/fd/5': No such file or directory
find: `/proc/4412/fdinfo/5': No such file or directory
/etc/a

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

[root@centos6 ~]# find /etc -perm -222  -ls
1311360    0 lrwxrwxrwx   1 root     root            7 Sep 29 17:51 /etc/rc -> rc.d/rc
1310814    0 lrwxrwxrwx   1 root     root           10 Sep 29 17:51 /etc/rc6.d -> rc.d/rc6.d
1311391    0 lrwxrwxrwx   1 root     root           20 Sep 29 17:51 /etc/sysconfig/network-scripts/ifdown -> ../../../sbin/ifdown
1311408    0 lrwxrwxrwx   1 root     root            9 Sep 29 17:51 /etc/sysconfig/network-scripts/ifup-isdn -> ifup-ippp
1311402    0 lrwxrwxrwx   1 root     root           18 Sep 29 17:51 /etc/sysconfig/network-scripts/ifup -> ../../../sbin/ifup
1311396    0 lrwxrwxrwx   1 root     root           11 Sep 29 17:51 /etc/sysconfig/network-scripts/ifdown-isdn -> ifdown-ippp
...

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

[root@centos6 ~]# find /etc -size +1M -type f -exec ls -lh {} \;
-rw-r--r--. 1 root root 7.0M Sep 29 17:52 /etc/selinux/targeted/policy/policy.24
-rw-r--r--. 1 root root 7.0M Sep 29 17:52 /etc/selinux/targeted/modules/active/policy.kern
-rw-r--r--. 1 root root 1.3M Oct 21 18:34 /etc/b

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

[root@centos6 ~]# find /etc/init.d/ -perm -113 -ls
1312308    0 -rwxr-x-wx   1 root     root            0 Oct 24 04:28 /etc/init.d/test

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

[root@centos6 ~]# find /usr -not \( -user root -o -user bin -o -user hadoop \) -ls
1445596   12 -rwsr-xr-x   1 abrt     abrt         9904 Nov 23  2013 /usr/libexec/abrt-action-install-debuginfo-to-abrt-cache
[root@centos6 ~]# find /usr ! -user root -a ! -user bin -a ! -user hadoop -ls
1445596   12 -rwsr-xr-x   1 abrt     abrt         9904 Nov 23  2013 /usr/libexec/abrt-action-install-debuginfo-to-abrt-cache

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

[root@centos6 ~]# find /etc -not -perm -222 -ls
1310937    8 -rw-r--r--   1 root     root         5139 Oct 17  2013 /etc/DIR_COLORS.256color
1310799    4 drwxr-xr-x   2 root     root         4096 Sep 23  2013 /etc/chkconfig.d
1311268    4 -rw-r--r--   1 root     root          216 Nov 23  2013 /etc/sestatus.conf
1310927    4 -rw-r--r--   1 root     root         1962 Feb 17  2012 /etc/vimrc
1310757    8 -rw-r--r--   1 root     root         6455 Jan 12  2010 /etc/protocols
1311037    4 -rw-r--r--   1 root     root           45 Mar 18  2013 /etc/Trolltech.conf
1312344   12 -rw-r--r--   1 root     root        10814 Feb 21  2006 /etc/ltrace.conf
1311206    4 drwxr-xr-x   3 root     root         4096 Sep 29 17:51 /etc/pango
...

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

[root@centos6 ~]# find /etc -mtime -7 -a ! \( -user root -o -user hadoop \) -ls
1312411    0 -rw-r--r--   1 500      500             0 Oct 24 04:15 /etc/a

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

(1)
lucklymelucklyme
上一篇 2016-10-24 09:09
下一篇 2016-10-24 09:09

相关推荐

  • shell脚本基础

    shell脚本基础 基本介绍 Shell脚本与Windows/Dos下的批处理相似,也就是用各类命令预先放入到一个文件中,方便一次性执行的一个程序文件,主要是方便管理员进行设置或者管理用的。但是它比Windows下的批处理更强大,比用其他编程程序编辑的程序效率更高。换一种说法也就是,shell script是利用shell的功能所写的一个程序,这个程序是使用…

    2017-07-01
  • Shell——Linux基本命令(2)

    1.Shell Shell是Linux系统的用户界面,提供了用户与内核进行交互操作的一种接口。它接收用户输入的命令并把它送入内核去执行. shell也被称为LINUX的命令解释器(commandinterpreter). vshell是一种高级程序设计语言. 2.bash shell GNUBourne-AgainShell(bash)是GNU计划中重要的工…

    2017-07-13
  • httpd配置

    1、Centos7系统下实现httpd-2.2的安装,并分别实现prefork、worker、event等几种工作方式

    2、简述request报文请求方法和状态响应码

    3、详细描述httpd虚拟主机、站点访问控制、基于用户的访问控制、持久链接等应用配置实例

    Linux干货 2018-02-05
  • linux网络属性命令操作介绍(第十一天)

            linux网络属性常用命令有ifconfig\route\ip命令,centos7 的还是nmcli\nmtui。     网络属性的三大文件:修改DNS的文件/etc/resolv.conf  、修改网卡IP地址/etc/sysconfig/…

    Linux干货 2016-06-01
  • 高级文件系统之-LVM管理

    lvm应用 lvm的重点在于可以弹性的调整filesystem的容量! 而并非在于数据的存储效率及安全上面。 需要文件的读写效能或者是数据的可靠性是RAID所考虑的问题。 lvm:逻辑卷管理器 允许对卷进行方便操作的抽象层,包括重新设定文件系统的大小 允许在多个物理设备间重新组织文件系统 将设备指定为物理卷 用一个或者多个物理卷来创建一个卷组 物理卷是用固定…

    Linux干货 2016-09-02
  • zabbix报警信息提取

    zabbix报警信息提取     在日常的监控中,我们除了日常的zabbix操作外,我们有的时候还涉及到与其他公司进行数据对接。由于别的公司的数据对接很多时候并不是按照zabbix的数据结构(尤其是大型厂家,或是专业监控厂家,并不会直接使用zabbix,多数是自己开发或是对其他监控软件进行二次开发之类),在这种需求基础…

    Linux干货 2015-12-19

评论列表(1条)

  • 马哥教育
    马哥教育 2016-10-27 13:04

    第二个问题请要注意题目要求