[awk]命令使用记录-find-查找文件修改权限
1.密码第一行和最后一行互换
tr 参考:http://www.cnblogs.com/wajika/p/6374043.html
head /etc/passwd >passwd.txt
cat /root/passwd.txt | awk -F "[:]" '{a=$1;$1=$NF;$NF=a;print}'|tr " " ":" #空格换成冒号打印
awk -F '[:]' -vOFS=":" '{print$NF,$6,$5,$4,$3,$2,$1}' passwd.txt
awk -F '[:]' '{print $NF,$5,$4,$3,$2,$1}' passwd.txt |tr" " ":"
awk -F '[:]' '{a=$1;$1=$NF;$NF=a;print}' passwd.txt|tr " " ":"
sed -r 's#(^.*)(:x.*:)(.*$)#\3\2\1#g' passwd.txt
awk -F '[:]' '{print $NF":"$5":"$4":"$3":"$2":"$1}' passwd.txt
cat passwd.txt | awk -F : '{a=$1;$1=$NF;$NF=a;print}'| tr " " ":"
2.ddos检测 连接数
netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n #端口把f1改成f4
netstat -antup | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
netstat -antup | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n | awk '{print $2"="$1}'
3.sed 编辑替换内容
sed -i "s/Server=127.0.0.1/Server=192.168.42.251/" /etc/zabbix/zabbix_agentd.conf
sed '/1/{s/123/miller/}' abc.txt #编辑某一行替换
sed '1s/aa/AA/g' test.txt
sed 's#11/22#/77/88#g' index.html
sed 's*/11/22*/77/88*g' index.html
4.查找文件并打包
find /tmp/wwwdir -mtime -5 -a -mtime +2 -exec cp {} /mnt/xuegod1802 \;
find /root/zsltest/ -name "zsl-web" -exec tar -cvf out.tar {} +
find /root/zsltest/ -name "zsl-web" -exec tar -cvf out.tar {} \;
find /root/zsltest/ -name "zsl-web" -print0 |tar -cvf out.tar --null -T -
传递给 find 命令的 -print0 选项处理特殊的文件名。--null 和 -T 选项告诉 tar 命令从标准输入/管道读取输入。也可以使用 xargs 命令:
find /root/zsltest/ -name "zsl-web" | xargs tar cfvz out.tar
find /tlvnksc -name "fabu.sh" -exec chmod 700 {} \;
find /tlvnksc -name "*.jar" -exec chmod 644 {} \;
5.查看端口占用的进程
lsof -i:80 netstat -tunlp|grep 80
6.随机密码生成
[root@localhost zsl-web]# openssl rand -base64 4 H4SWCw== [root@localhost zsl-web]# openssl rand -hex 4 0c86990d
date | base64
date | md5sum
7.常用信息查询
筛选cpu型号几核 cat /proc/cpuinfo | grep "model name" | awk -F : '{print $2}' | uniq -c 筛选内存占比 [root@doupo ~]# free -m | grep -i mem | awk '{print $3/$2*100"%"}' 硬盘使用超过35%的 [root@doupo ~]# df -Th|awk -F"[ %]+" '/^\/dev/{if($6>35)print $1,$6"%"}' cpu超的进程 top -bn 1 -i -c |awk '{ if (NR > 6) print }' |awk '{ if ($9 > 10) print $1,$9}' cpu 使用率查询 [root@doupo ~]# top -bn 1 -i -c | grep Cpu | awk '{ print $2 "%" }' 让cpu超100 没有测试 for i in `seq 1 $(cat /proc/cpuinfo |grep "physical id" |wc -l)`; do dd if=/dev/zero of=/dev/null & done
8.rm -rf 不释放空间
rm -rf #不 释放空间 lsof | grep deleted kill -9 41895
9.awk整理
ifconfig enp2s0f1 | grep netmask | awk '{print $2}' echo "one two three four" | awk '{print $(NF-2)}' #打印倒数第3列 awk -F: '$3<10{print $1"\t"$NF}' /etc/passwd #注:awk 最外面使用了单引号'' ,里面都使用双引号“” free -m | grep -i mem | awk '{print $3/$2*100"%"}' #内存比例 awk -F: '(NR>=3&&NR<=6){print NR,$0}' /etc/passwd #NR行号$0整行 route -n | sed 1d #不打印第一行 awk -F: '{printf "%s\n",$1}' /etc/passwd #换行打印 netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn | head -2
10.系统运行情况脚本
vi disk.sh #!/bin/bash menu(){ cat<<eof ++++系统管理工具箱++++++ h 显示命令帮助 f 显示磁盘分区 d 显示磁盘挂载 m 查看内存使用 u 查看系统负载 q 退出程序 eof } #显示磁盘分区 Fdisk(){ df -Th } #显示硬盘容量 Diskinfo(){ df -Th|awk '/^\/dev/{print $1"\t\t\t"$3}' } #查看内存占用前10进程 Memtop10(){ ps aux --sort -pmem|head -11|awk '{print $1"\t"$2"\t"$4"\t"$NF}' } #查看CPU占用前10进程 Cputop10(){ ps aux --sort -pcpu|head -11|awk '{print $1"\t"$2"\t"$4"\t"$NF}' } #显示帮助信息 menu while true do read -p "In a choose(h for help):" OP case $OP in h) clear menu;; f) echo "显示磁盘分区" Fdisk;; d) echo "显示磁盘容量" Diskinfo;; m) echo "内存占用前10进程" Memtop10;; u) echo "CPU占用前10进程" Cputop10;; q) exit;; *) echo "输入错误" continue esac done
11.ip信息过滤
ip a | grep "scope global" | egrep -o "([0-9]{1,3}.){3}[0-9]{1,3}/[0-9]{1,2}" ip route | grep src | egrep -o "([0-9]{1,3}.){3}[0-9]{1,3}"
ip a | grep "scope global" | egrep -o "((2[0-4][0-9]|25[0-5]|[01]?[0-9][0-9]?)\.){3}(2[0-4][0-9]|25[0-5]|[01]?[0-9][0-9]?)"
ip a | grep "scope global" | egrep -o "((2[0-4][0-9]|25[0-5]|1[0-9][0-9]|[1-9][0-9]|[1-9])\.){3}(2[0-4][0-9]|25[0-5]|1[0-9][0-9]|[1-9][0-9]|[1-9])"
ip a s ens192 | sed -n '3p' |sed 's/^.*inet //g'|sed 's#/.*$##g'
[root@node62 ~]# ip a s ens192 | sed -n '3p' |sed -r 's#^.*net ([0-9.]+)/.*$#\1#g'
192.168.1.62

sed 's/.*([0-9]+)$/\1/' myfile.txt
替换命令 s/A/B/
B中的 \1 指的就是正则A中的第一个group,即A中第一个小括号里匹配到的内容。
[root@node62 ~]# ip a | grep "scope global" | awk -F "[ /]+" '{print $3}' 192.168.1.62
参考
https://www.cnblogs.com/kongxianghai/p/3995463.html
满血拉二胡 残血到处浪

浙公网安备 33010602011771号