加深对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
下一篇 2017-07-30

相关推荐

  • N25-第四周作业

    1、复制/etc/skel目录为/home/tuse1,要求/home/tuse1及其内部文件的属组和其它用户均没有任何访问权限。   cp -r /etc/skel /home/tuser1;chmod go= /home/tuse1 2、编辑/etc/group文件,添加组hadoop    vi /et…

    Linux干货 2016-12-27
  • Nginx代理MogileFS并实现负载均衡和高可用

    Nginx代理MogileFS并实现负载均衡和高可用 MogileFS nginx 负载均衡 前言 实验拓扑 实验环境 实验步骤 配置MogileFS 配置Nginx 总结 前言 上篇文章我们了解分布式系统和MogileFS的基本使用, 但是那样的架构是有问题的, 本篇文章我们来了解一下如何使用nginx-mogilefs-module-…

    大数据运维 2016-05-05
  • MariaDB数据库基于SSL实现远程访问和主从复制

    MariaDB数据库基于SSL实现远程访问和主从复制 实验环境 系统环境:Centos6.5 数据库版本:5.5.36-MariaDB-log MariaDB Server 虚机数量:2 方案实施: 1,配置CA;并为node1和node2生成key和证书 2,在node1和node2上安装Mariadb 3,配置节点1为MariaDB主节点 4,配置节点2…

    Linux干货 2016-05-18
  • 第二周作业

    一Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。 which命令:显示(shell)命令全部的路径 which [option] programmame […] –skip-alias:忽略别名   whereis命令:搜索二进制程序,源码,和帮助手册页的命令 whereis [option] na…

    Linux干货 2017-02-18
  • Linux运维不想早死的方法 一

    Linux运维不想早死的方法 一 为了提高工作效率,减少工作时间,爱惜生命,远离辐射;务必做好以下几点: 1,一定用快捷键         这里简单的说下几个常用的快捷按键。 Ctrl + l     清屏,相当于clear命令。 Ctrl…

    Linux干货 2017-03-26
  • Linux中的权限修改指令及正则表达式

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其他用户均没有任何访问权限 [root@centos6 ~]# cp -r /etc/skel/ /home/tuser1 [root@centos6 ~]# ls -la&…

    Linux干货 2016-10-24

评论列表(1条)

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

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