实用shell
转:http://www.csdn123.com/html/blogs/20130427/7007.htm
1.查看地理位置:
命令:curl -s 'http://geoiplookup.wikimedia.org/' | sed 's/^.*=\s*//'
结果:{"city":"Wuhan","country":"CN","lat":"30.583300","lon":"114.266701","IP":"61.183.225.66","netmask":"17"}
2.批量下载文件:
curl -O http://www.site.com/img/image[001-175].jpg
3.将第五列中的foo替换为bar
awk '{gsub("foo","bar",$5)}1' file
4.计算字符串的md5码
echo -n "password"|md5sum|awk '{print $1}'
5.获得可用内存:
memnum=$(awk '{ print $2 }' /proc/meminfo |head -n1); echo "$memnum / 1024 / 1024" | bc -l
6.年月日转换为秒
date -ud "1970/01/01 00:29:36" +%s
7.每行后面增加空行
sed G file.txt
8.排除某几列
awk '{$1=$3=""}1' file
9.禁止任何人删除文件
find . -maxdepth 1 -type f -exec chmod +a "everyone deny delete" {} \;
10.pv命令显示进度:
显示压缩进度:pv file | gzip > file.gz
显示解包的进度:pv <example.tgz|tar zxf -
显示打包进度:tar cf - /usr |pv|gzip >/tmp/usr.tgz
11.获取后缀名函数:
filext () { echo ${1##*.}; }
12.图片分辨率调整:
convert -resize 1024x600 res-127-2011-07-05-14-02-02.jpg small-image.jpg
13.根据cpu排序进程:
ps -ef --sort=-%cpu
14.获取cpu数量:
grep -c '^$' /proc/cpuinfo
15.大小写转换函数:
upper() { echo ${@^^}; }
lower() { echo ${@,,}; }
16.查看开启的端口:
lsof -Pi | grep LISTEN
netstat -plntu
17.打印某文件的某一行:
tail -n +<N> <file> | head -n 1
sed -n '3p' aaa.txt
sed '3!d' aaa.txt
18.循环创建文件夹和文件:
for i in `seq 100`; do mkdir f${i}; touch ./f${i}/myfile$i ;done
19.获取机器内存频率:
/usr/sbin/dmidecode | perl -lne 'print $1 if /Current\s+Speed:\s+(\d+\s+MHz)/'
20.获取bios信息:
dmidecode --type 0
21.获取本机ip:
ifconfig|grep 'inet addr:'|grep 'Bcast'|awk '{print $2}'|awk -F : '{print $2}'
ifconfig eth0 |grep "inet addr"| cut -f 2 -d ":"|cut -f 1 -d " "
22.将windows文件转换为linux文件:
sed 's/.$//' test.sh >test2.sh
tr -d "\r" < dos.txt > linux.txt
23. 批量调整图片分辨率:
mogrify -resize 600x800! *.jpg
24.将文件名大写转为小写:
rename 'y/A-Z/a-z/' *
25.利用文字生成透明背景图片:
convert -background none -pointsize 55 label:"`whoami`" me.png
如果报错:convert: unable to read font `/usr/share/fonts/default/TrueType/arial.ttf'
则安装truetype字体包:msfonts.tbz
wget http://www.osresources.com/files/centos-windows-fonts/msfonts.tbz
mkdir /usr/share/fonts/default/TrueType
tar xvjpf msfonts.tbz -C /usr/share/fonts/default/TrueType/
26.将多个文件输出到一个文件:
cat $(file *.txt | grep ASCII | awk -F: {'print $1'})>3.txt
27.图片合并:
composite -geometry 296x296+60+70 res-127-2011-07-05-14-02-02.jpg res-41-2011-02-22-16-00-09.jpg image24.jpg
将res-127-2011-07-05-14-02-02.jpg附着在res-41-2011-02-22-16-00-09.jpg上,附着大小为296.,位置为x:60,y:70;
28.获取视频时长:
tmp=$(date +%N);ffmpeg -i res-4-2011-02-22-15-57-27.mp4 &> _test_$tmp;grep Duration _test_$tmp | awk -F'[, ]' '{print $4}' ;rm -f _test_$tmp ;
29.设置系统时间:
date -s '16:52:00 2011-11-10'
30.将命令执行结果生成图片:命令生成图片
ifconfig | convert label:@- ip.png
31.查询最近修改的文件:
find . -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort
32.创建目录并进入该目录:
mkdir /home/foo/doc/bar && cd $_
33.删除某文件超过一定字符数(255)的行:
sed -n '/^.\{255\}/!p' test.txt
34.验证网站服务器:
curl -Is http://www.google.com | grep -E '^Server'
35.列出目录名:
ls -l | grep ^d | sed 's:.*\ ::g'
36.只压缩目录结构:
find . -type d|xargs tar rf dirstructure.tar --no-recursion
37.列出链接本机的主机:
netstat -nut | sed '/ESTABLISHED/!d;s/.*[\t ]\+\(.*\):.*/\1/' | sort -u
38.获取服务器文件大小:
curl -s http://www.asp300.com/adimg/ewaydns.gif |wc -c
39.获取当前目录父目录全路径:
dirname `pwd`
40.将字符串追加到文件:
sed -i '1s/^/text to prepend\n/' file1
41.统计文件行数:
awk 'END {print NR}' /etc/passwd
42.获取mac地址:
ifconfig | awk '/HWaddr/ { print $1, $5 }'