您的位置:首页 > 运维架构 > Shell

分享平时工作中那些给力的shell命令

2012-04-25 08:34 495 查看
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://255361.blog.51cto.com/245361/836976

1.显示消耗内存/CPU最多的10个进程
ps aux | sort -nk +4 | tail
ps aux | sort -nk +3 | tail
——————————————————————————————————————————
2.查看Apache的并发请求数及其TCP连接状态
netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’

——————————————————————————————————————————
3.找出自己最常用的10条命令及使用次数(或求访问最多的ip数)
sed -e ‘s/| /\n/g’ ~/.bash_history |cut -d ‘ ‘ -f 1 | sort | uniq -c | sort -nr | head
——————————————————————————————————————————
4.日志中第10个字段表示连接时间,求平均连接时间
cat access_log |grep “connect cbp” |awk ‘BEGIN{sum=0;count=0;}{sum+=$10;count++;}END{printf(“sum=%d,count=%d,avg=%f\n”,sum,count,
sum/count)}’
——————————————————————————————————————————
5.lsof命令
lsof abc.txt 显示开启文件abc.txt的进程
lsof -i :22 知道22端口现在运行什么程序
lsof -c abc 显示abc进程现在打开的文件
lsof -p 12 看进程号为12的进程打开了哪些文件
——————————————————————————————————————————
6.杀掉一个程序的所有进程
pkill -9 httpd
killall -9 httpd
注意尽量不用-9,数据库服务器上更不能轻易用kill,否则造成重要数据丢失后果将不堪设想。
——————————————————————————————————————————
7.rsync命令(要求只同步某天的压缩文件,而且远程目录保持与本地目录一致)
/usr/bin/rsync -azvR –password-file=/etc/rsync.secrets `find . -name “*$yesterday.gz” -type f ` storage@192.168.2.23::logbackup/13.21/
——————————————————————————————————————————
8.把目录下*.sh文件改名为*.SH
find . -name “*.sh” | sed ’s/\(.*\)\.sh/mv \0 \1.SH/’ |sh
find . -name “*.sh” | sed ’s/\(.*\)\.sh/mv & \1.SH/’|sh (跟上面那个效果一样)
——————————————————————————————————————————
9.ssh执行远程的程序,并在本地显示
ssh -n -l zouyunhao 192.168.2.14 “ls -al /home/zouyunhao”
——————————————————————————————————————————
10. 直接用命令行修改密码
echo “zouyunhaoPassword” |passwd –stdin zouyunhao
——————————————————————————————————————————
ssh-keygen
ssh-copy-id -i ~/.ssh/id_rsa.pub user@remoteServer
——————————————————————————————————————————
12.以http方式共享当前文件夹的文件
$ python -m SimpleHTTPServer
在浏览器访问http://IP:8000/即可下载当前目录的文件。
——————————————————————————————————————————
13.shell段注释
:<<’echo hello,world!’
——————————————————————————————————————————
14.查看服务器序列号
dmidecode |grep “Serial Number” (查看机器其他硬件信息也可用这个命令)
——————————————————————————————————————————
15.查看网卡是否有网线物理连接
/sbin/mii-tool
——————————————————————————————————————————
16.查看linux系统或者mysql错误码表示的意思,如查看13错误码表示的意思:
perror 13
——————————————————————————————————————————
17.关于cpu个数

查看逻辑cpu个数:cat /proc/cpuinfo | grep “processor” | wc -l

查看物理cpu个数:cat /proc/cpuinfo | grep “physical id” | sort | uniq | wc -l

查看每个物理cpu的核数cores:cat /proc/cpuinfo | grep “cpu cores”

如果所有物理cpu的cores个数加起来小于逻辑cpu的个数,则该cpu使用了超线程技术。查看每个物理cpu中逻辑cpu的个数:cat /proc/cpuinfo | grep “siblings”

——————————————————————————————————————————
18.从格式不规范的日志中截取字符串
perl -ne ’print “$1\n” if /servletPath=(\S+)/g’ test.log
——————————————————————————————————————————
19. 把所有的文件名含有空格的,把空格去掉
find ./ -type f | while read line;do echo $line|grep -q " " && \mv "$line" $(echo $line|sed 's/ //g');done
------------------------------------------

20.把所有的文件夹的文件名含有空格的,把空格去掉
find ./ -type d -name '*'|while read file; do echo $file|grep -q " " && mv "$file" $(echo $file|tr -d ' '); done

