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