Linux 基础练习题

Linux 测试

1、找出/proc/meminfo文件中以s开头的行,至少用三种方式忽略大小写

[root@localhost proc]# grep -i '^s' /proc/meminfo

[root@localhost proc]# sed -n -e '/^S/p' -e '/^s/p' /proc/meminfo

[root@localhost proc]# awk '/^[Ss]/{print $0}' /proc/meminfo

[root@localhost ~]# sed -nr '/^[sS]/p' /etc/passwd

2、显示当前系统上的root,centos或者user的信息


疑惑:[root@localhost init.d]# grep -Erl '(root|centos|user)' /

grep -rE '^(root|centos|user)' /etc

3、找出/etc/init.d/functions文件下包含小括号的行

[root@localhost init.d]# awk '/\(\)/{print $0}' /etc/init.d/functions

grep -E '\(|\)' /etc/init.d/functions

4、输出指定目录的基名

[root@localhost etc]# find /etc -type f | head -10 | awk -F/ '{print $NF}'

pwd | awk -F/ '{print $NF}'

5、找出网卡信息中包含的数字

[root@localhost init.d]# ip a | grep '[0-9]'

[root@localhost ~]# ip a | egrep '[0-9]+'

6、找出/etc/passwd下每种解析器的用户个数

[root@localhost init.d]# awk -F: '{print $NF}' /etc/passwd | sort | uniq -c
      1 /bin/bash
      1 /bin/sync
      1 /sbin/halt
     16 /sbin/nologin
      1 /sbin/shutdown

数组 : 
	{"bash": 10, "sh": 9, "zsh": 1}

[root@localhost ~]# awk -F: '{arr[$NF]++}END{for(i in arr){printf"%-15s | %-6d\n",i, arr[i]}}' /etc/passwd

7、过滤网卡中的ip,用三种方式实现

[root@localhost ~]# ip a | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}'

[root@localhost ~]# ip a | sed -nr '/([0-9]{1,3}\.){1,3}[0-9]{1,3}/p'



8、搜索/etc目录下,所有的.html或.php文件中main函数出现的次数

[root@localhost ~]# grep -oE 'main' `find /etc -name '*.html'` | wc -l 

9、过滤php.ini中注释的行和空行

[root@localhost ~]# find / -name 'php.*'
/usr/share/augeas/lenses/dist/php.aug
/usr/share/vim/vim74/compiler/php.vim
/usr/share/vim/vim74/ftplugin/php.vim
/usr/share/vim/vim74/indent/php.vim
/usr/share/vim/vim74/syntax/php.vim
/usr/share/swig/2.0.10/php/php.swg

# 无 php.ini

# 注释:
[root@localhost tmp]# cat 1.txt
heihei
eat tanyuan
wowowo
wowowowo
# pengyuyan




# huge
# wujing
# liyifeng
# huangshiping
# wangloufu
# linchuxian

[root@localhost tmp]# grep -vE '^#\ +|^$' 1.txt
heihei
eat tanyuan
wowowo
wowowowo

10、找出文件中至少有一个空格的行

[root@localhost tmp]# sed -nr '/\ +/p' 1.txt
eat tanyuan
# peng yuyan
# huge
# wu jing
# li yifeng
# huang shi ping
# wang loufu
# lin chuxian

11、过滤文件中以#开头的行,后面至少有一个空格

[root@localhost tmp]# grep -E '^#\ +' 1.txt
# peng yuyan
# huge
# wu jing
# li yifeng
# huang shi ping
# wang loufu
# lin chuxian

12、查询出/etc目录中包含多少个root

[root@localhost tmp]# find /etc -name '*root*' | grep -o 'root' | wc -l
3

13、查询出所有的qq邮箱

[a-zA-Z0-9-_]+@[a-z0-9]+\.(com|cn|com\.cn|net)

14、查询系统日志中所有的error

[root@localhost tmp]# grep -r 'error' /var

15、删除某文件中以s开头的行的最后一个词

[root@localhost ~]# egrep '^s' /etc/passwd|sed -r 's/[0-9a-zA-Z]+$//g'

16、删除一个文件中的所有数字