当文件名的末尾以空格结束时,就不能用命令行来实现,需要使用脚本:

#!/bin/bash
IFS=$'\n'
find ./ -type f | while read line;do echo $line|grep -q " " && \mv "$line" $(echo $line|sed 's/ //g');done

-------------------------------------------

21.生成随机字符串:

# tr -dc _A-Z-a-z#$%^*-0-9 </dev/urandom |head -c8

chgSh^eJ
或者
# mkpasswd -l 8 -d 1 -c 3 -C 2 -s 2

G_ze3Hto

-------------------------------------------

22.linux统计PCI插槽数量:

[root@vcdog ~]# dmidecode |grep -1 PCI

ISA is supported

PCI is supported

PC Card (PCMCIA) is supported

--

System Slot Information

Designation: PCI Slot J11

Type: 32-bit PCI

Current Usage: In Use

--

System Slot Information

Designation: PCI Slot J12

Type: 32-bit PCI

Current Usage: In Use

--

System Slot Information

Designation: PCI Slot J13

Type: 32-bit PCI

Current Usage: In Use

--

System Slot Information

Designation: PCI Slot J14

Type: 32-bit PCI

Current Usage: Available

----------------------------------------

23. nmap探测远程主机的开放端口及操作系统:
# nmap -A -T4 192.168.1.28 //此处可以为主机名,域名,或主机IP地址
Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2010-12-28 09:46 CST

Interesting ports on bogon (192.168.1.29):

Not shown: 1677 closed ports

PORT STATE SERVICE VERSION

135/tcp open msrpc Microsoft Windows RPC

139/tcp open netbios-ssn

445/tcp open microsoft-ds Microsoft Windows XP microsoft-ds

MAC Address: 70:5A:B6:09:45:FA (Unknown)

Device type: general purpose

Running: Microsoft Windows NT/2K/XP

OS details: Microsoft Widows XP SP2

Service Info: OS: Windows

------------------------------------
24. linux下的文件去掉^M硬回车的方法:

(1)# cat test.txt |tr -d '^M' >test.new

(2).# sed -i 's/^M//g' test.txt

(3)# dos2unix test.txt

(4)在vi中用:%s/^M//g
注意:这里的“^M”要使用“CTRL-V CTRL-M”生成,而不是直接键入“^M”。

-------------------------------------
25.删除文件中的所有空行:
1.使用awk方法如下:

[root@dg ~]# cat t.txt | awk -F '' '{if($1!=null) print $0}'

203.208.46.146 www.google.com

223.208.46.146 www.google.com

203.208.46.147 www.google.com.hk

203.208.46.132 clients1.google.com

203.208.46.149 mail.google.com

2.sed方法如下:

[root@dg ~]# sed '/^$/d' t.txt

203.208.46.146 www.google.com

223.208.46.146 www.google.com

203.208.46.147 www.google.com.hk

203.208.46.132 clients1.google.com

203.208.46.149 mail.google.com

203.208.46.161 chatenabled.mail.google.com

3.awk方法如下:

[root@dg ~]# awk 'NF' t.txt

203.208.46.146 www.google.com

223.208.46.146 www.google.com

203.208.46.147 www.google.com.hk

203.208.46.132 clients1.google.com

203.208.46.149 mail.google.com

203.208.46.161 chatenabled.mail.google.com

4.vim中删除空行如下:
:g/^$/d
203.208.46.146 www.google.com

223.208.46.146 www.google.com

203.208.46.147 www.google.com.hk

203.208.46.132 clients1.google.com

203.208.46.149 mail.google.com

203.208.46.161 chatenabled.mail.google.com

------------------------------------
26.获取内存条TYPE和SPEED的信息:

# dmidecode |grep -A 16 "Memory Device"|grep -E "Speed|Type"

Type: DDR2 FB-DIMM

Speed: 667 MHz (1.5 ns)

Type: DDR2 FB-DIMM

Speed: 667 MHz (1.5 ns)

Type: DDR2 FB-DIMM

Speed: 667 MHz (1.5 ns)

Type: DDR2 FB-DIMM

Speed: 667 MHz (1.5 ns)

Type: DDR2 FB-DIMM

Type: DDR2 FB-DIMM

Type: DDR2 FB-DIMM

Type: DDR2 FB-DIMM
=================================================================
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell命令