8.5_Linux习题练习和作业

课堂练习题

1、找出ifconfig命令结果中本机的所有IPv4地址

# ifconfig | grep -oE "([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])){3}"

blob.png

2、查出分区空间使用率的最大百分比值

# df |grep "/dev/sd" | tr -s " " ":" |cut -d: -f1,5 |sort

blob.png

3、查出用户UID最大值的用户名、UID及shell类型

# cat /etc/passwd | sort -n -t: -k3 | tail -1 | cut -d: -f1,3,7

blob.png

4、查出/tmp的权限,以数字方式显示

# stat /tmp |grep 'Access' | head -1 | tr " " ":" | cut -d: -f3 |tr -cd "[[:digit:]]"

blob.png

5、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序

[root@centos7 ~]# netstat -nt | grep "tcp"|tr -s " " ":"|cut -d: -f6 |sort |uniq -c

blob.png

6、显示/proc/meminfo文件中以大小s开头的行;(要求:使用两种方式)

(1)grep "^[sS]" /proc/meminfo

blob.png

(2)grep -i "^s" /proc/meminfo

blob.png

7、显示/etc/passwd文件中不以/bin/bash结尾的行

[root@centos7 ~]# grep -v "/bin/bash$" /etc/passwd

8、显示用户rpc默认的shell程序

grep "^rpc\>" /etc/passwd | cut -d: -f1,7

blob.png

9、找出/etc/passwd中的两位或三位数

[root@centos7 ~]# grep -E "\b([1-9][0-9]|[1-9][0-9]{2})\b" /etc/passwd

10、显示/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面存非空白字符的行

[root@centos7 ~]# grep "^\s[^\s].*" /etc/grub2.cfg

11、找出“netstat -tan”命令的结果中以‘LISTEN’后跟任意多个空白字符结尾的行

[root@centos7 ~]# netstat -tan | grep "LISTEN\s*$"

blob.png

12、添加用户bash、testbash、basher以及nologin(其shell为/sbin/nologin),而后找出/etc/passwd文件中用户名同shell名的行

[root@centos7 ~]# grep -E "(^[[:alnum:]]+)\b.*/\1{1}$" /etc/passwd

blob.png

13、显示三个用户root、mage、wang的UID和默认shell

[root@centos7 ~]# grep -E "^(root|mage|wang)\>" /etc/passwd | cut -d: -f1,3,7

blob.png

14、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行

[root@centos7 ~]# egrep "\w+\(\).*" /etc/rc.d/init.d/functions

blob.png

15、使用egrep取出/etc/rc.d/init.d/functions中其基名

[root@centos7 ~]# echo "/etc/rc.d/init.d/functions" | egrep "\w*\.?\w*$"

blob.png

16、统计以root身份登录的每个远程主机IP地址的登录次数

[root@centos7 ~]# last | grep "root" | egrep -o "([0-9]|[0-9]{2}|[0-9]{3})(\.([0-9]|[0-9]{2}|[0-9]{3})){3}"|sort|uniq -c

blob.png

17、利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255

[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]

18、显示ifconfig命令结果中所有IPv4地址

[root@centos7 ~]# ifconfig | egrep -o "([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])){3}"

blob.png

作业题

1、取本机ip地址

[root@centos7 ~]# ifconfig|head -2|tail -1|grep -oE "([0-9]|[1-9][0-9]|[1-9][0-9]{2})(\.([0-9]|[1-9][0-9]|[1-9][0-9]{2})){3}"|head -1

blob.png

2、取各分区利用率的数值

[root@centos7 ~]# df -h | grep "/dev/sd" | tr -s " " ":" | cut -d: -f1,5 | sort

blob.png3、统计/etc/init.d/functions 文件中每个单词出现的次数,并按频率从高到低显示

[root@centos7 ~]# grep -o "\b[[:alnum:]]*\b" /etc/init.d/functions | sort | uniq -c | sort -n

blob.png

4、/etc/rc.d/init.d/functions或/etc/rc.d/init.d/functions/"  取目录名

[root@centos7 ~]# echo "/etc/rc.d/init.d/functions" | egrep -o "/.*/"

blob.png

5、正则表达式表示身份证号

egrep "\b[1-9]{2}[0-9]{15}[0-9xX]\b"

6、正则表达式表示手机号

egrep "\b1[3-8][0-9]{9}\b"

7、正则表达式表示邮箱

[root@centos7 ~]# egrep -i "\w*@[[:alnum:]]*\.[[:alpha:]]{1,3}\.?[[:alpha:]]{,2}"

8、正则表达式表示QQ号

 [root@centos7 ~]# egrep "\b[1-9][0-9]{4,9}\b"

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

(0)
上一篇 2016-08-10 10:27
下一篇 2016-08-10 10:27

相关推荐

  • 马哥教育21期网络班—第四周课程+练习

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 [root@localhost ~]#cp -rf /etc/skel /home/tuser1[root@localhost ~]#chmod -R g…

    Linux干货 2016-07-16
  • 作业练习题汇总

    需要用到的命令补充  文件通配符                                            …

    2017-07-22
  • Linux中Vim编辑器的使用

    Linux中Vim编辑器的使用 Vim功能介绍 vim是一款功能丰富而强大的文本编辑器,支持语法高亮,支持多窗口编辑,是vi文本编辑器的增强版。如果在CentOS7.2中如果是选择最小化安装,其默认是没有安装vim rpm包的,需要自行安装,可以使用yum工具进行在线安装。  vim至今已经走过了25个春秋,现在8.0版的已经发布了。  …

    Linux干货 2016-12-02
  • 1、安装配置介绍

    http://note.youdao.com/yws/public/redirect/share?id=b90faa657ee452edb27ab1a13fc7b0bc&type=false

    Linux干货 2016-08-08
  • 学习笔记–权限管理

    文件系统的权限管理 1、Linux系统下每个目录或文件的权限对象主要分为:属主(u)、属组(g)、其他用户(o),针对每类访问对象都定义了三种权限:读(r)、写(w)、执行(x),这三种权限针对目录和文件的作用各不相同,以下将做详细说明: 对于文件来说: 读(r)权限:可以使用文本查看类工具读取文件内容 写(w)权限:可以修改文件内容,但不能删除文件本身 执…

    Linux干货 2016-08-05
  • 网络文件系统–NFS

        网络文件系统(英语:Network File System,缩写为NFS)是一种分布式文件系统协议,最初由Sun Microsystems公司开发,并于1984年发布。其功能旨在允许客户端主机可以像访问本地存储一样通过网络访问服务器端文件。     NFS和其他许多协议一…

    Linux干货 2017-08-14