列舉平時常用但不易記住的Linux命令,工欲善其事,必先利其器,掌握了這些常用的工具命令就會在工作學習中得心應手。每掌握一個新的命令或者選項,可能你就會發現新的天地,加之不同命令的組合(管道)和重定向,必會受益匪淺。
top
top 命令用於動態檢視系統的程序資訊。top的輸出分為上部的綜述資訊及下部的任務列表資訊。
➜ top
top - 16:42:10 up 28 days, 1:33, 5 users, load average: 0。54, 0。83, 0。95
Tasks: 383 total, 1 running, 331 sleeping, 0 stopped, 0 zombie
%Cpu(s): 3。1 us, 1。3 sy, 0。0 ni, 91。9 id, 3。6 wa, 0。0 hi, 0。2 si, 0。0 st
KiB Mem : 16207712 total, 6220896 free, 8091128 used, 1895688 buff/cache
KiB Swap: 16557052 total, 11717176 free, 4839876 used。 7169268 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
9807 storm 20 0 1240256 132344 39220 S 1。3 0。8 2:16。22 sublime_text
16901 storm 20 0 3563824 80832 11560 S 1。0 0。5 147:04。65 mysql-workbench
29806 storm 20 0 1493396 166300 43012 S 1。0 1。0 227:12。95 chromium-browse
11794 storm 20 0 42080 3820 2968 R 0。7 0。0 0:02。40 top
8 root 20 0 0 0 0 I 0。3 0。0 59:51。17 rcu_sched
223 root 20 0 0 0 0 S 0。3 0。0 10:42。25 jbd2/sda2-8
249 root 0 -20 0 0 0 I 0。3 0。0 0:01。81 kworker/3:1H
5355 storm 25 5 7293144 1。083g 6828 S 0。3 7。0 2:00。27 java
9243 mysql 20 0 2289984 68336 3656 S 0。3 0。4 435:16。33 mysqld
10821 storm 20 0 3565716 135776 6840 S 0。3 0。8 6:37。23 java
19014 storm 20 0 3115864 150332 48100 S 0。3 0。9 193:51。63 chromium-browse
綜述資訊包括: (1)系統執行的時間,和uptime命令輸出一樣,比如這裡的 16:42:10 up 28 days, 1:33 表示當前時間是 16:42:10 ,運行了28天1小時33分。
(2)會話個數,比如這裡的 5 users,表示當前有5個會話連線,可以用who命令看到具體情況。
(3)系統負載,系統在過去1分鐘,5分鐘,15分鐘的平均負載,如果平均負載值超過了CPU的個數(可以使用nproc或者 cat /proc/cpuinfo 命令看)就說明負載很重了,要重視。
(4)處於執行、睡眠、停止、殭屍狀態的程序數量統計。
(5)CPU使用情況,us,sy分別表示CPU在使用者空間和核心空間花費的時間,用於執行修改了nice值得程序花費的時間會顯示在ni這裡,id表示CPU空閒時間,wa表示等待IO完成消耗的時間,hi,si分別表示CPU用於處理硬中斷,軟終端所用時間,st表示在虛擬化環境中,一個虛擬CPU等待真實CPU的時間(因為Hypervisor在服務另一個虛擬處理器),CPU資源被Hypervisor偷去了(Steal Time)。
(6)記憶體及交換分割槽使用情況,總共有多少,使用了多少,空閒多少。
接下來看任務列表資訊欄,表頭的各個欄位含義為:
PIDUSERPRNIVIRTRESSHR%MEMS%CPUTIME+COMMAND程序ID程序所屬使用者排程優先順序nice值虛擬記憶體大小使用的物理記憶體共享記憶體使用記憶體的比例程序狀態CPU份額CPU時間(秒)具體的命令
常用1
:根據資源使用情況排序程序列表
top 之後可以透過以下鍵實現對應的排序,預設是降序,可以使用R反轉。也可以使用 top -o %CPU 指定排序的列。
M : 根據記憶體使用量排序
P : 根據CPU使用排序
N : 根據程序ID號排序
T : 根據執行時長排序
常用2
:列出執行緒
透過H鍵來控制是否列出執行緒,列出執行緒後綜述資訊中也會多出執行緒的統計資訊。也可以執行top命令時指定選項 top -H 。
常用3
:列出命令的完整路徑
透過c鍵來控制是否列出命令對應的完整路徑。
常用4
:列出特定使用者的程序
透過u鍵,可以輸入使用者名稱,列出從屬的程序資訊。也可以執行top命令時指定選項,如 top -u root 。
常用5
:根據各種條件過濾程序
透過O鍵,可以各種條件來一層層過濾得到自己關注的程序,比如 COMMAND=java 得到命令包含java的程序,%CPU>3。0 得到CPU佔用大於3的程序。
常用6
:檢視一個程序對應的執行緒 top -H -p PID
ps
ps命令用於輸出當前系統的程序資訊,和top的持續輸出不同,ps輸出的是一個快照。
常用
:列出所有程序
➜ ~ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 15:22 ? 00:00:01 /sbin/init splash
root 2 0 0 15:22 ? 00:00:00 [kthreadd]
root 4 2 0 15:22 ? 00:00:00 [kworker/0:0H]
root 6 2 0 15:22 ? 00:00:00 [mm_percpu_wq]
storm 5892 21695 0 17:28 pts/36 00:00:00 less 3D_spatial_network。txt
……
-e:所有程序,等同於 -A。 -f:可以看到更多的資訊,比如完整的程式名。
lsof
lsof(list open files)可以列出系統開啟的檔案描述符(包括普通檔案,網路埠號,管道等),也十分強大。在下面的匹配模式中,^開頭都表示非,相反的模式。
常用1
:列出以特定字串開頭的程序開啟的檔案情況
➜ sudo lsof -c mysql
[sudo] password for storm:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 9243 mysql cwd DIR 8,2 4096 38283055 /var/lib/mysql
mysqld 9243 mysql rtd DIR 8,2 4096 2 /
mysqld 9243 mysql txt REG 8,2 24895464 46140873 /usr/sbin/mysqld
mysqld 9243 mysql DEL REG 0,18 49257 /[aio]
mysqld 9243 mysql DEL REG 0,18 49256 /[aio]
mysqld 9243 mysql DEL REG 0,18 49255 /[aio]
常用2
:特定程序開啟的檔案
➜ ~ lsof -p 9305
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 9305 storm cwd DIR 8,2 4096 56235542 /home/storm
java 9305 storm rtd DIR 8,2 4096 2 /
java 9305 storm txt REG 8,2 7414 13239133 /home/storm/dev/idea-IU-182。3684。101/jre64/bin/java
java 9305 storm mem REG 8,2 31312 46146073 /usr/lib/x86_64-linux-gnu/libnotify。so。4。0。0
java 9305 storm mem REG 8,2 65012836 46268507 /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt。jar
常用3
:列出網路連線資訊,top -i [i],i用於給定要匹配的地址模式
➜ ~ lsof -i
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 6983 storm 32u IPv6 362378 0t0 UDP *:38183
java 6983 storm 33u IPv6 363325 0t0 TCP *:http-alt (LISTEN)
java 9305 storm 21u IPv4 3089042 0t0 TCP localhost:37877->localhost:35762 (ESTABLISHED)
。。。。。
➜ ~ lsof -i tcp
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 6983 storm 33u IPv6 363325 0t0 TCP *:http-alt (LISTEN)
java 9305 storm 21u IPv4 3089045 0t0 TCP localhost:37877->localhost:35766 (ESTABLISHED)
。。。。
➜ ~ lsof -i udp
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 6983 storm 32u IPv6 362378 0t0 UDP *:38183
chromium- 19014 storm 118u IPv4 2483939 0t0 UDP *:mdns
➜ ~ lsof -i tcp:37877
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 9305 storm 21u IPv4 3090619 0t0 TCP localhost:37877->localhost:35772 (ESTABLISHED)
java 9305 storm 261u IPv4 2323357 0t0 TCP *:37877 (LISTEN)
java 9451 storm 77u IPv6 3089068 0t0 TCP localhost:35772->localhost:37877 (ESTABLISHED)
➜ ~ lsof -i udp:38183
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 6983 storm 32u IPv6 362378 0t0 UDP *:38183
常用4
:恢復誤刪除的檔案
一個檔案如果正在被程序使用(沒有close),誤刪後,可以被恢復。一種常見的情況是一個正在被使用的日誌檔案,被刪除了,但是控制代碼沒有被釋放,佔用的磁碟空間並沒有得到釋放,此時可以先恢復,然後 cat ‘’ > /path/to/log,或者直接kill掉程序。
PID=27878的程序此時使用了less打開了檔案3D_spatial_network。txt。
➜ ~ lsof -c less
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
less 27878 storm cwd DIR 8,2 4096 56235542 /home/storm
less 27878 storm rtd DIR 8,2 4096 2 /
less 27878 storm txt REG 8,2 170728 45875271 /bin/less
less 27878 storm mem REG 8,2 2981280 46137392 /usr/lib/locale/locale-archive
less 27878 storm mem REG 8,2 1868984 44307626 /lib/x86_64-linux-gnu/libc-2。23。so
less 27878 storm mem REG 8,2 167240 44307045 /lib/x86_64-linux-gnu/libtinfo。so。5。9
less 27878 storm mem REG 8,2 162632 44307624 /lib/x86_64-linux-gnu/ld-2。23。so
less 27878 storm 0u CHR 136,20 0t0 23 /dev/pts/20
less 27878 storm 1u CHR 136,20 0t0 23 /dev/pts/20
less 27878 storm 2u CHR 136,20 0t0 23 /dev/pts/20
less 27878 storm 3r CHR 5,0 0t0 13 /dev/tty
less 27878 storm 4r REG 8,2 20673913 56235819 /home/storm/3D_spatial_network。txt
現在刪除3D_spatial_network。txt檔案後,可以在lsof中看到。
➜ ~ rm 3D_spatial_network。txt
➜ ~ lsof -c less
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
less 27878 storm cwd DIR 8,2 4096 56235542 /home/storm
less 27878 storm rtd DIR 8,2 4096 2 /
less 27878 storm txt REG 8,2 170728 45875271 /bin/less
less 27878 storm mem REG 8,2 2981280 46137392 /usr/lib/locale/locale-archive
less 27878 storm mem REG 8,2 1868984 44307626 /lib/x86_64-linux-gnu/libc-2。23。so
less 27878 storm mem REG 8,2 167240 44307045 /lib/x86_64-linux-gnu/libtinfo。so。5。9
less 27878 storm mem REG 8,2 162632 44307624 /lib/x86_64-linux-gnu/ld-2。23。so
less 27878 storm 0u CHR 136,20 0t0 23 /dev/pts/20
less 27878 storm 1u CHR 136,20 0t0 23 /dev/pts/20
less 27878 storm 2u CHR 136,20 0t0 23 /dev/pts/20
less 27878 storm 3r CHR 5,0 0t0 13 /dev/tty
less 27878 storm 4r REG 8,2 20673913 56235819 /home/storm/3D_spatial_network。txt (deleted) // 這裡
➜ ~ ls -alh 3D_spatial_network。txt
ls: cannot access ‘3D_spatial_network。txt’: No such file or directory
可以根據PID和FD(檔案描述符)來恢復檔案。
➜ ~ cp /proc/27878/fd/4 3D_spatial_network。txt
➜ ~ ls -alh 3D_spatial_network。txt
-rw-rw-r—— 1 storm storm 20M Aug 29 15:54 3D_spatial_network。txt
netstat
netstat是檢視網路埠情況的必備命令。
常用:列出所有的TCP,UDP端口占用情況
➜ ~ netstat -atunp
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127。0。0。1:3306 0。0。0。0:* LISTEN -
。。。。
udp 0 0 0。0。0。0:68 0。0。0。0:* -
udp6 0 0 :::33527 :::* 1366/java
-a(——all):顯示包括LISTEN狀態的連線,預設沒有。
-t(——tcp):TCP連線。
-u(——udp):UDP連線。
-n(——numeric):顯示數字形式的地址,比如 localhost:mysql 會展示為 127。0。0。1:3306。
-l(——listening):僅顯示處於監聽狀態的套接字。
-p(——program): 顯示PID和程式名。
grep
grep(Global Regular Expression Print的縮寫)命令是一個強大的文字搜尋工具,它使用正則表示式搜尋文字,並把匹配的行輸出。
常用1
:在檔案中查詢特定的字串
➜ ~ grep ‘linux’ 1。log
hello linux
➜ ~ grep -n -e ‘linux’ 1。log 2。log
1。log:1:hello linux
2。log:2:linux is a great os
-e(——regexp=PATTERN):指定要匹配的模式,如果要搜尋多種模式,則形如 -e ‘pattern1’ -e ‘pattern’,否則會把後面的當做檔名。
-n:顯示匹配的行所處的行號。
常用2
:利用管道,在其他命令的輸出中查詢
最常見的是和cat,tail配合使用。
➜ ~ cat 3D_spatial_network。txt| grep 94532185752113
42991635,8。5053563,57。045265,7。94532185752113
常用3
:輸出匹配行的前面幾行(-B),後面幾行(-A)上下文
➜ ~ cat 3D_spatial_network。txt| grep 94532185752113 -A 3 -B 3
42991632,8。560503,57。0228637,23。5659512030544
42991632,8。5595327,57。0230085,23。0573080298429
42991634,8。5052981,57。0427326,9。32131212369931
42991635,8。5053563,57。045265,7。94532185752113
42991635,8。5038165,57。0452699,6。83530187642383
42991636,8。5038165,57。0452699,7。03679520220392
42991637,8。5058627,57。0443316,8。86855661728298
常用4
:在目錄中查詢特定的文字
-r(——recursive):遞迴處理目錄。
➜ ~ grep -nr ‘simple’ logs
logs/1。log:4:simple is beauty
df
df命令用於檢視檔案系統中各個掛載點的磁碟空間使用情況。
常用1
:以易讀方式輸出
➜ ~ df -h
Filesystem Size Used Avail Use% Mounted on
udev 7。8G 0 7。8G 0% /dev
tmpfs 1。6G 89M 1。5G 6% /run
/dev/sda2 901G 427G 428G 50% /
常用2
:列出inode使用情況,有時候會出現磁碟空間未滿,inode不足,可能是因為小檔案太多,取決於虛擬主機的inode的配額。
➜ ~ df -ih
Filesystem Inodes IUsed IFree IUse% Mounted on
udev 2。0M 509 2。0M 1% /dev
tmpfs 2。0M 808 2。0M 1% /run
/dev/sda2 58M 605K 57M 2% /
du
du命令用於顯示某目錄下各個檔案或資料夾佔用的磁碟空間大小。
常用
:du -ah -d 1
➜ test du -ah -d 1
4。0K 。/editDistance。go
20M 。/3D_spatial_network。txt
4。0K 。/hamming。go
4。0K 。/simHashTest。go
116M 。/src
136M 。
-a(——all) : 顯示所有檔案情況,而不僅僅是資料夾。
-h(——human-readable):佔用的磁碟空間大小會轉化為以KB,MB為單位,而不是位元組。
-d(——max-depth=N):對於目錄,可以指定遞迴的深度。
find
find命令是查詢檔案的利器。
常用
:刪除n天之前的檔案
find /path/to/directory/ -mindepth 1 -mtime +5 -delete
也可以使用:
find /path/to/dir -mtime +5 | xargs rm -rf
find 常用的選項有:
-name:指定要尋找的檔名符合的模式。
-i: 忽略大小寫。
-maxdepth:要遞迴的最大深度, -mindepth指定最小深度。
-mtime:指定修改時間在n天之前(+n),或在n天之內(-n)。
ls
常用
:統計一個目錄下檔案個數
ls -1 folder | wc -l
-1:注意是數字1,表示一個檔案佔一行
ln
常用
:建立符號連結(軟連結)
➜ ~ cat ‘hello’ > hello
➜ ~ ln -s hello hello2
➜ ~ ls -alh hello2
lrwxrwxrwx 1 storm storm 5 Aug 31 15:20 hello2 -> hello
-s(——symbolic):軟連結 hello2 會指向原檔案 hello
scp
常用
:複製檔案到伺服器,或者相反
C:\Users\vonzh>scp -P 22 D:\\log。txt storm@10。240。209。160:/tmp
storm@10。240。209。160‘s password:
log。txt 100% 2316KB 2。3MB/s 00:00
-P:指定目標主機SSH埠號。另一種實現檔案複製的方式是採用Python提供的SimpleHTTPServer,然後在對應機器上使用wget下載,如果是用VPN連的伺服器話,要使用隧道的IP。
python -m SimpleHTTPServer 9999
unzip
常用1
:解壓zip到指定目錄
➜ ~ zip test。zip 3D_spatial_network。txt hello
adding: 3D_spatial_network。txt (deflated 63%)
adding: hello (stored 0%)
➜ ~ unzip -d test2 test。zip
Archive: test。zip
inflating: test2/3D_spatial_network。txt
extracting: test2/hello
➜ ~ ls -alh test2
total 20M
drwxrwxr-x 2 storm storm 4。0K Aug 31 15:37 。
drwxr-xr-x 43 storm storm 4。0K Aug 31 15:37 。。
-rw-rw-r—— 1 storm storm 20M Aug 29 15:54 3D_spatial_network。txt
-rw-rw-r—— 1 storm storm 0 Aug 31 15:19 hello
常用2
:列出zip檔案中的檔名
unzip -Z -1 archive。zip
-Z:如果zip命令的第一個引數是-Z,則會相當於呼叫zipinfo命令。
-1:數字1,zipinfo命令的選項,表示只顯示檔名,每個一行,類似ls中的-1。
原文
:Linux常用命令總結