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

转载于:https://www.cnblogs.com/paul8339/p/6160511.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值