当前位置:网站首页>「实用」运维新手一定不能错过的17 个技巧
「实用」运维新手一定不能错过的17 个技巧
2022-08-05 05:16:00 【技福小咖】
1、查找当前目录下所有以.tar结尾的文件然后移动到指定目录:
find . -name “*.tar” -exec mv {}./backup/ ;
*
注解:find –name 主要用于查找某个文件名字,-exec 、xargs 可以用来承接前面的结果,然后将要执行的动作,一般跟 find 在一起用的很多,find 使用我们可以延伸 -mtime 查找修改时间、-type 是指定对象类型(常见包括 f 代表文件、d代表目录),-size 指定大小,例如经常用到的:查找当前目录30天以前大于100M的LOG文件并删除。
find . -name "*.log" –mtime +30 –type f –size +100M | xargs rm –rf {};
2、批量解压当前目录下以 .zip 结尾的所有文件到指定目录:
for i in `find . –name “*.zip”–type f `
do
unzip –d $i /data/www/img/
done
*
注解:for i in (command); do … done 为 for 循环的一个常用格式,其中I为变量,可以自己指定。
3、sed常用命收集:test.txt做测试
如何去掉行首的.字符:
sed -i ‘s/^.//g’ test.txt
在行首添加一个a字符:
sed’s/^/a/g’ test.txt
在行尾添加一个a字符:
sed’s/$/a/‘ tets.txt
在特定行后添加一个c字符:
sed ‘/wuguangke/ac’ test.txt
在行前加入一个c字符:
sed’/wuguangke/ic’ test.txt
更多sed命令请查阅相关文档。
4、如何判断某个目录是否存在,不存在则新建,存在则打印信息。
if
[! –d /data/backup/];then
Mkdir–p /data/backup/
else
echo "The Directory alreadyexists,please exit"
fi
注解:if…;then …else ..fi:为if条件语句,!叹号表示反义“不存在“,-d代表目录。
5、监控linux磁盘根分区,如果根分区空间大于等于90%,发送邮件给Linux SA
(1)、打印根分区大小
df -h |sed -n '//$/p'|awk '{print $5}'|awk –F ”%” '{print $1}'
注解:awk ‘{print $5}’意思是打印第5个域,-F的意思为分隔,例如以%分隔,简单意思就是去掉百分号,awk –F. ‘{print $1}’分隔点.号。
(2)、if条件判断该大小是否大于90,如果大于90则发送邮件报警
while sleep 5m
do
for i in `df -h |sed -n '//$/p' |awk '{print $5}' |sed 's/%//g'`
do
echo $i
if [ $i -ge 90 ];then
echo “More than 90% Linux of disk space ,Please LinuxSA Check Linux Disk !” |mail -s “Warn Linux / Parts is $i%”
[email protected]
fi
done
done
6、统计 Nginx 访问日志,访问量排在前20 的 ip地址:
cat access.log |awk '{print $1}'|sort|uniq -c |sort -nr |head -20
*
注解:sort排序、uniq(检查及删除文本文件中重复出现的行列 )
7、sed另外一个用法找到当前行,然后在修改该行后面的参数:
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
Sed冒号方式 sed -i ‘s:/tmp:/tmp/abc/:g’test.txt意思是将/tmp改成/tmp/abc/。
8、打印出一个文件里面最大和最小值:
cat a.txt |sort -nr|awk ‘{}END{print} NR==1′
cat a.txt |sort -nr |awk ‘END{print} NR==1′
这个才是真正的打印最大最小值:sed ‘s/ / /g’ a.txt |sort -nr|sed -n ’1p;$p’
9、使用snmpd抓取版本为v2的cacti数据方式:
snmpwalk -v2c -c public 192.168.0.241
10、修改文本中以jk结尾的替换成yz:
sed -e ‘s/jk$/yz/g’ b.txt
11、网络抓包:Tcpdump
tcpdump -nn host 192.168.56.7 and port 80 抓取56.7通过80请求的数据包。
tcpdump -nn host 192.168.56.7 or ! host 192.168.0.22 and port 80 排除0.22 80端口!
tcp/ip 7层协议物理层–数据链路层-网络层-传输层-会话层-表示层-应用层。
12、显示最常用的20条命令:
cat .bash_history | grep -v ^# | awk ‘{print $1}’ | sort | uniq -c | sort -nr | head-20
13、写一个脚本查找最后创建时间是3天前,后缀是*.log 的文件并删除。
find . -mtime +3 -name "*.log" |xargs rm -rf {} ;
14、写一个脚本将某目录下大于100k的文件移动至/tmp下。
find . -size +100k -exec mv {} /tmp ;
15、写一个防火墙配置脚本,只允许远程主机访问本机的80端口。
iptables -F
iptables -X
iptables -A INPUT -p tcp --dport 80 -j accept
iptables -A INPUT -p tcp -j REJECT
或者
iptables -A INPUT -m state --state NEW-m tcp -p tcp --dport 80 -j ACCEPT
16、写一个脚本进行 Nginx 日志统计,得到访问 IP 最多的前10个(nginx日志路径:
/home/logs/nginx/default/access.log)。
cd /home/logs.nginx/default
sort -m -k 4 -o access.logok access.1 access.2 access.3 .....
cat access.logok |awk '{print $1}'|sort -n|uniq -c|sort -nr |head -10
17、替换文件中的目录
sed 's:/user/local:/tmp:g' test.txt
或者
sed -i 's//usr/local//tmp/g' test.txtRUST 复制 全屏
更多相关大咖视频课程请在苹果App Store 或各安卓市场下载“技福小咖App”学习。
边栏推荐
- 【论文阅读-表情捕捉】High-quality Real Time Facial Capture Based on Single Camera
- 【Pytorch学习笔记】10.如何快速创建一个自己的Dataset数据集对象(继承Dataset类并重写对应方法)
- AIDL detailed explanation
- 读论文- pix2pix
- HQL statement execution process
- flink部署操作-flink on yarn集群安装部署
- 如何停止flink job
- ECCV2022 | RU & Google propose zero-shot object detection with CLIP!
- 《基于机器视觉的输电线路交叉点在线测量方法及技术方案》论文笔记
- 【MySQL】数据库多表链接的查询方式
猜你喜欢
AWS 常用服务
6k+ star,面向小白的深度学习代码库!一行代码实现所有Attention机制!
Machine Learning (1) - Machine Learning Fundamentals
Mysql-连接https域名的Mysql数据源踩的坑
CVPR 2020 - 频谱正则化
【数据库和SQL学习笔记】4.SELECT查询2:排序(ORDER BY)、聚合函数、分组查询(GROUP BY)
CVPR best paper winner Huang Gao's team from Tsinghua University presented the first dynamic network review
【数据库和SQL学习笔记】10.(T-SQL语言)函数、存储过程、触发器
Facial Motion Capture 调研
CVPR2020 - 自校准卷积
随机推荐
[Redis] Resid的删除策略
SQL(1) - Add, delete, modify and search
【Pytorch学习笔记】9.分类器的分类结果如何评估——使用混淆矩阵、F1-score、ROC曲线、PR曲线等(以Softmax二分类为例)
读论文- pix2pix
flink项目开发-flink的scala shell命令行交互模式开发
轻松接入Azure AD+Oauth2 实现 SSO
flink项目开发-配置jar依赖,连接器,类库
HQL statement execution process
《基于机器视觉测量系统的工业在线检测研究》论文笔记
The University of Göttingen proposed CLIPSeg, a model that can perform three segmentation tasks at the same time
flink yarn-session的两种使用方式
Web Component-处理数据
[Database and SQL study notes] 9. (T-SQL language) Define variables, advanced queries, process control (conditions, loops, etc.)
[Pytorch study notes] 10. How to quickly create your own Dataset dataset object (inherit the Dataset class and override the corresponding method)
Tensorflow2 与 Pytorch 在张量Tensor基础操作方面的对比整理汇总
服务网格istio 1.12.x安装
[Pytorch study notes] 9. How to evaluate the classification results of the classifier - using confusion matrix, F1-score, ROC curve, PR curve, etc. (taking Softmax binary classification as an example)
门徒Disciples体系:致力于成为“DAO世界”中的集大成者。
SQL (2) - join window function view
BroadCast Receiver(广播)详解