加深对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

相关推荐

  • 系统无法正常启动的解决之道

    先来看下配置文件的格式: [root@centos6 ~]# uname -r 2.6.32-642.el6.x86_64 [root@centos6 ~]# cat /boot/grub/grub.conf  # Note that you do&…

    Linux干货 2016-09-19
  • Linux启动之grub详解,故障排除,自建linux

    概述     上篇我们以CentOS6为例介绍了一下系统启动流程,本篇将承接上篇,详细的介绍一下系统启动流程中的grub,以及系统启动过程中的各种故障的排除,以及利用现有内核自己构建一个能够正常启动的简单Linux系统。具体分为一下几个部分:     1、grub相关概念详解 &…

    Linux干货 2016-09-13
  • N26-第五周

    1、显示当前系统上root、fedora或user1用户的默认shell; [root@localhost test]# cat /etc/passwd | grep -E “^(root|fedora|user1)\>” |cut -d: -f  1,7 root:/bin/bash user1:/bin/bash …

    Linux干货 2017-02-26
  • N25-第四周博客

      复制/etc/ske1目录为/home/tuser1,要求/home/tuser1及其内部文件的属组合其他用户均没有任何访问权限。            2,编辑/etc/group文件,添加组hadoop.。    &nbs…

    Linux干货 2016-12-22
  • 互联网的实现

    互联网的实现 全世界几十亿台电脑,连接在一起,两两通信。上海的某一块网卡送出信号,洛杉矶的另一块网卡就收到了,两者实际上根本就不知道对方的物理位置,这难道不是一件很神奇的事情吗? 互联网的**核心**是一些列协议,总称为“互联网协议”(Internet Protocol Suite)。它们对电脑如何连接和组网做出了详细的规定。 互联网协议入门 概述 五层模型…

    Linux干货 2017-05-05
  • 重定向和管道

    1.Linux给程序提供三种I/O设备 标准输入-0    默认接受来自键盘的输入 标准输出-1    默认输出到终端窗口 标准错误-2    默认输出到终端窗口 在dev下有个fd设备,打开的文件都有一个fd:file dexcriptor 文件描述符 I/O重定向:…

    2017-07-20

评论列表(1条)

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

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