加深对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)
N27_huiyezhichengN27_huiyezhicheng
上一篇 2017-07-30 20:17
下一篇 2017-07-30 20:47

相关推荐

  • 20161019第5天作业

    1、只显示/root下的隐藏文件 ls -d '.'* 2、只显示/etc下的目录 ls -d */ ls -l |grep '^d' ls -F | grep "/$"  &nb…

    Linux干货 2016-10-20
  • LVM 逻辑卷管理器

    1、什么是LVM:PV、PE、VG、LV的意义    LVM:Logical Volume Manager(逻辑卷管理器),可以将多个物理分区整合成看起来像一个磁盘一样,并可随意增加或减少逻辑卷大小 dm:device mapper,将一个或多个底层块设备组织成一个逻辑设备的模块; /dev/mapper/VG_NAME-LV_NAME …

    Linux干货 2016-09-19
  • 第二周作业

    1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。     文件管理类命令:cp、mv、rm     (1)cp:复制命令      单源复制(源文件为文本文件)    &n…

    Linux干货 2017-01-18
  • shell精讲

    shell精讲 shell变量        定义变量时,变量名不加美元符号($,PHP语言中变量需要) 注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程语言都不一样。同时,变量名的命名须遵循如下规则: 首个字符必须为字母(a-z,A-Z)。 中间不能有空格,可以使用下划线(_)。 不能使…

    Linux干货 2016-08-21
  • 马哥教育网络班21期+第6周课程练习

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

    Linux干货 2016-08-15
  • LINUX的初探

    一般而言,计算机的组成遵循着“冯诺依曼体系结构”。这种结构的体现实质上是一种存储程序的原理,把程序本身当作数据来对待,程序和该程序处理的数据用同样的方式来存储。这种结构的要点是:计算机的数制采用二进制;计算机应该按照程序顺序执行。

    2018-03-04

评论列表(1条)

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

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