天天看點

分享平時工作中那些給力的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

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'

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

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

Designation: PCI Slot J12

Designation: PCI Slot J13

Designation: PCI Slot J14

Current Usage: Available

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

23. nmap探測遠端主機的開放端口及作業系統:

# nmap -A -T4 192.168.1.28 //此處可以為主機名,域名,或主機IP位址

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.132 clients1.google.com

203.208.46.149 mail.google.com

2.sed方法如下:

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

203.208.46.161 chatenabled.mail.google.com

3.awk方法如下:

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

4.vim中删除空行如下:

:g/^$/d

========================================

(不斷更新中...)

    本文轉自vcdog 51CTO部落格,原文連結:http://blog.51cto.com/255361/837938,如需轉載請自行聯系原作者