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

相关推荐

  • VIM使用总结

    VIM使用总结:     I,vim + [option] +filename 其中option为m时,文件以只读模式打开。                               &n…

    Linux干货 2017-06-05
  • 压缩与归档

    一、压缩     压缩:根据一定算法将数据以更加节省空间的形式存放。下面来看一下Linux常见的压缩工具:     1、gzip/gunzip         后缀名:.gz   …

    Linux干货 2015-05-04
  • linux中用ACL实现灵活的权限管理

    ACL是什么?? ACL英文原意是Access Control List(访问控制列表).它能够实现灵活的权限管理,除了文件的所有者,所属组和其他人,设置相应的权限外,ACL允许你给任何用户或是用户组设置任何文件/目录的访问权限(注意的是有些不支持数字模式的权限给定) ACL有什么用?? 作为UGO权限管理的补充,acl有GUO办不到或者是难以办到的功能 &…

    Linux干货 2016-08-05
  • sed编辑器

    sed: Stream EDitor, 行编辑器;  用法: sed [option]… ‘script’ inputfile… script: ‘地址命令‘  常用选项: -n:不输出模式中的内容至屏幕; -e: 多点编辑; -f /PATH/TO/SCRIPT_F…

    Linux干货 2015-07-06
  • N26 第四周博客作业

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 演示:     [root@47d523181d25 /]# cp -r /etc/skel/ /home/tuser1     [root@47d523…

    Linux干货 2017-02-25
  • 磁盘配额

    磁盘配额 1 启用磁盘配额 首先创建新的分区 /dev/sd5,并创建文件系统。 [root@local ~]# mkfs.ext4 /dev/sda5 由于xfs 不磁盘配额能成功,这里使用ext4. 然后把/dev/sda5分区挂载到 /home [root@local ~]# mount /dev/sda5 /home [root@local ~]# …

    Linux干货 2017-05-02

评论列表(1条)

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

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