分享平時工作中那些給力的shell命令

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 ` [email protected]::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
——————————————————————————————————————————
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章