加深对grep命令和find命令的熟悉

1、显示当前系统上rootfedorauser1用户的默认shell

grep -E "^(root|fedora|user1)\>" /etc/passwd | cut -d: -f7
[root@localhost ~]# grep -E “^(root|fedora|user1)\>” /etc/passwd | cut -d: -f7
/bin/bash
/bin/bash
/bin/bash


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

grep -E -o "[[:alpha:]]+\(\)" /etc/rc.d/init.d/functions
[root@localhost ~]# grep -E -o “[[:alpha:]]+\(\)” /etc/rc.d/init.d/functions
str()
checkpid()
readlink()
fgrep()
loop()
loop()
run()
pidof()
daemon()
killproc()
pidfileofproc()
pidofproc()
status()
success()
failure()
passed()
warning()
stage()
success()
failure()
passed()
warning()
action()
strstr()
confirm()
dev()
file()
true()
false()
sysctl()
random()
point()
crypto()

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

echo "/var/log/messages"|grep -Eo  "[^/]+\/?$"
[root@localhost ~]# echo “/var/log/messages”|grep -Eo  “[^/]+\/?$”
messages

 echo "/var/log/messages"|grep -Eo  "^/.*/"
[root@localhost ~]# echo “/var/log/messages”|grep -Eo  “^/.*/”
/var/log/

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

ifconfig |grep -Eo "\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>"
[root@localhost ~]# ifconfig |grep -Eo “\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>”
29
192
168
3
35
192
168
3
255
255
255
255
64
1
7
6
127
1
255
1
128
1
4
4
240
240
240
240

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

[root@localhost ~]# ifconfig |grep -Eo "([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])\.([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])"
[root@localhost ~]# ifconfig |grep -Eo “([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])\.([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])”
192.168.3.35
192.168.3.255
255.255.255.0
127.0.0.1
255.0.0.0

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

grep -E "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+/.[A-Za-z]{2,6}"

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

find /var -user root -group mail -ls
[root@localhost ~]# find /var -user root -group mail -ls
1440148    4 drwxrwxr-x   2 root     mail         4096 7月 31 01:54 /var/spool/mail


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

find / -nouser -o -nogroup -ls

find / ( -nouser -o -nogroup ) -atime -3 -ls

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

find /etc -perm -222 -ls

结果全部为链接文件。


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

find /etc -size +1M -type f -ls
[root@localhost ~]# find /etc -size +1M -type f -ls
1320064 2148 -rw-r–r–   1 root     root      2198778 7月 21 18:12 /etc/gconf/gconf.xml.defaults/%gconf-tree.xml
1191199 7124 -rw-r–r–   1 root     root      7292689 7月 21 18:11 /etc/selinux/targeted/policy/policy.24
1191196 7124 -rw-r–r–   1 root     root      7292689 7月 21 18:11 /etc/selinux/targeted/modules/active/policy.kern

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

(0)
上一篇 2017-07-30 20:17
下一篇 2017-07-30 20:47

相关推荐

  • ☞RAID使用手册

    RAID使用手册 RAID演示

    Linux干货 2016-09-02
  • Centos7基于虚拟主机的Lamp配置bbs、Blog、PhpMyAdmin应用程序

    Centos7实现基于虚拟主机的各应用程序搭建: 一、配置三个基于名称的虚拟主机;      (a) discuzX      (b) wordpress      (c) https: phpMyAdmin 1.安装Lamp环境及安装mo…

    Linux干货 2016-10-09
  • PHP异常处理详解

    PHP异常处理详解          异常处理(又称为错误处理)功能提供了处理程序运行时出现的错误或异常情况的方法。   异常处理通常是防止未知错误产生所采取的处理措施。异常处理的好处是你不用再绞尽脑汁去考虑各种错误,这为处理某一类错误提供了一个很有效的方法,使编程效率大大提高。当异常被触发时,通常会发生:&…

    Linux干货 2015-06-02
  • Tomcat基于memcached会话共享

    安装两台memcache,基于tomcat做会话同步;(只对动态内容缓存,用于追踪用户会话) 前提:两个tomcat节点:172.16.100.7(tomcatA.magedu.com),172.16.100.8(tomcatB.magedu.com)两个memcached节点:172.16.100.9, 172.16.100.10一个负载均衡节点:172….

    2017-05-25
  • KeepAlived的配置及使用!

    此篇博客主要是和大家共同了解一下keepalived的功能及基础配置,以便于牢固掌握,此篇博客共分为三个部分;                ⊙ 简述keepalived的主要功能及安装环境;           &nb…

    2017-05-13
  • grep及正则表达式应用

    grep:文本过滤(模式:pattern)工具 grep家族介绍: grep:标准grep命令,支持标准的正则表达式  BRE  egrep:扩展的grep命令,支持扩展的正则表达式  ERE fgrep:快速的grep命令,不支持正则表达式 grep作用: 文本搜索工具,根据用户指定的“模式”对目标文本逐行进行匹配检查;打印匹…

    Linux干货 2016-07-10

评论列表(1条)

  • 马哥教育
    马哥教育 2017-08-04 16:14

    看来grep和find已经很好掌握了,如果再加上sed和awk就理完美了,加油。