shell命令一行代码搞定【转】
查看文件内容-while:
cat 1.txt|while read line;do echo $line;done
while read line; do echo $line; done <./1.txt
从日志当中测试连接统计情况
while :; do tail -n 100 /app/nginx/web.log |cut -d -f 8|sort|uniq -c;sleep 5;done
统计文件代码行数:
find . -type f -name "*.sh" | xargs wc -l
删除或移动文件
find . -type f -name *.log | xargs rm
find . -type f -name "*.sh" | xargs -i mv {} /home/shell
统计sh文件中含有lst字符串并降序排列
find . -type f -iname "*.sh"| xargs grep -c lst | grep -v ":0$" | sort -t : -k 2 -nr
排名前5的文件大小
find . -type f -exec ls -l -h {} \; | sort -nr -k 5 | head -n 5
找到当前文件夹下非jpg和JPG文件内容中的ab替换为cd
find . -type f -print |grep -v ".*\.\(jpg\|JPG\)" | xargs sed -i "s/ab/cd/g"
if/else:
exist=1;ip=192.168.0.1;port=3306;if [ ${exist} -eq 1 ]; then echo ${ip}:${port};fi
for:
for ip in 192.168.1.{1..255} ;do ping ${ip} -w 1 &> /dev/null && echo ${ip} is Alive;done
for ip in `cat iplist`;do scp config/${ip}.conf $ip:/home/shell/;done
for x in `cat 1.txt`; do echo $x;mkdir dir{0..2}{0..2}_$x; done
for i in `cat iplistr.ip`; do echo $i; ssh $i "supervisorctl start nginx";done
mysql相关:
socket=`ps -ef|grep mysql|grep -v grep|grep sock|awk 'BEGIN{FS = "--"}{for (f=1; f <= NF; f+=1) {if ($f ~ /sock/) {print $f}}}'|awk -F'=' '{print $2}'|grep 3306`;echo $socket
while :; do sleep 1;netstat -lanp|grep 3306|grep ESTABLISHED|awk '{print $5}'|awk -F ':' '{print $1}'|sort|uniq|wc -l|awk 'BEGIN{a="'$(date +%H:%M:%S)'";}{printf "%s,%d\n",a,$1}' >> access_num.log;done
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?