工作中實用給力的shell命令

標籤shell  分類: shell

 
分享平時工作中那些給力的shell命令---(Notice: 藍色部分爲在網友孤風顛影基礎上新增加內容

 

 

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的核數corescat /proc/cpuinfo | grep “cpu cores”
如果所有物理cpucores個數加起來小於邏輯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

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章