在linux中一切皆文件
。
linux中的絕對路徑和相對路徑。路徑就是文件存放的位置。
絕對路徑的寫法是由根目錄“/”寫起,相對路徑的寫法不是由根目錄寫起
eg:絕對路徑 cd /bin
相對路徑 cd python
1.命令mkdir 該命令是用於創建目錄,英文縮寫為:make directory,格式 mkdir [-mp] [目錄名稱] -m用於制定要創建目錄的權限(該選項不常用)-p該選項可以創建串聯級目錄,若上一個目錄不存在也不會報錯,若有已存在的目錄也不會報錯。
1 eg:[root@localhost tmp]$ mkdir /tmp/test/py 2 mkdir: cannot create directory `/tmp/test/py': No such file or directory 3
4 [root@localhost /]# mkdir -p /tmp/test/py
5 [root@localhost tmp]# ls -ld /tmp/test/
6 drwxr-xr-x. 3 root root 4096 May 2 18:31 /tmp/test/
7 [root@localhost tmp]# ls -ld /tmp/test/py
8 drwxr-xr-x. 2 root root 4096 May 2 18:31 /tmp/test/py
2.命令rm 與 命令rmdir (rm既可以刪除文件也可以刪除目錄,而rmdir只能刪除空目錄不常用)
rm -r:刪除目錄的選項,當使用該選項時,系統會詢問是否刪除,輸入“y”表示同意,“n”表示不同意
1 eg:[root@localhost tmp]# rm -r /tmp/test/py 2 rm: remove directory `/tmp/test/py'
另外rm -f 可以善春非空目錄。
rm -f 強制刪除,不再詢問。
1 eg:[root@localhost tmp]# rm -f /tmp/test/py 2 rm: cannot remove `/tmp/test/py': Is a directory
這樣用該命令,系統提示“這是一個目錄”,因此在使用rm刪除目錄的時候,一定要加上 -r
1 rm -rf /tmp/test/py 2 eg;[root@localhost ]# ls -ld /tmp/test/py
3 ls: cannot access /tmp/test/py: No such file or directory
注意:經常使用的選項是-rf,但是在rm -rf后面不能加“/”,這樣會把整個系統文件全部刪除。
3.命令mv 用於移動目錄或者文件,其還有重命名的作用,是英文move的縮寫。命令格式 mv [選項] [源文件或目錄] 目標文件或目錄]
目標文件是目錄,但該目錄不存在。
目標文件是目錄,但該目錄存在。
(如果該目錄存在,會把源文件或者目錄移動到該目錄中,如果該目錄不存在,則會把源目錄重命名為給定的目標文件)
目標文件是文件,且該文件不存在。
1 eg: 2 [root@localhost home]# mkdir snow snowy
3 [root@localhost home]# ls
4 123 456 py snow snowy 5 [root@localhost home]# mv snow snowman
6 [root@localhost home]# ls
7 123 456 py snowman snowy 8 [root@localhost home]# mv snowman snowy
9 [root@localhost home]# ls
10 123 456 py snowy 11 [root@localhost home]# cd snowy
12 [root@localhost snowy]# ls
13 snowman 14
15 [root@localhost snowman]# touch file_one
16 [root@localhost snowman]# ls
17 file_one 18 [root@localhost snowman]# mv file_one file_two
19 [root@localhost snowman]# ls
20 file_two 21
22 [root@localhost snowman]# mkdir -p file
23 [root@localhost snowman]# ls
24 file file_two 25 [root@localhost snowman]# mv file_two file
26 [root@localhost snowman]# ls
27 file 28 [root@localhost snowman]# cd file
29 [root@localhost file]# ls
30 file_two 31 [root@localhost file]#
目標文件是文件,且文件存在。
(如果該文件存在,系統詢問是否覆蓋。如果該文件不存在,則會把源文件重命名為給定的目標文件名)
eg:mv /tmp/python file /home/py
4.命令cp,是copy的縮寫,格式 cp [選項] [來源文件] [目的文件]
-r 用於復制一個目錄,必須加-r選項,否則不能復制目錄。
-i 是安全選項,若遇到一個已經存在的文件,會詢問是否覆蓋。
1 eg:[root@localhost snowy]# mkdir snow
2 [root@localhost snowy]# ls
3 snow snowman snow_white 4 [root@localhost snowy]# cp snow_white snow
5 [root@localhost snowy]# ls
6 snow snowman snow_white 7 [root@localhost snowy]# cd snow
8 [root@localhost snow]# ls
9 snow_white 10 [root@localhost snow]#
5.命令which,用於查找某個命令的絕對路徑。
eg:[root@localhost snow]# which pwd
/bin/pwd [root@localhost snow]#
6.命令touch,該命令是:如果有這個文件,則會改變這個文件的訪問時間;如果沒有這個文件就會創建這個文件(一般用於創建新文件)。
eg:[root@localhost home]# ls
123 456 py snowy [root@localhost home]# cd snowy
[root@localhost snowy]# ls
snowman [root@localhost snowy]# touch snow_white
[root@localhost snowy]# ls
snowman snow_white
7.命令cat用於連接文件並打印到標准輸出設備上,cat經常用來顯示文件的內容,
注意:當文件較大時,文本在屏幕上迅速閃過(滾屏),用戶往往看不清所顯示的內容。因此,一般用more等命令分屏顯示。為了控制滾屏,可以按Ctrl+S鍵,停止滾屏;按Ctrl+Q鍵可以恢復滾屏。按Ctrl+C(中斷)鍵可以終止該命令的執行,並且返回Shell提示符狀態。
cat [選項] [參數]
-n 或 -number 由1開始對所有輸出的行數編號
eg:[root@localhost home]# echo 'good luck' > home/snowy/snow_while
bash: home/snowy/snow_while: No such file or directory [root@localhost home]# echo 'good luck' > snowy/snow_while
[root@localhost home]# cd snowy
[root@localhost snowy]# ls
snow snowman snow_while snow_white [root@localhost snowy]# vi snow_while
(上面重定向的時候,文件名輸錯了,導致新建了一個文件,snow_while)
-A 顯示所有的內容,包含特殊字符。
eg:[root@localhost snowy]# cat -A snow_while
good luck$
god god$
(Ps:命令tac,也是把文件的內容顯示在屏幕上,只不過先顯示的是最后一行,然后顯示第二行,最后才顯示第一行)
8.命令 echo,用於在shell中打印shell變量的值,或者直接輸出指定的字符串。功能是在顯示器上顯示一段文字,一般起到一個提示的作用。
echo [選項][參數]
-e 激活轉義字符
其中出現的“>”作用是重定向,把前面的內容輸入到后面的文件中,符號“>>”是追加的意思,當使用“>”時,如果文件
中有內容則會刪除文件中原有的內容,而使用符號“>>”不會刪除原有的內容。
9.命令 more 也是用於查看一個文件內容,后面直接跟文件名。當文件內容太多,cat看不了的話,就應該用more來解決
這個問題。
more [語法][參數]
按Space鍵:顯示文本的下一屏內容。
按Enier鍵:只顯示文本的下一行內容。
按斜線符|:接着輸入一個模式,可以在文本中尋找下一個相匹配的模式。
按H鍵:顯示幫助屏,該屏上有相關的幫助信息。 按B鍵:顯示上一屏內容。
按Q鍵:退出rnore命令。
eg: more -c -10 file 顯示文件file的內容,每10行顯示一次,而且在現實之前先清屏。
10.命令less ,less命令的作用與more十分相似,都可以用來瀏覽文字檔案的內容,不同的是less命令允許用戶向前或
向后瀏覽文件,而more命令只能向前瀏覽。用less命令顯示文件時,用PageUp鍵向上翻頁,用PageDown鍵向下翻頁。
要退出less程序,應按Q鍵。
less[選項][參數]
小知識點:"/"查找一個字符串,可以用?來代替。不同的是“/”從當前行向下搜索,而“?”從當前行,向上搜索。
“n”顯示下一個查找出的字符串。
11.命令head ,用於顯示文件的開頭的內容。在默認情況下,head命令顯示文件的頭10行內容。
head [參數] [參數]
-n<數字>:指定顯示頭部內容的行數;
eg:[root@localhost /]# head /etc/passwd
root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin [root@localhost /]# head -5 /etc/passwd
root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
(ps;命令tail,tail命令用於輸入文件中的尾部內容。tail命令默認在屏幕上顯示指定文件的末尾10行。)
12.linux中的文件
一個liunx目錄或者文件,都會有一個所屬主和所屬組。
所屬主食指文件的擁有者;所屬組是指該文件所屬主所在的一個組。
eg:[root@localhost /]# ls -l
total 90 dr-xr-xr-x. 2 root root 4096 Apr 26 08:38 bin dr-xr-xr-x. 5 root root 1024 Apr 26 15:58 boot drwxr-xr-x. 18 root root 3800 May 2 18:01 dev drwxr-xr-x. 101 root root 4096 May 2 18:01 etc drwxr-xr-x. 6 root root 4096 May 2 20:19 home
例子中,第3 4列的root就是所屬主和所屬組。
通過ls-al顯示的結果中看文件屬性示意圖
列中的第1個字符代表這個文件是“目錄、文件或鏈接文件等”
若是【d】則代表該條記錄是目錄;
若是【-】則代表是文件;
若是【|】則表示為連接文件(linkfile);
若是【b】則表示設備文件里面的可供存儲的接口設備;
若是【c】則表示設備文件里面的串口端口設備,例如鍵盤、鼠標。
接下來的字符中,以3個為一組,且均為”rwx”:
其中【r】代表可讀(read);
其中【w】代表可寫(write);
其中【x】代表可執行(execute);
這3個權限的位置不會改變,如果沒有相應的權限,就會出現減號【-】
第2列表示有多少文件名連接到此節點(i-node)
第3列表表示這個文件(或目錄)的“所有者賬號”
第4列表表示這個文件的所屬用戶組
第5列為這個文件的大小,默認單位為B
第6列為這個文件的創建文件日期或者是最近的修改日期
第7列為文件名
13.命令chgrp,用來改變文件或目錄所屬的用戶組。該命令用來改變指定文件所屬的用戶組。其中,組名可以是用戶組
的id,也可以是用戶組的組名。文件名可以 是由空格分開的要改變屬組的文件列表,也可以是由通配符描述的文件集
合。如果用戶不是該文件的文件主或超級用戶(root),則不能改變該文件的組。
eg:[root@localhost py]# groupadd testsnow
[root@localhost py]# touch snowfile1
[root@localhost py]# ls -l snowfile1
-rw-r--r--. 1 root root 0 May 3 20:57 snowfile1 [root@localhost py]# chgrp testsnow snowfile1
[root@localhost py]# ls -l snowfile1
-rw-r--r--. 1 root testsnow 0 May 3 20:57 snowfile1
14.命令chown,改變某個文件或目錄的所有者和所屬的組,該命令可以向某個用戶授權,使該用戶變成指定文件的所有者或
者改變文件所屬的組。用戶可以是用戶或者是用戶D,用戶組可以是組名或組id。文件名可以使由空格分開的文件列表,在
文件名中可以包含通配符。
只有文件主和超級用戶才可以便用該命令。
chown [選項][參數] -R或——recursive:遞歸處理,將指定目錄下的所有文件及子目錄一並處理;
eg:[root@localhost py]# useradd user1
[root@localhost py]# touch testsnow/test
[root@localhost py]# chown user1 test
chown: cannot access `test': No such file or directory [root@localhost py]# cd testsnow
[root@localhost testsnow]# chown user1 test
[root@localhost testsnow]# ls -l test
-rw-r--r--. 1 user1 root 0 May 3 21:02 test [root@localhost testsnow]# [root@localhost testsnow]# ll
total 0
-rw-r--r--. 1 user1 root 0 May 3 21:02 test [root@localhost testsnow]# chown -R user1:testsnow test
[root@localhost testsnow]# ls
test [root@localhost testsnow]# ll
total 0
-rw-r--r--. 1 user1 testsnow 0 May 3 21:02 test [root@localhost testsnow]#
15.chmod命令,用來變更文件或目錄的權限。用戶可以使用chmod指令去變更文件與目錄的權限,設置方式采用文字或數字
代號皆可。符號連接的權限無法變更,如果用戶對符號連接修改權限,其改變會作用在被連接的原始文件。
chmod(選項)(參數)
liunx中使用數字代替“rwx”,r = 4,w = 2,x = 1, - 等於0.-R選項的作用也是級聯更改。
在liunx中,一個目錄的默認權限為755,而一個文件的默認權限是644.
eg:[root@localhost testsnow]# ls -ld
drwxr-xr-x. 2 root root 4096 May 3 21:02 . [root@localhost testsnow]# ls -ld test
-rw-r--r--. 1 user1 testsnow 0 May 3 21:02 test [root@localhost testsnow]# chmod 750 test
[root@localhost testsnow]# ls -ld test
-rwxr-x---. 1 user1 testsnow 0 May 3 21:02 test
chmod還支持rwx的方式來設置權限,可以使用 u g o 分別代表user group others的屬性,a代表all.
eg:[root@localhost testsnow]# chmod u=rwx,og=rx test
[root@localhost testsnow]# ls -ld test
-rwxr-xr-x. 1 user1 testsnow 0 May 3 21:02 test
chmod的還可以針對u g o a 增加或者減少它們的某個權限
eg:[root@localhost testsnow]# chmod u-x test
[root@localhost testsnow]# ls -ld test
-rw-r-xr-x. 1 user1 testsnow 0 May 3 21:02 test
16.命令chattr,命令用來改變文件屬性。這項指令可改變存放在ext2文件系統上的文件或目錄屬性,這些屬性共有以下8種
模式: a:讓文件或目錄僅供附加用途; b:不更新文件或目錄的最后存取時間; c:將文件或目錄壓縮后存放;
d:將文件或目錄排除在傾倒操作之外; i:不得任意更動文件或目錄; s:保密性刪除文件或目錄;
S:即時更新文件或目錄; u:預防意外刪除。
chattr [選項] 或 chattr [+-=][Asaci][文件或目錄名]
在8種模式中,常用的是a 和 i
eg:[root@localhost testsnow]# mkdir test2
[root@localhost testsnow]# ll
total 4
-rw-r-xr-x. 1 user1 testsnow 0 May 3 21:02 test drwxr-xr-x. 2 root root 4096 May 4 00:04 test2 [root@localhost testsnow]# chattr +i test2
[root@localhost testsnow]# touch test2/test1
touch: cannot touch `test2/test1': Permission denied [root@localhost testsnow]# chattr -i test2
[root@localhost testsnow]# touch test2/test1
[root@localhost testsnow]# chattr +i test2
[root@localhost testsnow]# rm -f test2/test1
rm: cannot remove `test2/test1': Permission denied [root@localhost testsnow]#
(在給test2目錄增加了i權限后,雖然是root賬戶也不能在test2中創建或者刪除test1文件) [root@localhost testsnow]# chattr -i test2
[root@localhost testsnow]# touch test2/test3
[root@localhost testsnow]# ls test2
test1 test3 [root@localhost testsnow]# chattr +a test2
[root@localhost testsnow]# rm -f test2/test1
rm: cannot remove `test2/test1': Operation not permitted [root@localhost testsnow]# touch test2/test4
[root@localhost testsnow]# ls test2
test1 test3 test4
17.命令which ,用於查找並顯示給定命令的絕對路徑,環境變量PATH中保存了查找命令時需要遍歷的目錄。which指令會
在環境變量$PATH設置的目錄里查找符合條件的文件。也就是說,使用which命令,就可以看到某個系統命令是否存在,
以及執行的到底是哪一個位置的命令。
which(選項)(參數)
18.命令find,用來在指定目錄下查找文件。任何位於參數之前的字符串都將被視為欲查找的目錄名。如果使用該命令時,
不設置任何參數,則find命令將在當前目錄下查找子目錄與文件。並且將查找到的子目錄和文件全部進行顯示。
-mtime<24小時數>:查找在指定時間曾被更改過的文件或目錄,單位以24小時計算;
-name<范本樣式>:指定字符串作為尋找文件或目錄的范本樣式;
find(選項)(參數)
eg:[root@localhost testsnow]# find /tmp/ -mtime -1
/tmp/
/tmp/.X0-lock /tmp/vmware-root /tmp/vmware-root/vmware-db.pl.2275
/tmp/vmware-root/vmware-db.pl.2282
/tmp/vmware-root/vmware-db.pl.2269
/tmp/vmware-root/vmware-db.pl.2272
/tmp/orbit-gdm /tmp/orbit-gdm/linc-a34-0-1f62f913b8b24 /tmp/orbit-gdm/bonobo-activation-server-ec8d57b0a591075f1b131c1000000037-ior /tmp/orbit-gdm/linc-a36-0-4f545290a6420 /tmp/orbit-gdm/linc-a18-0-2a8fbedb3a6ea /tmp/orbit-gdm/linc-a67-0-2545db41d4629 /tmp/orbit-gdm/bonobo-activation-register-ec8d57b0a591075f1b131c1000000037.lock [root@localhost testsnow]# find .-name test2
find: `.-name': No such file or directory test2 test2/test1 test2/test3 test2/test4
19.命令groupadd,用於創建一個新的工作組,新工作組的信息將被添加到系統文件中。
groupadd(選項)(參數)
eg:[root@localhost testsnow]# groupadd grptest1
[root@localhost testsnow]# tail -n1 /etc/group
grptest1:x:503:
-g:指定新建工作組的id; -r:創建系統工作組,系統工作組的組ID小於500;
eg:[root@localhost testsnow]# groupadd -g 511 grptest2
[root@localhost testsnow]# tail -n2 /etc/group
grptest1:x:503: grptest2:x:511:
20.命令groupdel,用於刪除指定的工作組,本命令要修改的系統文件包括/ect/group和/ect/gshadow。若該群組中仍包括某
些用戶,則必須先刪除這些用戶后,方能刪除群組。
groupdel(參數)
eg:[root@localhost testsnow]# groupdel grptest2
[root@localhost testsnow]# tail -n3 /etc/group
testsnow:x:501: user1:x:502: grptest1:x:503:
若組中有用戶,必須刪除用戶后才能刪除該組。
eg:[root@localhost testsnow]# groupdel user1
groupdel: cannot remove the primary group of user 'user1'
21.命令useradd,可用來建立用戶帳號。帳號建好之后,再用passwd設定帳號的密碼.而可用userdel刪除帳號。
使用useradd指令所建立的帳號,實際上是保存在/etc/passwd文本文件中。
-s:指定用戶登入后所使用的shell; -u:指定用戶id。-d<登入目錄>:指定用戶登入時的啟始目錄;
-M:不要自動建立用戶的登入目錄;-g<群組>:指定用戶所屬的群組;
useradd(選項)(參數)
eg:[root@localhost testsnow]# useradd test0
[root@localhost testsnow]# tail -n1 /etc/passwd
test0:x:502:504::/home/test0:/bin/bash [root@localhost testsnow]# tail -n1 /etc/group
test0:x:504:
命令useradd,不加任何選項,直接跟用戶名,會創建一個跟用戶名同名的組。指定組則需要加入參數。
[root@localhost testsnow]# useradd -u510 -g 513 -M -s /sbin/nologin user11
useradd: group '513' does not exist [root@localhost testsnow]# useradd -u510 -g 502 -M -s /sbin/nologin user11
[root@localhost testsnow]# tail -n2 /etc/passwd
test0:x:502:504::/home/test0:/bin/bash user11:x:510:502::/home/user11:/sbin/nologin [root@localhost testsnow]# tail -n2 /etc/group
grptest1:x:503: test0:x:504:
22.命令userdel,用於刪除給定的用戶,以及與用戶相關的文件。若不加選項,則僅刪除用戶帳號,而不刪除相關文件。
userdel(選項)(參數)
-f:強制刪除用戶,即使用戶當前已登錄; -r:刪除用戶的同時,刪除與用戶相關的所有文件。
[root@localhost testsnow]# ls -ld /home/user12
ls: cannot access /home/user12: No such file or directory [root@localhost testsnow]# ls -ld /home/user1
drwx------. 4 user1 user1 4096 May 3 21:01 /home/user1 [root@localhost testsnow]# userdel user1
userdel: group user1 is the primary group of another user and is not removed. [root@localhost testsnow]# useradd user12
[root@localhost testsnow]# ls
test test2 [root@localhost testsnow]# ls -ld /home/user12
drwx------. 4 user12 user12 4096 May 4 01:27 /home/user12 [root@localhost testsnow]# userdel user12
[root@localhost testsnow]# ls -ld /home/user12
drwx------. 4 511 511 4096 May 4 01:27 /home/user12 [root@localhost testsnow]# ls -ld /home/test2
ls: cannot access /home/test2: No such file or directory [root@localhost testsnow]# ls -ld /home/test10
ls: cannot access /home/test10: No such file or directory [root@localhost testsnow]# ls -ld /home/test1
ls: cannot access /home/test1: No such file or directory [root@localhost testsnow]# ls -ld /home/test
ls: cannot access /home/test: No such file or directory [root@localhost testsnow]# useradd test12
[root@localhost testsnow]# ls -ld /home/test12/
drwx------. 4 test12 test12 4096 May 4 01:29 /home/test12/ [root@localhost testsnow]# userdel -r test12
[root@localhost testsnow]# ls -ld /home/test12
ls: cannot access /home/test12: No such file or directory [root@localhost testsnow]#
23.命令passwd,用於設置用戶的認證信息,包括用戶密碼、密碼過期時間等。系統管理者則能用它管理系統用戶的密碼。
只有管理者可以指定用戶名稱,一般用戶只能變更自己的密碼。
passwd(選項)(用戶名)
-d:刪除密碼,僅有系統管理者才能使用; -f:強制執行; -k:設置只有在密碼過期失效后,方能更新;
-l:鎖住密碼; -s:列出密碼的相關信息,僅有系統管理者才能使用; -u:解開已上鎖的帳號。
ps:只有在root權限下,才能更改其他賬戶的密碼發,普通賬戶只能修改自己的密碼。
eg:[root@localhost /]# passwd test0
Changing password for user test0. New password: Retype new password: passwd: all authentication tokens updated successfully. [root@localhost /]#
24.命令df,用於顯示磁盤分區上的可使用的磁盤空間。默認顯示單位為KB。可以利用該命令來獲取硬盤被占用了多少空間,
目前還剩下多少空間等信息。
df(選項)(參數)
常用選項:-h或--human-readable:以可讀性較高的方式來顯示信息;
eg:[root@localhost /]# df -h
Filesystem Size Used Avail Use% Mounted on /dev/sda2 18G 3.3G 14G 20% / tmpfs 495M 72K 495M 1% /dev/shm /dev/sda1 283M 37M 232M 14% /boot [root@localhost /]# df
Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda2 18208184 3432564 13844036 20% / tmpfs 506228 72 506156 1% /dev/shm /dev/sda1 289293 36874 237059 14% /boot
25.命令du,也是查看使用空間的,但是與df命令不同的是Linux du命令是對文件和目錄磁盤使用的空間的查看,還是和df
命令有一些區別的。
du [選項][文件]
常用的選項:-s或--summarize 僅顯示總計,只列出最后加總的值。
-h或--human-readable 以K,M,G為單位,提高信息的可讀性。
eg:[root@localhost /]# du -sh /etc/passwd
4.0K /etc/passwd
26.liunx中壓縮文件
.gz 表示由gzip壓縮工具壓縮的文件
.bz2 表示由bzip2壓縮工具壓縮的文件
.tar 表示由tar打包程序打包的文件(tar並沒有壓縮功能,只是把一個目錄合並成一個文件)
.tar.gz 表示由tar打包程序后,再由gzip壓縮。
.tar.bz2 表示由tar打包,然后再由bzip2壓縮
命令gzip,gzip是在Linux系統中經常使用的一個對文件進行壓縮和解壓縮的命令,既方便又好用。gzip不僅可以用來
壓縮大的、較少使用的文件以節省磁盤空間,還可以和tar命令一起構成Linux操作系統中比較流行的壓縮文件格式。
據統計,gzip命令對文本文件有60%~70%的壓縮率。減少文件大小有兩個明顯的好處,一是可以減少存儲空間,二是
通過網絡傳輸文件時,可以減少傳輸的時間。
gzip(選項)(參數)
-d或--decompress或----uncompress:解開壓縮文件;
-#:表示壓縮等級,1為最差,9為最好,6為默認。
eg:[root@localhost py]# cd /.
[root@localhost /]# cd home
[root@localhost home]# mkdir test
[root@localhost home]# mv test.txt test
mv: cannot stat `test.txt': No such file or directory [root@localhost home]# touch test.txt
[root@localhost home]# ls
123 456 py snowy test test0 test.txt user1 user12 [root@localhost home]# mv test.txt test
[root@localhost home]# cd test
[root@localhost test]# ls
test.txt [root@localhost test]# gzip test.txt
[root@localhost test]# ls
test.txt.gz [root@localhost test]#
用gzip壓縮后,源文件消失了。
用gzip用來解壓文件,
eg:[root@localhost test]# ls
test.txt.gz [root@localhost test]# gzip -d test.txt.gz
[root@localhost test]# ls
test.txt
注意:gzip不支持壓縮目錄,壓縮目錄時會報錯。
27.命令bzip2壓縮工具, 用於創建和管理(包括解壓縮)“.bz2”格式的壓縮包。
bzip2(選項)(參數)
-d或——decompress:執行解壓縮;
-z或——compress:強制執行壓縮;
eg:[root@localhost test]# ls
test.txt [root@localhost test]# bzip test.txt
bash: bzip: command not found [root@localhost test]# bzip2 test.txt
[root@localhost test]# ls
test.txt.bz2 [root@localhost test]# bzip2 -d test.txt.bz2
[root@localhost test]# ls
test.txt [root@localhost test]# bzip2 -z test.txt
[root@localhost test]# ls
test.txt.bz2 [root@localhost test]# ll
total 4
-rw-r--r--. 1 root root 14 May 4 05:57 test.txt.bz2 [root@localhost test]#
28.命令tar,可以為linux的文件和目錄創建檔案。利用tar,可以為某一特定文件創建檔案(備份文件),也可以在檔案中
改變文件,或者向檔案中加入新的文件。
首先要弄清兩個概念:打包和壓縮。打包是指將一大堆文件或目錄變成一個總的文件;壓縮則是將一個大的文件
通過一些壓縮算法變成一個小文件。
為什么要區分這兩個概念呢?這源於Linux中很多壓縮程序只能針對一個文件進行壓縮,這樣當你想要壓縮一大堆
文件時,你得先將這一大堆文件先打成一個包(tar命令),然后再用壓縮程序進行壓縮(gzip bzip2命令)。
-c或--create:建立一個tar包或者壓縮文件包;
-t或--list:查看壓縮包中的文件;
-z或--gzip或--ungzip:同時用gzip壓縮;
-f<備份文件>或--file=<備份文件>:指定備份文件;
-v或--verbose:顯示指令執行過程;
-j:支持bzip2解壓文件;
-v:顯示操作過程;
-p或--same-permissions:用原來的文件權限還原文件;(不常用)
-P或--absolute-names:文件名使用絕對名稱,不移除文件名稱前的“/”號;(不常用)
--exclude=<范本樣式>:排除符合范本樣式的文件。(不常用)
eg: [root@localhost test]# mkdir test11
[root@localhost test]# touch cd /test2.txt
[root@localhost test]# echo "nihao" > test11/test2.txt
[root@localhost test]# ls
test11 test.txt.bz2 [root@localhost test]# tar -cvf test11.tar test11
test11/ test11/test2.txt [root@localhost test]# ll
total 20 drwxr-xr-x. 2 root root 4096 May 4 06:32 test11 -rw-r--r--. 1 root root 10240 May 4 06:33 test11.tar [root@localhost test]# rm -rf test11
[root@localhost test]# ls
test11.tar test.txt.bz2 [root@localhost test]# tar -xvf test.tar
tar: test.tar: Cannot open: No such file or directory tar: Error is not recoverable: exiting now [root@localhost test]# tar -xvf test11.tar
test11/ test11/test2.txt
打包的同時使用gzip壓縮 ,使用-czvf就可以同時打包
eg:[root@localhost test]# ll
total 20 drwxr-xr-x. 2 root root 4096 May 4 06:32 test11 -rw-r--r--. 1 root root 10240 May 4 06:33 test11.tar -rw-r--r--. 1 root root 14 May 4 05:57 test.txt.bz2 [root@localhost test]# tar -czvf test11.tar.gz test11
test11/ test11/test2.txt [root@localhost test]# ]ll
bash: ]ll: command not found [root@localhost test]# ll
total 24 drwxr-xr-x. 2 root root 4096 May 4 06:32 test11 -rw-r--r--. 1 root root 10240 May 4 06:33 test11.tar -rw-r--r--. 1 root root 159 May 4 06:45 test11.tar.gz -rw-r--r--. 1 root root 14 May 4 05:57 test.txt.bz2
使用-zxvf解壓tar.gz格式的壓縮包
eg:[root@localhost test]# ll
total 20
-rw-r--r--. 1 root root 10240 May 4 06:33 test11.tar -rw-r--r--. 1 root root 159 May 4 06:45 test11.tar.gz -rw-r--r--. 1 root root 14 May 4 05:57 test.txt.bz2 [root@localhost test]# tar -zxvf test.tar.gz
tar (child): test.tar.gz: Cannot open: No such file or directory tar (child): Error is not recoverable: exiting now tar: Child returned status 2 tar: Error is not recoverable: exiting now [root@localhost test]# tar -zxvf test11.tar.gz
test11/ test11/test2.txt [root@localhost test]# ll
total 24 drwxr-xr-x. 2 root root 4096 May 4 06:32 test11 -rw-r--r--. 1 root root 10240 May 4 06:33 test11.tar -rw-r--r--. 1 root root 159 May 4 06:45 test11.tar.gz -rw-r--r--. 1 root root 14 May 4 05:57 test.txt.bz2 [root@localhost test]#
使用-cjvf選項來壓縮
eg:[root@localhost test]# tar -cjvf test11.tar.bz2 test11
test11/ test11/test2.txt [root@localhost test]# ll
total 28 drwxr-xr-x. 2 root root 4096 May 4 06:32 test11 -rw-r--r--. 1 root root 10240 May 4 06:33 test11.tar -rw-r--r--. 1 root root 155 May 4 06:59 test11.tar.bz2 -rw-r--r--. 1 root root 159 May 4 06:45 test11.tar.gz -rw-r--r--. 1 root root 14 May 4 05:57 test.txt.bz2 [root@localhost test]#
使用-jxvf來解壓.tar.bz2格式的壓縮包
eg:[root@localhost test]# tar -jxvf test.tar.bz2
\tar (child): test.tar.bz2: Cannot open: No such file or directory tar (child): Error is not recoverable: exiting now tar: Child returned status 2 tar: Error is not recoverable: exiting now [root@localhost test]# tar -jxvf test11.tar.bz2
test11/ test11/test2.txt [root@localhost test]#
29.命令w,用於顯示已經登陸系統的用戶列表,並顯示用戶正在執行的指令。執行這個命令可得知目前登入系統的用戶
有那些人,以及他們正在執行的程序。單獨執行w命令會顯示所有的用戶,您也可指定用戶名稱,僅顯示某位用戶的
相關信息。
w(選項)(參數)
eg:[root@localhost /]# w
08:11:41 up 13:13, 1 user, load average: 0.00, 0.00, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT py pts/0 192.168.100.11 Tue19 1.00s 3.22s 0.38s sshd: py [priv] [root@localhost /]#
查看CPU的詳細信息,用命令cat /proc/cpuinfo
eg:[root@localhost /]# cat /proc/cpuinfo
processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 42 model name : Intel(R) Core(TM) i3-2130 CPU @ 3.40GHz stepping : 7 microcode : 40 cpu MHz : 3392.340 cache size : 3072 KB physical id : 0 siblings : 2 core id : 0 cpu cores : 2 apicid : 0 initial apicid : 0 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss ht syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf unfair_spinlock pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt xsave avx hypervisor lahf_lm arat epb xsaveopt pln pts dts bogomips : 6784.68 clflush size : 64 cache_alignment : 64 address sizes : 40 bits physical, 48 bits virtual power management: processor : 1 vendor_id : GenuineIntel cpu family : 6 model : 42 model name : Intel(R) Core(TM) i3-2130 CPU @ 3.40GHz stepping : 7 microcode : 40 cpu MHz : 3392.340 cache size : 3072 KB physical id : 0 siblings : 2 core id : 1 cpu cores : 2 apicid : 1 initial apicid : 1 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss ht syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf unfair_spinlock pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt xsave avx hypervisor lahf_lm arat epb xsaveopt pln pts dts bogomips : 6784.68 clflush size : 64 cache_alignment : 64 address sizes : 40 bits physical, 48 bits virtual power management: [root@localhost /]#
30.命令pwd,以絕對路徑的方式顯示用戶當前工作目錄。命令將當前目錄的全路徑名稱(從根目錄)寫入標准輸出。
全部目錄使用/分隔。第一個/表示根目錄,最后一個目錄是當前目錄。執行pwd命令可立刻得知您目前所在的工作
目錄的絕對路徑名稱。
pwd(選項)
--help:顯示幫助信息; --version:顯示版本信息。
eg:[py@localhost ~]$ pwd /home/py [py@localhost ~]$
31.命令rename,用字符串替換的方式批量改變文件名。
rename(參數)
原字符串:將文件名需要替換的字符串;
目標字符串:將文件名中含有的原字符替換成目標字符串;
文件:指定要改變文件名的文件列表。
注意:rename支持通配符
?可替代單個字符
* 可替代多個字符
[charset] 克替代charset集中的任意單個字符
eg:[root@localhost home]# touch main1.c
[root@localhost home]# ll
total 32 drwxr-xr-x. 2 root root 4096 May 2 19:13 123 drwxr-xr-x. 2 root root 4096 May 2 19:10 456
-rw-r--r--. 1 root root 0 May 5 07:23 main1.c drwx------. 28 py py 4096 May 3 21:01 py drwxr-xr-x. 4 root root 4096 May 3 06:52 snowy drwxr-xr-x. 3 root root 4096 May 4 06:59 test drwx------. 4 test0 test0 4096 May 4 00:54 test0 drwx------. 4 501 user1 4096 May 3 21:01 user1 drwx------. 4 511 511 4096 May 4 01:27 user12 [root@localhost home]# rename main1.c main.c main.c
[root@localhost home]# ll
'total 32 drwxr-xr-x. 2 root root 4096 May 2 19:13 123 drwxr-xr-x. 2 root root 4096 May 2 19:10 456
-rw-r--r--. 1 root root 0 May 5 07:23 main1.c drwx------. 28 py py 4096 May 3 21:01 py drwxr-xr-x. 4 root root 4096 May 3 06:52 snowy drwxr-xr-x. 3 root root 4096 May 4 06:59 test drwx------. 4 test0 test0 4096 May 4 00:54 test0 drwx------. 4 501 user1 4096 May 3 21:01 user1 drwx------. 4 511 511 4096 May 4 01:27 user12 [root@localhost home]# rename main1.c main.c main1.c
[root@localhost home]# ll
total 32 drwxr-xr-x. 2 root root 4096 May 2 19:13 123 drwxr-xr-x. 2 root root 4096 May 2 19:10 456
-rw-r--r--. 1 root root 0 May 5 07:23 main.c drwx------. 28 py py 4096 May 3 21:01 py drwxr-xr-x. 4 root root 4096 May 3 06:52 snowy drwxr-xr-x. 3 root root 4096 May 4 06:59 test drwx------. 4 test0 test0 4096 May 4 00:54 test0 drwx------. 4 501 user1 4096 May 3 21:01 user1 drwx------. 4 511 511 4096 May 4 01:27 user12 [root@localhost home]# [root@localhost home]# ll
total 32 drwxr-xr-x. 2 root root 4096 May 2 19:13 123 drwxr-xr-x. 2 root root 4096 May 2 19:10 456
-rw-r--r--. 1 root root 0 May 5 07:35 foo1 -rw-r--r--. 1 root root 0 May 5 07:35 foo2 -rw-r--r--. 1 root root 0 May 5 07:35 foo3 -rw-r--r--. 1 root root 0 May 5 07:35 foo4 -rw-r--r--. 1 root root 0 May 5 07:35 foo5 -rw-r--r--. 1 root root 0 May 5 07:23 main.c drwx------. 28 py py 4096 May 3 21:01 py drwxr-xr-x. 4 root root 4096 May 3 06:52 snowy drwxr-xr-x. 3 root root 4096 May 4 06:59 test drwx------. 4 test0 test0 4096 May 4 00:54 test0 drwx------. 4 501 user1 4096 May 3 21:01 user1 drwx------. 4 511 511 4096 May 4 01:27 user12 [root@localhost home]# rename foo foo0 foo?
[root@localhost home]# ll
total 32 drwxr-xr-x. 2 root root 4096 May 2 19:13 123 drwxr-xr-x. 2 root root 4096 May 2 19:10 456
-rw-r--r--. 1 root root 0 May 5 07:35 foo01 -rw-r--r--. 1 root root 0 May 5 07:35 foo02 -rw-r--r--. 1 root root 0 May 5 07:35 foo03 -rw-r--r--. 1 root root 0 May 5 07:35 foo04 -rw-r--r--. 1 root root 0 May 5 07:35 foo05 -rw-r--r--. 1 root root 0 May 5 07:23 main.c drwx------. 28 py py 4096 May 3 21:01 py drwxr-xr-x. 4 root root 4096 May 3 06:52 snowy drwxr-xr-x. 3 root root 4096 May 4 06:59 test drwx------. 4 test0 test0 4096 May 4 00:54 test0 drwx------. 4 501 user1 4096 May 3 21:01 user1 drwx------. 4 511 511 4096 May 4 01:27 user12 [root@localhost home]#
32.命令tree,以樹狀圖列出目錄的內容。
tree(選項)(參數)
-a:顯示所有文件和目錄;
-C:在文件和目錄清單加上色彩,便於區分各種類型;
-d:先是目錄名稱而非內容; -D:列出文件或目錄的更改時間;
-f:在每個文件或目錄之前,顯示完整的相對路徑名稱;
-F:在執行文件,目錄,Socket,符號連接,管道名稱名稱,各自加上"*","/","@","|"號;
-g:列出文件或目錄的所屬群組名稱,沒有對應的名稱時,則顯示群組識別碼;
-l:如遇到性質為符號連接的目錄,直接列出該連接所指向的原始目錄;
-n:不在文件和目錄清單加上色彩;
-N:直接列出文件和目錄名稱,包括控制字符;
-p:列出權限標示;
-s:列出文件和目錄大小;
-t:用文件和目錄的更改時間排序;
eg: ├── Python-2.7.11.tar.xz ├── Python-3.5.1.tar.xz ├── snowfile1 ├── Templates ├── testsnow │ ├── test │ └── test2 │ ├── test1 │ ├── test3 │ └── test4
33.命令basename,用於打印目錄或者文件的基本名稱。basename和dirname命令通常用於shell腳本中的命令替換來指
定和指定的輸入文件名稱有所差異的輸出文件名稱。
basename(選項)(參數)
eg:顯示一個shell變量的基本名稱
[py@localhost ~]$ basename /etc/yum.conf
yum.conf
命令dirname,顯示指定路徑處了文件名之外的路徑前綴
eg:[py@localhost ~]$ dirname /home/py/test.tar
/home/py
34.命令lsblk,列出塊設備。除了RAM外,以標准的樹狀輸出格式,整齊地顯示塊設備。
eg:[py@localhost ~]$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 300M 0 part /boot ├─sda2 8:2 0 17.8G 0 part / └─sda3 8:3 0 2G 0 part [SWAP] sr0 11:0 1 1024M 0 rom
“lsblk -l”命令以列表格式顯示塊設備(而不是樹狀格式)。
[py@localhost ~]$ lsblk -l NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk sda1 8:1 0 300M 0 part /boot sda2 8:2 0 17.8G 0 part / sda3 8:3 0 2G 0 part [SWAP] sr0 11:0 1 1024M 0 rom
注意:lsblk是最有用和最簡單的方式來了解新插入的USB設備的名字,特別是當你在終端上處理磁盤/塊設備時。
35.命令lsattr,顯示文件隱藏屬性。
lsattr [選項][文件或目錄]
-R
遞歸地列出目錄以及其下內容的屬性.
-V
顯示程序版本.
-a
列出目錄中的所有文件,包括以`.'開頭的文件的屬性.
-d
以列出其它文件的方式那樣列出目錄的屬性, 而不列出其下的內容.
-v
顯示文件版本.
eg:[root@localhost /]# mkdir /tmp/attrtest
[root@localhost /]#
[root@localhost /]# chattr +aij attrtest
chattr: No such file or directory while trying to stat attrtest [root@localhost /]# cd /tmp
[root@localhost tmp]# chattr +aij attrtest
[root@localhost tmp]# lsattr attrtest/
[root@localhost tmp]# lsattr -a attrtest/
----ia---j---e- attrtest/. -------------e- attrtest/.. [root@localhost tmp]#
36. 命令file,確定文件類型
eg:[root@localhost tmp]# file *
attrtest: directory keyring-2DcMS9: directory ks-script-nGXdB2: ASCII text ks-script-nGXdB2.log: ASCII text orbit-gdm: directory pulse-tuvfPBIzAlnP: directory pulse-vWYu5SLNfS11: directory test: directory virtual-py.DV03kt: directory virtual-py.H7iN1p: directory virtual-py.TSYhZy: directory vmware-config0: directory VMwareDnD: sticky directory vmware-py: directory vmware-root: directory vmware-root-1018539731: directory yum.log: empty yum_save_tx-2016-05-05-08-14vjVlfn.yumtx: ASCII text, with very long lines [root@localhost tmp]#
37.命令vmstat,監控系統各項壓力參數。此命令可以打印出procs memory swap io system cpu
eg:[root@localhost tmp]# vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 1 0 0 749652 20724 114132 0 0 25 4 32 28 0 1 98 0 0 [root@localhost tmp]#
注意;關注r b wa這3列
類別 項目 含義 說明
Procs r 等待執行的任務數 展示了正在執行和等待CPU資源的任務個數。當這個值超過了CPU數目,就會出現CPU瓶頸了
b 處在非中斷睡眠狀態的進程數
Memory swpd 正在使用的swap大小單位K
free 空閑的內存空間
buff 已使用的buff大小,對塊設備的讀寫進行緩沖
cache 已使用的cache大小,文件系統的cache
inact 非活躍內存大小
active 活躍的內存大小
Swap si 交換內存使用,由磁盤調入內存
so 交換內存使用,由內存調入磁盤
IO bi 從塊設備讀入的數據總量(讀磁盤) (KB/s),
bo 寫入到塊設備的數據總理(寫磁盤) (KB/s)
System in 每秒產生的中斷次數
cs 每秒產生的上下文切換次數 上面這2個值越大,會看到由內核消耗的CPU時間會越多
CPU us 用戶進程消耗的CPU時間百分比 us 的值比較高時,說明用戶進程消耗的CPU時間多,但是如果長期超過50% 的使用,那么我們就該考慮優化程序算法或者進行加速了
sy 內核進程消耗的CPU時間百分比 sy 的值高時,說明系統內核消耗的CPU資源多,這並不是良性的表現,我們應該檢查原因。
id 空閑
wa IO等待消耗的CPU時間百分比 wa 的值高時,說明IO等待比較嚴重,這可能是由於磁盤大量作隨機訪問造成,也有可能是磁盤的帶寬出現瓶頸(塊操作)。
38.命令top,可以顯示當前系統正在執行的進程的相關信息,包括進程ID、內存占用率、CPU占用率等。可以理解為
動態監控進程所占的系統資源。3秒更新一次
[root@localhost tmp]# top
top - 08:26:51 up 1:01, 1 user, load average: 0.00, 0.00, 0.00 Tasks: 114 total, 1 running, 113 sleeping, 0 stopped, 0 zombie Cpu(s): 0.2%us, 0.2%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 1012456k total, 263348k used, 749108k free, 20924k buffers Swap: 2031612k total, 0k used, 2031612k free, 114420k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 12 root 20 0 0 0 0 S 0.3 0.0 0:05.83 events/1
1679 root 20 0 174m 4448 3608 S 0.3 0.4 0:15.71 vmtoolsd 2706 py 20 0 99972 1820 856 S 0.3 0.2 0:00.77 sshd 1 root 20 0 19356 1600 1272 S 0.0 0.2 0:04.45 init 2 root 20 0 0 0 0 S 0.0 0.0 0:00.02 kthreadd 3 root RT 0 0 0 0 S 0.0 0.0 0:00.12 migration/0
4 root 20 0 0 0 0 S 0.0 0.0 0:00.56 ksoftirqd/0
5 root RT 0 0 0 0 S 0.0 0.0 0:00.00 stopper/0
6 root RT 0 0 0 0 S 0.0 0.0 0:00.06 watchdog/0
7 root RT 0 0 0 0 S 0.0 0.0 0:00.79 migration/1
8 root RT 0 0 0 0 S 0.0 0.0 0:00.00 stopper/1
9 root 20 0 0 0 0 S 0.0 0.0 0:00.36 ksoftirqd/1
注意:top -bn1表示非動態打印系統資源的使用情況。
39.命令sar,sar是目前 Linux 上最為全面的系統性能分析工具之一,可以從多方面對
系統的活動進行報告,包括:文件的讀寫情況、系統調用的使用情況、磁盤I/O、CPU效率、
內存使用狀況、進程活動及IPC有關的活動等。
sar命令常用格式
sar [options] [-A] [-o file] t [n]
t為采樣間隔,n為采樣次數,默認值是1;
查看網卡流量,sar -n DEV
eg: [root@localhost tmp]# sar -n DEV
Linux 2.6.32-573.el6.x86_64 (localhost.localdomain) 05/06/2016 _x86_64_(2 CPU) 07:25:15 AM LINUX RESTART 07:30:01 AM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s 07:40:01 AM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
07:40:01 AM eth0 0.70 0.36 0.06 0.04 0.00 0.00 0.00 Average: lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 Average: eth0 0.70 0.36 0.06 0.04 0.00 0.00 0.00
07:40:55 AM LINUX RESTART 07:50:02 AM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s 08:00:01 AM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
08:00:01 AM eth0 0.43 0.21 0.04 0.03 0.00 0.00 0.00
08:10:01 AM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
08:10:01 AM eth0 0.11 0.03 0.01 0.00 0.00 0.00 0.00
08:20:01 AM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
08:20:01 AM eth0 0.15 0.06 0.01 0.00 0.00 0.00 0.00
08:30:01 AM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
08:30:01 AM eth0 0.22 0.15 0.02 0.09 0.00 0.00 0.00 Average: lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 Average: eth0 0.23 0.11 0.02 0.03 0.00 0.00 0.00 [root@localhost tmp]#
查看歷史負載,sar -q
[root@localhost tmp]# sar -q
Linux 2.6.32-573.el6.x86_64 (localhost.localdomain) 05/06/2016 _x86_64_(2 CPU) 07:25:15 AM LINUX RESTART 07:30:01 AM runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15
07:40:01 AM 0 213 0.00 0.02 0.05 Average: 0 213 0.00 0.02 0.05
07:40:55 AM LINUX RESTART 07:50:02 AM runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15
08:00:01 AM 0 188 0.00 0.00 0.00
08:10:01 AM 0 189 0.00 0.00 0.00
08:20:01 AM 0 189 0.00 0.00 0.00
08:30:01 AM 0 189 0.00 0.00 0.00 Average: 0 189 0.00 0.00 0.00 [root@localhost tmp]#
40. 命令ps,專門顯示系統進程的命令。此命令比top更加方便。
經常用到的,ps aux ps -elf
eg:[root@localhost tmp]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.1 19356 1600 ? Ss 07:24 0:04 /sbin/init root 2 0.0 0.0 0 0 ? S 07:24 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? S 07:24 0:00 [migration/0] root 4 0.0 0.0 0 0 ? S 07:24 0:00 [ksoftirqd/0] root 5 0.0 0.0 0 0 ? S 07:24 0:00 [stopper/0] root 6 0.0 0.0 0 0 ? S 07:24 0:00 [watchdog/0] root 7 0.0 0.0 0 0 ? S 07:24 0:00 [migration/1] root 8 0.0 0.0 0 0 ? S 07:24 0:00 [stopper/1] root 9 0.0 0.0 0 0 ? S 07:24 0:00 [ksoftirqd/1] root 10 0.0 0.0 0 0 ? S 07:24 0:00 [watchdog/1] root 11 0.0 0.0 0 0 ? S 07:24 0:01 [events/0] root 12 0.1 0.0 0 0 ? S 07:24 0:07 [events/1] root 13 0.0 0.0 0 0 ? S 07:24 0:00 [events/0] root 14 0.0 0.0 0 0 ? S 07:24 0:00 [events/1] root 15 0.0 0.0 0 0 ? S 07:24 0:00 [events_long/0] root 16 0.0 0.0 0 0 ? S 07:24 0:00 [events_long/1] root 17 0.0 0.0 0 0 ? S 07:24 0:00 [events_power_e] root 18 0.0 0.0 0 0 ? S 07:24 0:00 [events_power_e] root 19 0.0 0.0 0 0 ? S 07:24 0:00 [cgroup] root 20 0.0 0.0 0 0 ? S 07:24 0:00 [khelper] root 21 0.0 0.0 0 0 ? S 07:24 0:00 [netns]
命令ps -elf
[root@localhost tmp]# ps -elf
F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD 4 S root 1 0 0 80 0 - 4839 poll_s 07:24 ? 00:00:04 /sbin/
1 S root 2 0 0 80 0 - 0 kthrea 07:24 ? 00:00:00 [kthr] 1 S root 3 2 0 -40 - - 0 migrat 07:24 ? 00:00:00 [migr] 1 S root 4 2 0 80 0 - 0 ksofti 07:24 ? 00:00:00 [ksof] 1 S root 5 2 0 -40 - - 0 cpu_st 07:24 ? 00:00:00 [stop] 5 S root 6 2 0 -40 - - 0 watchd 07:24 ? 00:00:00 [watc] 1 S root 7 2 0 -40 - - 0 migrat 07:24 ? 00:00:00 [migr] 1 S root 8 2 0 -40 - - 0 cpu_st 07:24 ? 00:00:00 [stop] 1 S root 9 2 0 80 0 - 0 ksofti 07:24 ? 00:00:00 [ksof] 5 S root 10 2 0 -40 - - 0 watchd 07:24 ? 00:00:00 [watc] 1 S root 11 2 0 80 0 - 0 worker 07:24 ? 00:00:01 [even] 1 S root 12 2 0 80 0 - 0 worker 07:24 ? 00:00:07 [even] 1 S root 13 2 0 80 0 - 0 worker 07:24 ? 00:00:00 [even] 1 S root 14 2 0 80 0 - 0 worker 07:24 ? 00:00:00 [even] 1 S root 15 2 0 80 0 - 0 worker 07:24 ? 00:00:00 [even] 1 S root 16 2 0 80 0 - 0 worker 07:24 ? 00:00:00 [even] 1 S root 17 2 0 80 0 - 0 worker 07:24 ? 00:00:00 [even] 1 S root 18 2 0 80 0 - 0 worker 07:24 ? 00:00:00 [even] 1 S root 19 2 0 80 0 - 0 worker 07:24 ? 00:00:00 [cgro] 1 S root 20 2 0 80 0 - 0 worker 07:24 ? 00:00:00 [khel] 1 S root 21 2 0 80 0 - 0 worker 07:24 ? 00:00:00 [netn] 1 S root 22 2 0 80 0 - 0 async_ 07:24 ? 00:00:00 [asyn] 1 S root 23 2 0 80 0 - 0 worker 07:24 ? 00:00:00 [pm] 1 S root 24 2 0 80 0 - 0 bdi_sy 07:24 ? 00:00:00 [sync]
PID表示進程的id,這個id很有用,比如我們要終止一個進程,則用命令 kill 進程的id.有時候
這樣還不能終止進程,那么可以使用kill -9 進程的id。
STAT表示進程的狀態。
41.命令netstat,查看網絡連接狀況。顯示網絡連接,路由表,接口狀態,偽裝連接,網絡鏈路信息和組播成員組。
netstat -lnp 打印系統啟動哪些端口
eg:[root@localhost tmp]# netstat -lnp
Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 2269/sshd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 2105/cupsd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2397/master tcp 0 0 :::22 :::* LISTEN 2269/sshd tcp 0 0 ::1:631 :::* LISTEN 2105/cupsd tcp 0 0 ::1:25 :::* LISTEN 2397/master udp 0 0 0.0.0.0:68 0.0.0.0:* 2108/dhclient udp 0 0 0.0.0.0:631 0.0.0.0:* 2105/cupsd Active UNIX domain sockets (only servers) Proto RefCnt Flags Type State I-Node PID/Program name Path unix 2 [ ACC ] STREAM LISTENING 13803 2156/hald @/var/run/hald/dbus-t9u3xeiG2L unix 2 [ ACC ] STREAM LISTENING
netstat -an打印網絡連接狀況
eg:[root@localhost tmp]# netstat -an
Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp 0 64 192.168.100.44:22 192.168.100.5:6080 ESTABLISHED tcp 0 0 :::22 :::* LISTEN tcp 0 0 ::1:631 :::* LISTEN tcp 0 0 ::1:25 :::* LISTEN udp 0 0 0.0.0.0:68 0.0.0.0:* udp 0 0 0.0.0.0:631 0.0.0.0:* Active UNIX domain sockets (servers and established) Proto RefCnt Flags Type State I-Node Path unix 2 [ ACC ] STREAM LISTENING 13803 @/var/run/hald/dbus-t9u3xeiG2L unix 2 [ ACC ] STREAM LISTENING 9292 @/com/ubuntu/upstart unix 2 [ ACC ] STREAM LISTENING 13810 @/var/run/hald/dbus-MKoSzKTZ4n unix 2 [ ] DGRAM 13835 @/org/freedesktop/hal/udev_event unix 2 [ ] DGRAM 9798 @/org/kernel/udev/udevd unix 2 [ ACC ] STREAM LISTENING 13448 /var/run/dbus/system_bus_socket unix 2 [ ACC ] STREAM LISTENING 13679 /var/run/cups/cups.sock unix 2 [ ACC ] STREAM LISTENING 14587 public/cleanup unix 2 [ ACC ] STREAM LISTENING 14595 private/tlsmgr unix 2 [ ACC ] STREAM LISTENING 14599 private/rewrite unix 2 [ ACC ] STREAM LISTENING 14603 private/bounce unix 2 [ ACC ] STREAM LISTENING 14607 private/defer unix 2 [ ACC ] STREAM LISTENING 14611 private/trace unix 2 [ ACC ] STREAM LISTENING 14615 private/verify unix 2 [ ACC ] STREAM LISTENING 14619 public/flush unix 2 [ ACC ] STREAM LISTENING 14623 private/proxymap unix 2 [ ACC ] STREAM LISTENING 14627 private/proxywrite unix 2 [ ACC ] STREAM LISTENING 14631 private/smtp unix 2 [ ACC ] STREAM LISTENING 14635 private/relay
42.命令cut,用來顯示行中的指定部分,刪除文件中指定字段。cut經常用來顯示文件的內容
cut(選項)(參數)
-b:僅顯示行中指定直接范圍的內容;
-c:僅顯示行中指定范圍的字符;
-d:指定字段的分隔符,默認的字段分隔符為“TAB”;
-f:顯示指定字段的內容
cut命令有2個功能:
1.用來顯示文件的內容,它依次讀取由參數file所指 明的文件,將它們的內容輸出到標准輸出上
2.是連接兩個或多個文件,如cut fl f2 > f3將把文件fl和幾的內容合並起來,然后通過輸出重定
向符“>”的作用,將它們放入文件f3中。
43.命令split,可以將一個大文件分割成很多個小文件,有時需要將文件分割成更小的片段,比如為提高可讀性,
生成日志等。
-b:值為每一輸出檔案的大小,單位為 byte。
-C:每一輸出檔中,單行的最大 byte 數。
-d:使用數字作為后綴。
-l:值為每一輸出檔的列數大小。
eg:生成一個大小為100KB的測試文件。 [root@localhost test]# dd if=/dev/zero bs=100k count=1 of=date.file
1+0 records in
1+0 records out 102400 bytes (102 kB) copied, 0.000919489 s, 111 MB/s [root@localhost test]#
使用split將上面創建的date.file文件分割大小為10kb的小文件
[root@localhost test]# split -b 10k date.file
[root@localhost test]# lls
bash: lls: command not found [root@localhost test]# ls
date.file test11.tar test11.tar.gz xaa xac xae xag xai test11 test11.tar.bz2 test.txt.bz2 xab xad xaf xah xaj [root@localhost test]#
44.命令sort,它將文件進行排序,並將排序結果標准輸出。sort命令既可以從特定的文件,也可以從stdin中
獲取輸入。
sort(選項)(參數)
-b:忽略每行前面開始出的空格字符;
-c:檢查文件是否已經按照順序排序;
-d:排序時,處理英文字母、數字及空格字符外,忽略其他的字符;
-f:排序時,將小寫字母視為大寫字母;
-n:依照數值的大小排序;
-r:以相反的順序來排序;
eg:[root@localhost sort]# cat /etc/passwd | sort
abrt:x:173:173::/etc/abrt:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin apache:x:48:48:Apache:/var/www:/sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin halt:x:7:0:halt:/sbin:/sbin/halt
45.命令diff,在最簡單的情況下,比較給定的兩個文件的不同。如果使用“-”代替“文件”參數,則要
比較的內容將來自標准輸入。diff命令是以逐行的方式,比較文本文件的異同處。如果該命令指定進行
目錄的比較,則將會比較該目錄中具有相同文件名的文件,而不會對其子目錄文件進行任何比較操作。
diff(選項)(參數)
-<行數>:指定要顯示多少行的文本。此參數必須與-c或-u參數一並使用;
-a或——text:diff預設只會逐行比較文本文件;
-b或--ignore-space-change:不檢查空格字符的不同;
-B或--ignore-blank-lines:不檢查空白行;
-c:顯示全部內容,並標出不同之處;
eg:[root@localhost home]# diff test/ test0/
Only in test0/: .bash_logout Only in test0/: .bash_profile Only in test0/: .bashrc Only in test/: date.file Only in test0/: .gnome2 Only in test0/: .mozilla Only in test/: test11 Only in test/: test11.tar Only in test/: test11.tar.bz2 Only in test/: test11.tar.gz Only in test/: test.txt.bz2 Only in test/: xaa Only in test/: xab Only in test/: xac Only in test/: xad Only in test/: xae Only in test/: xaf Only in test/: xag Only in test/: xah Only in test/: xai Only in test/: xaj
46.命令rev,將文件中的每行內容以字符為單位反序輸出,即第一個字符最后輸出,最后一個字符最先輸出,
依次類推。
rev(參數)
eg:[root@localhost home]# ls
123 foo01 foo03 foo05 py sort test0 user12 456 foo02 foo04 main.c snowy test user1 [root@localhost home]# cat main.c
printf ("Hello World!\n") [root@localhost home]# rev main.c
)"n\!dlroW olleH"( ftnirp [root@localhost home]#
47.命令ln,用來為文件創件連接,連接類型分為硬連接和符號連接兩種,默認的連接類型是硬連接。如果要
創建符號連接必須使用"-s"選項。
ln(選項)(參數)
鏈接文件分為:1.硬鏈接 2.軟鏈接
硬鏈接,直接建立一個inode鏈接到文件放置的塊區域,就是進行硬鏈接的時候,該文件內容沒有任何變化,
只是增加了一個指向這個文件的inode。硬鏈接的限制有2個,1.不能跨文件系統,因為不同的文件系統有
不同的inode table 2.不能鏈接目錄
軟鏈接,是建立一個獨立的文件,當讀取這個鏈接文件時,它會把讀取的行為轉發到該文件所鏈接的文件上。
命令ln ,-s該選項就是建立軟連接,如果不加-s就是硬鏈接。
源文件:指定連接的源文件。如果使用-s選項創建符號連接,則“源文件”可以是文件或者目錄。創建硬連接時,則“源文件”參數只能是文件; 目標文件:指定源文件的目標連接文件。
eg:[root@localhost home]# mkdir ceshi
[root@localhost home]# ls
123 ceshi foo02 foo04 main.c snowy test0 user12 456 foo01 foo03 foo05 py sort user1 [root@localhost home]# cd ceshi
[root@localhost ceshi]# cp /etc/passwd ./
[root@localhost ceshi]# ls
passwd [root@localhost ceshi]# ll
total 4
-rw-r--r--. 1 root root 1509 May 6 21:11 passwd [root@localhost ceshi]# ln passwd passwd-hard
[root@localhost ceshi]# ll
total 8
-rw-r--r--. 2 root root 1509 May 6 21:11 passwd -rw-r--r--. 2 root root 1509 May 6 21:11 passwd-hard [root@localhost ceshi]# du -sk
8 . [root@localhost ceshi]#
軟連接測試 [root@localhost home]# cd ceshi2
[root@localhost ceshi2]# cd /etc/passwd ./
bash: cd: /etc/passwd: Not a directory [root@localhost ceshi2]# cp /etc/passwd ./
[root@localhost ceshi2]# ln -s passwd passed-soft
[root@localhost ceshi2]# ll
total 4 lrwxrwxrwx. 1 root root 6 May 6 21:15 passed-soft -> passwd -rw-r--r--. 1 root root 1509 May 6 21:15 passwd [root@localhost ceshi2]# head n1 passed
head: cannot open `n1' for reading: No such file or directory head: cannot open `passed' for reading: No such file or directory [root@localhost ceshi2]# head n1 passwd
head: cannot open `n1' for reading: No such file or directory ==> passwd <== root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin [root@localhost ceshi2]# rm -f passwd
[root@localhost ceshi2]# head -n1 passwd-soft
head: cannot open `passwd-soft' for reading: No such file or directory [root@localhost ceshi2]# ll
total 0 lrwxrwxrwx. 1 root root 6 May 6 21:15 passed-soft -> passwd [root@localhost ceshi2]#
48. 命令ifconfig,用於配置和顯示Linux內核中網絡接口的網絡參數。用ifconfig命令配置的網卡信息,在網卡重啟
后機器重啟后,配置就不存在。
ifconfig(參數)
add<地址>:設置網絡設備IPv6的ip地址;
del<地址>:刪除網絡設備IPv6的IP地址;
down:關閉指定的網絡設備;
<硬件地址>:設置網絡設備的類型與硬件地址;
media<網絡媒介類型>:設置網絡設備的媒介類型;
mtu<字節>:設置網絡設備的MTU;
netmask<子網掩碼>:設置網絡設備的子網掩碼;
up:啟動指定的網絡設備;
IP地址:指定網絡設備的IP地址;
網絡設備:指定網絡設備的名稱。
eg:[root@localhost ceshi2]# clear
[root@localhost ceshi2]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:D0:8B:8C inet addr:192.168.100.44 Bcast:192.168.100.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fed0:8b8c/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2352 errors:0 dropped:0 overruns:0 frame:0 TX packets:1270 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:214617 (209.5 KiB) TX bytes:265181 (258.9 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:4 errors:0 dropped:0 overruns:0 frame:0 TX packets:4 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:240 (240.0 b) TX bytes:240 (240.0 b)
說明: eth0表示第一塊網卡,其中HWaddr表示網卡的物理地址,可以看到目前這個網卡的物理地址(MAC地址)是00:0C:29:D0:8B:8C 。
inet addr用來表示網卡的IP地址,此網卡的IP地址是192.168.100.44,廣播地址Bcast:192.168.100.255,掩碼地址Mask:255.255.255.0。
lo是表示主機的回壞地址,這個一般是用來測試一個網絡程序,但又不想讓局域網或外網的用戶能夠查看,只能在此台主機上運行和查看所用的網絡接口。
比如把 httpd服務器的指定到回壞地址,在瀏覽器輸入127.0.0.1就能看到你所架WEB網站了。但只是您能看得到,局域網的其它主機或用戶無從知道。
第一行:連接類型:Ethernet(以太網)HWaddr(硬件mac地址)。
第二行:網卡的IP地址、子網、掩碼。
第三行:UP(代表網卡開啟狀態)RUNNING(代表網卡的網線被接上)MULTICAST(支持組播)MTU:1500(最大傳輸單元):1500字節。 第四、五行:接收、發送數據包情況統計。
第七行:接收、發送數據字節數統計信息。
49.命令wc,用於統計文檔的行數,字符數或詞數。該命令常用的選項有-l(統計行數),-m(統計字符數),-w(統計詞數)
[root@localhost ceshi2]# wc /etc/passwd
33 47 1509 /etc/passwd [root@localhost ceshi2]# wc -l /etc/passwd
33 /etc/passwd [root@localhost ceshi2]# wc -m /etc/passwd
1509 /etc/passwd [root@localhost ceshi2]# wc -w /etc/passwd
47 /etc/passwd [root@localhost ceshi2]#
若wc后面不跟任何參數,直接跟文檔,會把行數,詞數和字符數依次輸出。
50.命令tr,可以對來自標准輸入的字符進行替換、壓縮和刪除。它可以將一組字符變成另一組字符,
經常用來編寫優美的單行命令,作用很強大。
tr(選項)(參數)
-c或——complerment:取代所有不屬於第一字符集的字符;
-d或——delete:刪除所有屬於第一字符集的字符;
-s或--squeeze-repeats:把連續重復的字符以單獨一個字符表示;
-t或--truncate-set1:先刪除第一字符集較第二字符集多出的字符。
eg:[root@localhost /]# head -n2 /etc/passwd | tr '[a-z]''[A-Z]'
tr: missing operand after `[a-z][A-Z]' Two strings must be given when translating. Try `tr --help' for more information. [root@localhost /]# echo "HELLO WORLD" | tr 'A-Z''a-z'
tr: missing operand after `A-Za-z' Two strings must be given when translating. Try `tr --help' for more information. [root@localhost /]# echo "HELLO WORLD" | tr 'A-Z' 'a-z'
hello world
tr還可以替換一個字符,代碼如下
[root@localhost /]# grep 'root' /etc/passwd | tr 'r' 'R'
Root:x:0:0:Root:/Root:/bin/bash opeRatoR:x:11:0:opeRatoR:/Root:/sbin/nologin [root@localhost /]#
51.命令uniq,用來刪除重復的行,該命令只有-c選項比較常用給,它表示統計重復的行數,並把數字寫在前面
uniq(選項)(參數)
eg:[root@localhost home]# mkdir test
[root@localhost home]# ls
123 ceshi foo01 foo03 foo05 py sort test0 user12 456 ceshi2 foo02 foo04 main.c snowy test user1 [root@localhost home]# cd test
[root@localhost test]# ls
[root@localhost test]# vi test.txt
[root@localhost test]# cat test.txt
111
222
111
333 [root@localhost test]# uniq test.txt
111
222
111
333 [root@localhost test]# sort test.txt | uniq
111
222
333 [root@localhost test]# sort test.txt | uniq -c
1
2 111
1 222
1 333 [root@localhost test]#
liunx的常用命令,暫時就總結到這里,可能有錯誤,請打擊給予指正。后續學到的知識點,會及時更新到此篇文章中。