[root@localhost tmp]# sed '/[0-9]/d' 1.txt

17、显示奇数行

[root@localhost tmp]# awk 'NR%2==1{print NR,$0}' /etc/passwd
1 root:x:0:0:root:/root:/bin/bash
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

18、删除passwd文件中以bin开头的行到nobody开头的行

[root@localhost tmp]# sed '/^bin/,/^nobody/d' /etc/passwd

19、从指定行开始,每隔两行显示一次

awk -F: '{if(NR>3){num=(NR-3)%2; if(num){print $0}}}' /etc/passwd

20、每隔5行打印一个空格

[root@localhost tmp]# awk '{if(NR%5==0){print " "}print $0}' /etc/passwd

21、不显示指定字符的行

[root@localhost tmp]# grep -v 'lou' 1.txt

22、将文件中1到5行中aaa替换成AAA

[root@localhost tmp]# awk 'NR < 6{print $0}' forgive | sed 's/aaa/AAA/g'
wo
songAAA
AAA
sing
dfAAA

23、显示用户id为奇数的行

[root@localhost tmp]# awk -F: '$3 % 2 == 1{print $0}' /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
halt:x:7:0:halt:/sbin:/sbin/halt

24、显示系统普通用户,并打印系统用户名和id

[root@localhost home]# awk -F: '$3 >= 1000{print $1, $3}' /etc/passwd
elijah 1000
user01 1001
user02 1002

25、统计nginx日志中访问量(ip唯独计算)

[root@localhost ~]# grep -Ec '([0-9]{1,3}\.){3}[0-9]{1,3}' /var/log/nginx/access.log

26、实时打印nginx的访问ip

[root@localhost ~]# tail -f /var/log/nginx/access.log |awk '{print $1}'
[root@localhost ~]# tail -f /var/log/nginx/access.log | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}'

27、统计php.ini中每个词的个数

	[root@localhost ~]# grep -Eow '[0-9a-zA-Z]+' /etc/php.ini | awk '{words[$1]++}END{for (i in words){print i,words[i]}}'

28、统计1分钟内访问nginx次数超过10次的ip

#!/bin/bash
NGINX_LOG=/var/log/nginx/access.log
TIME=`date +%s`
DATE=`echo $TIME - 3600 | bc`
declare -A IP
while read line
do
	timestamp=`echo $line | grep -oE '[0-9]{4}.*T[0-9]{2}:[0-9]{2}:[0-9]{2}'`
	timestamp=`date -d "$timestamp" +%s`
	if (( $TIME >= $timestamp && $DATE <= $timestamp ));then
		ip=`echo $line| grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}'`
		number=`echo ${IP["$ip"]} | wc -L`
		[ $number -eq 0 ] && IP["$ip"]=0
		num=${IP["$ip"]}
		IP["$ip"]=`echo "$num + 1" | bc`
	fi
done < $NGINX_LOG
for i in ${!IP[*]}
do
	if (( ${IP[$i]} >= 10 ));then
		echo $i
	fi
done

29、找出nginx访问的峰值,按每个小时计算

#!/bin/bash

NGINX_LOG=/var/log/nginx/access.log

declare -A IP 

while read line
do
	timestamp=`echo $line | grep -oE '[0-9]{4}.*T[0-9]{2}:[0-9]{2}:[0-9]{2}'`
	timestamp=`date -d "$timestamp" +%Y%m%d%H`

	number=`echo ${IP["$timestamp"]} | wc -L`
	[ $number -eq 0 ] && IP["$timestamp"]=0
	num=${IP["$timestamp"]}
	IP["$timestamp"]=`echo "$num + 1" | bc`
done < $NGINX_LOG

for i in ${!IP[*]}
do
	if (( ${IP[$i]} >= 10 ));then
		echo "$i ${IP[$i]}"
	fi
done

30、统计访问nginx前10的ip

grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}' /var/log/nginx/access.log | sort | uniq -c | sort -r | head 

posted @   elijah_li  阅读(290)  评论(2编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
//一下两个链接最好自己保存下来,再上传到自己的博客园的“文件”选项中
点击右上角即可分享
微信分享提示