1. 前言
2.文件目錄操做
ls——獲取文件目錄屬性
pwd——獲取當前工做目錄路徑
cd——切換工做目錄
tree——顯示目錄結構
file——顯示文件類別等信息
mv——移動文件目錄或修改文件目錄名稱
cp——拷貝文件目錄
scp、sz、rz——文件傳輸
touch、vim、重定向——建立文件
mkdir——建立目錄
rm、rmdir——刪除文件或目錄
ln——建立快捷方式
小測試php
3.打包壓縮文件
gzip/gunzip——gz壓縮解壓相關
zip/unzip——zip打包壓縮相關
bzip2/bunzip2——bz2壓縮相關
xz/unxz——xz壓縮相關
compress/uncompress/zcat——Z壓縮解壓相關
tar——tar包相關
4.文本內容處理
cat、tac——打印文件內容
more、less——分頁查看文本內容
head、tail——從頭或者從尾查看
wc——統計文本內容
nl——計算文件中的行號
grep——文本內容查找
cut——按需分割文件
sort——對文本內容進行排序
uniq——對文本內容進行去重計數
comm——比較兩個文件中的內容
vim——文本內容編輯
sed——文本的過濾和轉換(流處理)html
5.系統信息操做
fdisk、df、du——查看系統磁盤空間信息
lscpu——查看系統cpu配置狀況
free——查看系統內存使用狀況
top——查看系統實時運行狀態
ps——查看當前運行程序信息
kill——殺掉某個進程
date、cal——查看當前系統時間與日曆
bc——系統自帶簡易計算器
ifconfig、ping、route——查看系統網絡信息
wget——從網上下載數據
bg、fg、jobs——讓程序先後臺切換運行
which——查看命令的位置
who、w——查看誰在登錄,在幹什麼
history——查看歷史命令
man——查看命令函數等幫助文檔node
6. 重要備註
alias——命令重命名
|——管道操做符
.bashrc——環境變量設置
screen、nohup——程序不中斷運行linux
7. 小結nginx
總結web
1. 前言正則表達式
類別:算法
注意:shell
學習資料:vim
http://www.cnblogs.com/peida/archive/2012/12/05/2803591.html
http://www.oschina.net/translate/useful-linux-commands-for-newbies
http://www.php100.com/html/webkaifa/Linux/2009/1106/3485.html
http://blog.csdn.net/xiaoguaihai/article/details/8705992
Linux 重要的幾個熱鍵
注:進入終端後會顯示以下默認內容:
1
2
3
|
[Test@cpst01 ~]$
#其中 Test表示用戶名, cpst01表示服務器的主機名, ~表示用戶所在的當前目錄, $爲命令輸入提示符, 光標閃爍地方便可輸入命令。
|
2.文件目錄操做
基本的文件目錄操做是Linux的基礎知識,若是這些都不能掌握更何談後面的分析。
ls(list)意思就是列舉出指定文件或目錄的基本屬性(List Directory Contents),下面咱們直接執行ls命令來看下輸出結果。用的最多的Linux命令之一。
1
|
ls
|
上面的圖中列舉了文件和目錄的基本屬性,包括權限(r-read, w-write,x-executive),d-Directory。
下面詳細介紹下文件及目錄的權限屬性:
-rw-r–r–
r(Read,讀取):對文件而言,具備讀取文件內容的權限;對目錄來講,具備瀏覽目錄的權限。
w(Write,寫入):對文件而言,具備新增、修改文件內容的權限;對目錄來講,具備刪除、移動目錄內文件的權限。
x(eXecute,執行):對文件而言,具備執行文件的權限;對目錄了來講該用戶具備進入目錄的權限。
文件和目錄的權限表示,是用rwx這三個字符來表明全部者、用戶組和其餘用戶的權限。有時候,字符彷佛過於麻煩,所以還有另一種方法是以數字來表示權限,並且僅需三個數字。
r: 對應數值4
w: 對應數值2
x:對應數值1
-:對應數值0
其實很簡單,咱們將rwx當作二進制數(不懂的話百度),若是有則有1表示,沒有則有0表示,那麼rwx r-x r- -則能夠表示成爲:111 101 100,再將其每三位轉換成爲一個十進制數,就是754。
ls命令格式及參數:
1
2
3
4
5
6
7
8
9
10
11
12
|
命令格式:
ls [參數] 文件或目錄
經常使用參數:
-a (all) 顯示全部文件目錄(包括. 和..,以及隱藏文件目錄)名稱
-l (long)顯示詳細信息( . 和.., 以及隱藏文件目錄除外)
-h(human)以人類能夠更好的理解的形式展現信息
-F 將目錄下的目錄顯示出來(後面有/)
-R 遞歸地顯示全部內容
其它經常使用參數。
其中文件或目錄若是沒有指定的話默認是當前目錄
.
|
其它更詳細的參數:
應用舉例:
1
2
3
4
5
6
7
8
9
10
11
|
#列舉當前路徑下的全部內容
ls –la .
#以K M G 等形式展現文件或目錄大小
ls –lh
#遞歸(子目錄)顯示全部文件或目錄
ls –R
#多個參數聯合
ls –Slh (或者:ls -S -l -h -r)
|
直接運行pwd便可
1
2
|
[Test@cpst01 ~]$ pwd
/home/Test
|
cd(change directory) 就是切換當前工做目錄的意思。
使用示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# 進入根目錄下的public文件夾
cd /public
# 進入當前目錄的上一級目錄
cd ../
# 進入當前目錄的上兩級目錄
cd ../../
# 進入本身的home目錄
cd
# 進入本身的home目錄下的tt1目錄
cd ~/tt1
# 進入上一次的目錄
cd –
|
1
2
3
4
5
6
7
|
[Test@cpst01 ~]$ tree
.
├──
public_data
├──
public_script
└──
public_software
3 directories, 0 files
|
1
2
|
[Test@cpst01 ~]$ file ~/.bashrc
/home/Test/.bashrc: ASCII text
|
move (rename) files, 同時具有移動和重命名的功能。用的最多的Linux命令之一。
1
2
3
4
5
6
|
#重命名
[Test@cpst01 ~]$ ls
public_data public_script public_software pwd.help.txt
[Test@cpst01 ~]$ mv pwd.help.txt pwd.help.new.txt
[Test@cpst01 ~]$ ls
public_data public_script public_software pwd.help.new.txt
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#移動文件或目錄
[Test@cpst01 ~]$ ls
folder public_data public_script public_software pwd.help.new.txt
[Test@cpst01 ~]$ mv pwd.help.new.txt folder/
[Test@cpst01 ~]$ tree
.
├──
folder
│
└── pwd.help.new.txt
├──
public_data
├──
public_script
└──
public_software
4 directories, 1 file
|
1
2
3
4
5
|
#常見參數
-f do not prompt before overwriting (默認是會覆蓋的)
-i prompt before overwrite(提示是否覆蓋)
-u move only when the SOURCE file is newer than the destination file or when the destination file is missing(新文件覆蓋舊文件)
-v explain what is being done(如今過程)
|
copy files and directories, 複製文件或目錄。用的最多的Linux命令之一。
1
2
3
4
|
#常見參數
-i prompt before overwrite(提示是否覆蓋)
-R, -r copy directories recursively(複製整個文件夾及其子內容)
-f if an existing destination file cannot be opened, remove it and try again(之間覆蓋)
|
使用示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
[Test@cpst01 ~]$ tree
.
├──
folder
│
└── pwd.help.new.txt
├──
lyy_all.txt
├──
public_data
├──
public_script
├──
public_software
└──
pwd.help.new.txt
4 directories, 3 files
[Test@cpst01 ~]$ cp -i pwd.help.new.txt folder/
cp: overwrite `folder/pwd.help.new.txt'? n
[Test@cpst01 ~]$ cp -r folder/ public_data/
[Test@cpst01 ~]$ tree
.
├──
folder
│
└── pwd.help.new.txt
├──
lyy_all.txt
├──
public_data
│
└── folder
│
└── pwd.help.new.txt
├──
public_script
├──
public_software
└──
pwd.help.new.txt
5 directories, 4 files
|
scp、sz、rz是一組提供文件傳輸功能的命令,其中scp(secure copy)主要是針對遠程服務器之間的拷貝,而sz(send)是從服務器上將文件下載到本地電腦,而rz(receive)則是將本地電腦上的文件傳輸到服務器相應的路徑,有的服務器上不必定自帶了sz rz功能,須要利用root帳戶安裝相應的軟件lrzsz.x86_64(yum install lrzsz.x86_64)。
scp通常的格式是 scp [參數] [[user@]host1:]file1 [[user@]host2:]file2, 其中參數部分和cp相似,像最經常使用的-r
1
2
3
4
5
6
7
8
9
10
11
|
[Test@cpst01 ~]$ scp whgao@xxx.xxx.xxx.xxx:/home/whgao/slaves ./
The authenticity of host 'xxx.xxx.xxx.xxx (xxx.xxx.xxx.xxx)' can't be established.
RSA key fingerprint is ba:43:22:d4:e9:73:49:c8:82:04:7b:f6:32:5b:d0:0c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'xxx.xxx.xxx.xxx' (RSA) to the list of known hosts.
whgao@xxx.xxx.xxx.xxx's password: (此處輸入密碼, 也能夠經過設置免輸密碼)
slaves 100% 36 0.0KB/s 00:00
[Test@cpst01 ~]$ ls
folder lyy_all.txt public_data public_script public_software pwd.help.new.txt slaves
#若是想遠程傳輸文件夾, 則是 -r參數
|
sz 後面直接跟文件便可從服務器上進行下載,以下:
1
2
|
#將當期路徑下的slaves文件下載到本地
sz ./slaves
|
rz 在終端敲入rz後會彈出文件選擇器對話框(注:本地須要上傳的文件最好不要存放在中文路徑,不然會出錯)
scp在服務器之間進行數據的傳輸頗有效,rz sz則在與服務器進行小文件的傳輸不少效,大數據的傳輸可使用一些專業的傳輸軟件,如filezilla、winscp等。
上面講了不少與文件相關的命令,那麼如何去建立一個文件呢?(比如windows下單擊郵件->New->New Documents txt)。Linux中有不少建立文件的方式,下面介紹幾種。
touch——change file timestamps,touch的主要用途是改變文件的時間戳,但同時也是能夠去建立一個空白的文本文件。
1
2
3
|
#常見參數
-r use this file’s times instead of current time (利用file1的時間才更新file2的時)間
-t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time (直接設置指定的時間)
|
使用示例:
1
2
3
4
5
6
7
8
|
#建立一個不存在的文件
touch example.txt
#更新文件的時間戳使其相同
touch –r example1.txt example2.txt
#設置某個文件的時間戳爲指定值
touch –t 201211142234.50 log.log
|
vim——Vi IMproved, a programmers text editor, 是vi的一個升級版,強大功能的文本編輯工具,和咱們普通的視窗編輯器不一樣,用熟練了後效率會很高。vim能夠建立一個空白的文檔,也能夠打開編輯多個文檔,多以是指令的方式來完成相應的操做。這裏簡單說起下利用vim 建立一個文檔。
1
2
|
vim somefile
#打開後 再輸入 :wq! 就會在當前目錄下產生一個名爲somefile的文檔
|
重定向——咱們默認運行的一些命令,其輸出結果(包括標準正確輸出和標準錯誤輸出)通常是直接輸出到屏幕上去的,有時咱們但願把這些結果存在文件中,這就涉及到告終果的重定向輸出。
1
2
3
4
5
6
|
Someprogram xxx xxx 1>log.txt 2>errlog.txt (若是不存在log.txt errlog.txt 則先建立再寫內容,若是存在則直接覆蓋原來的內容)
1. 其中1> 表示的是標準正確輸出重定向, 2>表示的是標準錯誤輸出重定向。
2. 且 > 也能夠用>>替代, 表示的是不覆蓋原文件內容,而是將新的內容追加到原文件的後面
#好比下面的命令 就是將pwd命令的幫助文件 也就是man pwd的標準輸出結果 重定向 保存當一個文件中
man pwd >pwd.help.txt
|
mkdir——看着英文縮寫就應該知道該命令的意思了(make directory)常見文件夾。linux mkdir 命令用來建立指定的名稱的目錄,要求建立目錄的用戶在當前目錄中具備寫權限,而且指定的目錄名不能是當前目錄中已有的目錄。用的最多的Linux命令之一。
1
2
3
|
常見參數:
-m 模式,設定權限<模式> (相似 chmod),而不是 rwxrwxrwx 減 umask
-p 能夠是一個路徑名稱。此時若路徑中的某些目錄尚不存在,加上此選項後,系統將自動創建好那些尚不存在的目錄,即一次能夠創建多個目錄;
|
使用示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
[Test@cpst01 ~]$ mkdir t1
[Test@cpst01 ~]$ tree
.
├──
folder
│
└── pwd.help.new.txt
├──
lyy_all.txt
├──
public_data
│
└── folder
│
└── pwd.help.new.txt
├──
public_script
├──
public_software
├──
pwd.help.new.txt
├──
slaves
└──
t1
6 directories, 5 files
[Test@cpst01 ~]$ mkdir -p t1/t11
[Test@cpst01 ~]$ tree
.
├──
folder
│
└── pwd.help.new.txt
├──
lyy_all.txt
├──
public_data
│
└── folder
│
└── pwd.help.new.txt
├──
public_script
├──
public_software
├──
pwd.help.new.txt
├──
slaves
└──
t1
└── t11
|
rm——remove files and directory, 刪除文件和目錄,用的最多的Linux命令之一。
1
2
3
4
|
#常見參數
-f 忽略不存在的文件,從不給出提示。
-i 進行交互式刪除
-r, -R 指示rm將參數中列出的所有目錄和子目錄均遞歸地刪除。
|
使用示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
[Test@cpst01 ~]$ tree
.
├──
folder
│
└── pwd.help.new.txt
├──
lyy_all.txt
├──
public_data
│
└── folder
│
└── pwd.help.new.txt
├──
public_script
├──
public_software
├──
pwd.help.new.txt
├──
slaves
└──
t1
└── t11
7 directories, 5 files
[Test@cpst01 ~]$ rm -i lyy_all.txt
rm: remove regular file `lyy_all.txt'? n
[Test@cpst01 ~]$ rm -fr t1/ #f 參數和 r 參數聯合使用
[Test@cpst01 ~]$ tree
.
├──
folder
│
└── pwd.help.new.txt
├──
lyy_all.txt
├──
public_data
│
└── folder
│
└── pwd.help.new.txt
├──
public_script
├──
public_software
├──
pwd.help.new.txt
└──
slaves
5 directories, 5 files
|
rmdir——remove empty directory 主要是用來刪除空目錄的,能夠用rm –r dir 來替代。
鏈接也是實現數據共享,避免數據冗餘的一種方法。
軟連接:
1.軟連接,以路徑的形式存在。相似於Windows操做系統中的快捷方式
2.軟連接能夠 跨文件系統 ,硬連接不能夠
3.軟連接能夠對一個不存在的文件名進行連接
4.軟連接能夠對目錄進行連接
硬連接:
1.硬連接,以文件副本的形式存在。但不佔用實際空間。
2.不容許給目錄建立硬連接
3.硬連接只有在同一個文件系統中才能建立
1
2
|
#常見參數
-s make symbolic links instead of hard links(軟鏈接)
|
使用示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
[Test@cpst01 ~]$ tree
.
├──
folder
│
└── pwd.help.new.txt
├──
lyy_all.txt
├──
public_data
│
└── folder
│
└── pwd.help.new.txt
├──
public_script
├──
public_software
├──
pwd.help.new.txt
└──
slaves
5 directories, 5 files
[Test@cpst01 ~]$ ln -s lyy_all.txt lyy_all.ln.txt
[Test@cpst01 ~]$ tree
.
├──
folder
│
└── pwd.help.new.txt
├──
lyy_all.ln.txt -> lyy_all.txt
├──
lyy_all.txt
├──
public_data
│
└── folder
│
└── pwd.help.new.txt
├──
public_script
├──
public_software
├──
pwd.help.new.txt
└──
slaves
5 directories, 6 files
|
從本地電腦上傳一個txt文件到服務器,將該txt文件修改成example.txt,建立example目錄並將example.txt移動到該目錄。查看該文件的文件類別信息,拷貝一個example.txt並命名爲example.copy.txt。最後刪除example目錄及其所包含的全部內容。
1
2
3
4
5
6
7
|
rz
mv a.txt example.txt
mkdir example
mv example.txt example/
file example/example.txt
cp example/example.txt ./example.copy.txt
rm –fr example/
|
3.打包壓縮文件
文件及目錄的打包壓縮可使文檔更加整潔和減小磁盤存儲,如window上的zip,rar等相似。壓縮的算法有不少種,也對應下面不一樣的命令,可是基本的參數都很類似。
最簡單用法(通常壓縮文件後綴名爲gz):
解壓:gunzip somefile.gz,或者,gzip –d somefile.gz
壓縮:gzip somefile
1
2
3
4
5
6
7
8
9
10
|
#gzip 常見參數
-c 將壓縮後的內容之間打印到屏幕(能夠經過重定向保存到文件),原文件不動
-d 解壓 等同於 gunzip
-l 列舉已經被壓縮文件的特徵
-v 顯示處理過程
-S [後綴名] 替代默認的 .gz
#gunzip 常見參數
-c 將解壓後的內容之間打印到屏幕(能夠經過重定向保存到文件),gz文件不動
-v 顯示處理過程
|
gzip
使用示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
[Test@cpst01 ~]$ tree
.
├──
folder
│
└── pwd.help.new.txt
├──
public_data
│
└── folder
│
└── pwd.help.new.txt
├──
public_script
├──
public_software
├──
pwd.help.new.copy.txt
└──
pwd.help.new.txt
5 directories, 4 files
[Test@cpst01 ~]$ gzip pwd.help.new.* #直接壓縮, 原來的文件會被替換成帶.gz後綴
[Test@cpst01 ~]$ tree
.
├──
folder
│
└── pwd.help.new.txt
├──
public_data
│
└── folder
│
└── pwd.help.new.txt
├──
public_script
├──
public_software
├──
pwd.help.new.copy.txt.gz
└──
pwd.help.new.txt.gz
5 directories, 4 files
[Test@cpst01 ~]$ gzip -l pwd.help.new.txt.gz
compressed uncompressed ratio uncompressed_name
1039 2017 50.6% pwd.help.new.txt
[Test@cpst01 ~]$ gzip -cv pwd.help.new.copy.txt >test.gz #原文件不動
pwd.help.new.copy.txt: 50.6%
|
gunzip
使用示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
[Test@cpst01 ~]$ tree
.
├──
folder
│
└── pwd.help.new.txt
├──
public_data
│
└── folder
│
└── pwd.help.new.txt
├──
public_script
├──
public_software
├──
pwd.help.new.copy.txt
├──
pwd.help.new.txt.gz
└──
test.gz
5 directories, 5 files
[Test@cpst01 ~]$ gunzip -c test.gz >t.txt
[Test@cpst01 ~]$ gunzip pwd.help.new.txt.gz
[Test@cpst01 ~]$ tree
.
├──
folder
│
└── pwd.help.new.txt
├──
public_data
│
└── folder
│
└── pwd.help.new.txt
├──
public_script
├──
public_software
├──
pwd.help.new.copy.txt
├──
pwd.help.new.txt
├──
test.gz
└──
t.txt
5 directories, 6 files
|
上述介紹的gzip只是壓縮但並不能對多個文件或者文件夾打包,zip具備打包和壓縮功能,這和咱們在windows上常見的zip是同樣的,這種文件在windows和linux上是通用的,後綴名通常爲zip。(package and compress (archive) files)
使用語法: zip some.zip addfile1 addfile2 adddir1
1
2
3
4
5
6
7
8
9
|
#zip 常見參數
-r 將目錄下的內容都打包壓縮
-d 刪除打包文件中的某個文件
#unzip 常見參數
-l 列舉zip文件中的內容
-t 測試壓縮文件是否完整
-v 顯示壓制包內容
-d 解壓到某個路徑
|
使用示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[Test@cpst01 ~]$ tree
.
└──
pwd.help.new.txt
0 directories, 1 file
[Test@cpst01 ~]$ zip example.zip pwd.help.new.txt
adding: pwd.help.new.txt (deflated 50%)
[Test@cpst01 ~]$ tree
.
├──
example.zip
└──
pwd.help.new.txt
0 directories, 2 files
[Test@cpst01 ~]$ zip example.zip testdir/
adding: testdir/ (stored 0%)
[Test@cpst01 ~]$ zip -d example.zip pwd.help.new.txt
deleting: pwd.help.new.txt
|
使用示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
[Test@cpst01 ~]$ unzip -l example.zip
Archive: example.zip
Length Date Time Name
--------- ---------- ----- ----
2017 05-28-2015 10:16 pwd.help.new.txt
0 05-30-2015 11:20 testdir/
--------- -------
2017 2 files
[Test@cpst01 ~]$ unzip -t example.zip
Archive: example.zip
testing: pwd.help.new.txt OK
testing: testdir/ OK
No errors detected in compressed data of example.zip.
[Test@cpst01 ~]$ unzip -d ./ example.zip
Archive: example.zip
replace ./pwd.help.new.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
[Test@cpst01 ~]$ unzip -d testdir/ example.zip
Archive: example.zip
creating: testdir/testdir/
inflating: testdir/pwd.help.new.txt
|
與上述的zip不一樣,bzip2只能壓縮而不提供打包功能,這與gzip相同。(a block-sorting file compressor),通常後綴名爲.bz2。
最簡單語法:
壓縮:bzip2 [參數] file1 file2
解壓:bunzip2 file.bz2
1
2
3
4
5
6
7
|
#bzip2 常見參數
-c 將壓縮內容打印到屏幕
-t 檢查壓縮包完整性
#bunzip2 常見參數
-c 將解壓內容打印到屏幕
-t 檢查壓縮包完整性
|
使用示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
[Test@cpst01 ~]$ tree
.
├──
example.zip
├──
pwd.help.new.txt
├──
test
└──
testdir
├── pwd.help.new.txt
└── testdir
2 directories, 4 files
[Test@cpst01 ~]$ bzip2 pwd.help.new.txt test
[Test@cpst01 ~]$ tree
.
├──
example.zip
├──
pwd.help.new.txt.bz2
├──
test.bz2
└──
testdir
├── pwd.help.new.txt
└── testdir
2 directories, 4 files
[Test@cpst01 ~]$ bunzip2 -v *.bz2
pwd.help.new.txt.bz2: done
test.bz2: done
|
xz壓縮用的相對少一點,用法和bzip二、gz差很少。(a general-purpose data compression tool with command line syntax similar to gzip(1) and bzip2(1).)
使用示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
[Test@cpst01 ~]$ tree
.
├──
example.zip
├──
pwd.help.new.txt
├──
test
└──
testdir
├── pwd.help.new.txt
└── testdir
2 directories, 4 files
[Test@cpst01 ~]$ xz -v pwd.help.new.txt test
pwd.help.new.txt (1/2)
100.0 % 1,104 B / 2,017 B = 0.547
test (2/2)
100.0 % 1,104 B / 2,017 B = 0.547
[Test@cpst01 ~]$ tree
.
├──
example.zip
├──
pwd.help.new.txt.xz
├──
testdir
│
├── pwd.help.new.txt
│
└── testdir
└──
test.xz
2 directories, 4 files
[Test@cpst01 ~]$ unxz -v *.xz
pwd.help.new.txt.xz (1/2)
100.0 % 1,104 B / 2,017 B = 0.547
test.xz (2/2)
100.0 % 1,104 B / 2,017 B = 0.547
[Test@cpst01 ~]$ tree
.
├──
example.zip
├──
pwd.help.new.txt
├──
test
└──
testdir
├── pwd.help.new.txt
└── testdir
2 directories, 4 files
|
compress 是一個至關古老的 unix 檔案壓縮指令,壓縮後的檔案會加上一個 .Z 延伸檔名以區別未壓縮的檔案,壓縮後的檔案能夠以 uncompress 解壓。若要將數個檔案壓成一個壓縮檔,必須先將檔案 tar 起來再壓縮。因爲 gzip 能夠產生更理想的壓縮比例,通常人多已改用 gzip 爲檔案壓縮工具。
1
2
3
4
|
#compress/uncompress 常見參數參數:
-c 輸出結果至標準輸出設備(通常指熒幕)
-f 強迫寫入檔案,若目的檔已經存在,則會被覆蓋 (force)
-v 將程序執行的訊息印在熒幕上 (verbose)
|
有的服務器上可能只有它的幫助文檔,可是並無其命令。(需root用戶安裝:yum install ncompress.x86_64)
使用示例:
1
2
3
4
5
6
7
8
|
[Test@cpst01 ~]$ ls
example.zip pwd.help.new.txt test testdir
[Test@cpst01 ~]$ compress -v pwd.help.new.txt
pwd.help.new.txt: -- replaced with pwd.help.new.txt.Z Compression: 36.04%
[Test@cpst01 ~]$ uncompress -v pwd.help.new.txt.Z
pwd.help.new.txt.Z: -- replaced with pwd.help.new.txt
[Test@cpst01 ~]$ ls
example.zip pwd.help.new.txt test testdir
|
tar——tar包相關
tar是很是重要打包壓縮命令,由於它能夠打包解包,並且還能夠調用其它命令來進行壓縮,是一個綜合性的命令。
單純的tar是打包而不是壓縮
解包:tar -xvf somefile.tar,
打包:tar -cvf somefile.tar dir_or_file_list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#基本參數:
-c 建立新的包,
-x 展開內部文件,
-t 顯示tar包內容
-v顯示正在處理的文件,
-f 指定包文件
#tar命令經過調用適當的參數來實如今打包完成的基礎上進行相應的壓縮處理,從而簡化打包壓縮操做。
#打包壓縮後綴名:.tar.gz(.tgz)、.tar.bz2(.tbz2)、tar.xz、.tar.Z
#調用對應的壓縮解壓命令參數:
-z 檢查是否具備gzip屬性,也可用來調用gzip/gunzip命令 (這個用的最多)
-j 檢查是否具備bzip2屬性,也可用來調用bzip2/bunzip2命令
-J 檢查是否具備xz屬性,也可用來調用xz/unxz命令
-Z 檢查是否具備compress屬性,也可用來調用compress命令
|
使用示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
[Test@cpst01 ~]$ ls
example.zip pwd.help.new.txt test testdir
[Test@cpst01 ~]$ tar -cvf create.tar pwd.help.new.txt testdir/ test
pwd.help.new.txt
testdir/
testdir/pwd.help.new.txt
testdir/testdir/
test
[Test@cpst01 ~]$ ls
create.tar example.zip pwd.help.new.txt test testdir
[Test@cpst01 ~]$ tar -tvf create.tar
-rw-rw-r-- Test/Test 2017 2015-05-28 10:16 pwd.help.new.txt
drwxrwxr-x Test/Test 0 2015-05-30 15:23 testdir/
-rw-rw-r-- Test/Test 2017 2015-05-28 10:16 testdir/pwd.help.new.txt
drwxrwxr-x Test/Test 0 2015-05-30 11:20 testdir/testdir/
-rw-rw-r-- Test/Test 2017 2015-05-30 15:05 test
[Test@cpst01 ~]$ ls
example.zip pwd.help.new.txt test testdir
[Test@cpst01 ~]$ tar -zcvf create.tar.gz pwd.help.new.txt test testdir/
pwd.help.new.txt
test
testdir/
testdir/pwd.help.new.txt
testdir/testdir/
[Test@cpst01 ~]$ tree
.
├──
create.tar.gz
├──
example.zip
├──
pwd.help.new.txt
├──
test
└──
testdir
├── pwd.help.new.txt
└── testdir
2 directories, 5 files
[Test@cpst01 ~]$ tar -jcvf create.tar.bz2 pwd.help.new.txt test testdir/
pwd.help.new.txt
test
testdir/
testdir/pwd.help.new.txt
testdir/testdir/
[Test@cpst01 ~]$ ls
create.tar.bz2 create.tar.gz example.zip pwd.help.new.txt test testdir
|
你們可能會問,windows下經常使用的rar在linux下有對應的命令嗎?因爲rar在Linux不經常使用因此不自帶rar相關的命令,須要另外安裝,這裏就不講解了。
4.文本內容處理
前兩節講解的主要是如何對文件系統的中的文件和目錄進行操做(包括瀏覽、建立、打包、壓縮等),那麼這節就要講解下如何去文件的內容進行操做(包括查看、統計、搜索、編輯等)。
cat – concatenate files and print on the standard output
tac – concatenate and print files in reverse
1
2
3
4
5
6
|
#cat 參數(tac與cat不共參數):
-A 打印全部內容包括隱藏字符(等同於-vET)
-b 給非空行遍行號
-E 在每行行末顯示$
-n 給全部行顯示行號
-T 顯示tab字符爲^I
|
使用示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
[Test@cpst01 ~]$ cat -A short.txt
1 line1$
$
1 xxx$
$
2 xxsdf$
$
3 sdfdshfh$
$
4 rtrtyy$
[Test@cpst01 ~]$ cat -n short.txt
1 1 line1
2
3 1 xxx
4
5 2 xxsdf
6
7 3 sdfdshfh
8
9 4 rtrtyy
[Test@cpst01 ~]$ cat -b short.txt
1 1 line1
2 1 xxx
3 2 xxsdf
4 3 sdfdshfh
5 4 rtrtyy
|
more命令,功能相似 cat ,cat命令是整個文件的內容從上到下顯示在屏幕上。 more會以一頁一頁的顯示方便使用者逐頁閱讀,並且還有搜尋字串的功能 。more命令從前向後讀取文件,所以在啓動時就加載整個文件。
more 命令格式:
more [-dlfpcsu ] [-num ] [+/ pattern] [+ linenum] [file … ]
1
2
3
4
5
6
7
8
9
10
11
12
|
#more 常見參數
+n 從笫n行開始顯示
-n 定義屏幕大小爲n行
+/pattern 在每一個檔案顯示前搜尋該字串(pattern),而後從該字串前兩行
以後開始顯示
-c 從頂部清屏,而後顯示
-d 提示「Press space to continue,’q’ to quit(按空格鍵繼
續,按q鍵退出)」,禁用響鈴功能
-l 忽略Ctrl+l(換頁)字符
-p 經過清除窗口而不是滾屏來對文件進行換頁,與-c選項類似
-s 把連續的多個空行顯示爲一行
-u 把文件內容中的下畫線去掉
|
more 經常使用操做命令(用more打開文件後,操做鍵盤):
Enter 向下n行,須要定義。默認爲1行
Ctrl+F 向下滾動一屏
空格鍵 向下滾動一屏
Ctrl+B 返回上一屏
= 輸出當前行的行號
:f 輸出文件名和當前行的行號
V 調用vi編輯器
!命令 調用Shell,並執行命令
q 退出more
使用示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
[Test@cpst01 ~]$ cat short.txt
1 line1
1 xxx
2 xxsdf
3 sdfdshfh
4 rtrtyy
[Test@cpst01 ~]$ more short.txt
1 line1
1 xxx
2 xxsdf
3 sdfdshfh
4 rtrtyy
[Test@cpst01 ~]$ more +3 short.txt
1 xxx
2 xxsdf
3 sdfdshfh
4 rtrtyy
[Test@cpst01 ~]$ more +/ds short.txt
...skipping
2 xxsdf
3 sdfdshfh
4 rtrtyy
[Test@cpst01 ~]$ more -3 short.txt
1 line1
1 xxx
--More--(32%)
|
less – opposite of more, 應該說是linux正統查看文件內容的工具,功能極其強大。less 的用法比起 more 更加的有彈性。若使用了 less 時,就可使用 [pageup] [pagedown] 等按鍵的功能來往前日後翻看文件,更容易用來查看一個文件的內容!除此以外,在 less 裏頭能夠擁有更多的搜索功能,不止能夠向下搜,也能夠向上搜。
less 命令格式:
less [參數] 文件(s)
1
2
3
4
5
|
#less 常見命令參數:
-N 顯示每行的行號
-s 顯示連續空行爲一行
-S 行過長時間將超出部分捨棄
-x <數字> 將「tab」鍵顯示爲規定的數字空格
|
less 經常使用操做命令(用less打開文件後,操做鍵盤):
less 與 more 相似,但使用 less 能夠隨意瀏覽文件,並且 less 在查看以前不會加載整個文件。
/字符串:向下搜索「字符串」的功能
?字符串:向上搜索「字符串」的功能
n:重複前一個搜索(與 / 或 ? 有關)
N:反向重複前一個搜索(與 / 或 ? 有關)
空格鍵 滾動一行
回車鍵 滾動一頁
[pagedown]: 向下翻動一頁
[pageup]: 向上翻動一頁
附加備註
1.全屏導航
ctrl + F – 向前移動一屏
ctrl + B – 向後移動一屏
ctrl + D – 向前移動半屏
ctrl + U – 向後移動半屏
2.單行導航
j – 向前移動一行
k – 向後移動一行
3.其它導航
G – 移動到最後一行
g – 移動到第一行
q / ZZ – 退出 less 命令
4.其它有用的命令
v – 使用配置的編輯器編輯當前文件
h – 顯示 less 的幫助文檔
&pattern – 僅顯示匹配模式的行,而不是整個文件
head – output the first part of files
tail – output the last part of files
head 用來顯示檔案的開頭至標準輸出中,默認head命令打印其相應文件的開頭10行。
tail 命令從指定點開始將文件寫到標準輸出。
1
2
3
4
5
6
7
8
9
10
11
12
|
#head 令參數:
-q 隱藏文件名
-v 顯示文件名
-c <字節> 顯示字節數
-n <行數> 顯示的行數
#tail 命令參數:
-f 循環讀取
-q 不顯示處理信息
-v 顯示詳細的處理信息
-c <數目> 顯示的字節數
-n <行數> 顯示行數
|
使用tail命令的-f選項能夠方便的查閱正在改變的日誌文件, tail -f filename會把filename裏最尾部的內容顯示在屏幕上,而且不但刷新,使你看到最新的文件內容。
此wc非彼wc,wc – print newline, word, and byte counts for each file, 打印每一個文件的行數、字數、字節數。
wc [OPTION]… [FILE]
1
2
3
4
5
|
#wc 常見命令參數:
-c 打印字節數
-m 打印字符數
-l 打印行數
-w 打印單詞數
|
使用示例:
1
2
3
4
5
6
7
8
9
10
|
[Test@cpst01 ~]$ wc short.txt
9 10 46 short.txt
[Test@cpst01 ~]$ wc -l short.txt
9 short.txt
[Test@cpst01 ~]$ wc -c short.txt
46 short.txt
[Test@cpst01 ~]$ wc -m short.txt
46 short.txt
[Test@cpst01 ~]$ wc -w short.txt
10 short.txt
|
nl命令在linux系統中用來計算文件中行號。nl 能夠將輸出的文件內容自動的加上行號!其默認的結果與 cat -n 有點不太同樣, nl 能夠將行號作比較多的顯示設計,包括位數與是否自動補齊 0 等等的功能。
nl [選項]… [文件]…
1
2
3
4
5
6
7
8
9
|
#nl 常見參數
-b :指定行號指定的方式,主要有兩種:
-b a :表示不管是否爲空行,也一樣列出行號(相似 cat -n);
-b t :若是有空行,空的那一行不要列出行號(默認值);
-n :列出行號表示的方法,主要有三種:
-n ln :行號在螢幕的最左方顯示;
-n rn :行號在本身欄位的最右方顯示,且不加 0 ;
-n rz :行號在本身欄位的最右方顯示,且加 0 ;
-w [n]:行號欄位的佔用的位數(默認是6位)。
|
使用示例:
1
2
3
4
5
6
7
8
9
10
|
[Test@cpst01 ~]$ nl -n rz -b a -w 4 short.txt
0001 1 line1
0002
0003 1 xxx
0004
0005 2 xxsdf
0006
0007 3 sdfdshfh
0008
0009 4 rtrtyy
|
1
2
3
4
5
6
7
8
9
10
|
#grep 常見參數
-f <規則文件>
指定規則文件,其內容含有一個或多個規則樣式,讓grep查
找符合規則條件的文件內容,格式爲每行一個規則樣式。
-c 計算符合樣式的行數。
-i 忽略字符大小寫的差異。
-l 列出文件內容符合指定的樣式的文件名稱
-L 列出文件內容不符合指定的樣式的文件名稱。
-v 顯示不包含匹配文本的全部行。
-n 在顯示符合樣式的那一行以前,標示出該行的編號。
|
其中-c -i -v -n 較經常使用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#grep 常見pattern模式
grep的規則表達式:
^ #錨定行的開始 如:'^grep'匹配全部以grep開頭的行。
$ #錨定行的結束 如:'grep$'匹配全部以grep結尾的行。
. #匹配一個非換行符的字符 如:'gr.p'匹配gr後接一個任意字符,而後是p。
* #匹配零個或多個先前字符 如:’ *grep'匹配全部一個或多個空格後緊跟grep的行。
.* #一塊兒用表明任意字符。
[] #匹配一個指定範圍內的字符,如'[Gg]rep'匹配Grep和grep。
[^] #匹配一個不在指定範圍內的字符,如:'[^A-FH-Z]rep'匹配不包含A-R和T-Z的一個字母開頭,緊跟rep的行。
\(..\) #標記匹配字符,如'\(love\)',love被標記爲1。
\< #錨定單詞的開始,如:'\<grep'匹配包含以grep開頭的單詞的行。
\> #錨定單詞的結束,如'grep\>'匹配包含以grep結尾的單詞的行。
x\{m\} #重複字符x,m次,如:'0\{5\}'匹配包含5個o的行。
x\{m,\} #重複字符x,至少m次,如:'o\{5,\}'匹配至少有5個o的行。
x\{m,n\} #重複字符x,至少m次,很少於n次,如:'o\{5,10\}'匹配5--10個o的行。
\w #匹配文字和數字字符,也就是[A-Za-z0-9],如:'G\w*p'匹配以G後跟零個或多個文字或數字字符,而後是p。
\W #\w的反置形式,匹配一個或多個非單詞字符,如點號句號等。
\b #單詞鎖定符,如: '\bgrep\b'只匹配grep。
|
使用示例:
1
2
3
|
#統計passwd中含有bash的行數
[Test@cpst01 ~]$ grep -c "bash" /etc/passwd
40
|
grep在後面的平常操做中使用的很頻繁,必須掌握。
cut——按需分割文件
cut是一個選取命令,就是將一段數據通過分析,取出咱們想要的。通常來講,選取信息一般是針對「行」來進行分析的,並非整篇信息分析的。cut 命令從文件的每一行剪切字節、字符和字段並將這些字節、字符和字段寫至標準輸出。若是不指定 File 參數,cut 命令將讀取標準輸入。必須指定 -b、-c 或 -f 標誌之一。
1
2
3
|
#cut 常見參數
-b :以字節爲單位進行分割。這些字節位置將忽略多字節字符邊 界,除非也指定了 -n 標誌。-c :以字符爲單位進行分割。-d :自定義分隔符,默認爲製表符。(delimiter)-f :與-d一塊兒使用,指定顯示哪一個區域。(fields)-n :取消分割多字節字符。僅和 -b 標誌一塊兒使用。若是字符的 最後一個字節落在由 -b 標誌的 List 參數指示的<br />範圍之 內,該字符將被寫出;不然,該字符將被排除。
--output-delimiter=STRING 指定輸出時的分隔符
|
使用示例:
1
2
3
4
5
6
7
8
9
10
|
[Test@cpst01 ~]$ cut -d " " -f 1,2 --output-delimiter=";" short.txt
1;line1
1;xxx
2;xxsdf
3;sdfdshfh
4;rtrtyy
|
sort – sort lines of text files, 對文本內容按行或者(某一個字段進行排序),sort將文件的每一行做爲一個單位,相互比較,比較原則是從首字符向後,依次按ASCII碼值進行比較,最後將他們按升序(默認)輸出。
sort [OPTION]… [FILE]…
1
2
3
4
5
6
7
8
9
|
#sort 常見參數
-b 忽略每行前面開始出的空格字符。
-c 檢查文件是否已經按照順序排序。
-f 排序時,忽略大小寫字母。
-n 依照數值的大小排序。
-o <輸出文件> 將排序後的結果存入指定的文件。
-r 以相反的順序來排序。
-t <分隔字符> 指定排序時所用的欄位分隔字符。
-k 選擇以哪一個區間進行排序。 n,p 第n個字段到第p個字段, n.s,p.t 第n個字段的第s個字符到第p個字段的第t個字符
|
其中的-k -n -r都很重要。
使用示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
[Test@cpst01 ~]$ cat short.txt
1 line1
1 xxx
2 xxsdf
3 sdfdshfh
4 rtrtyy
[Test@cpst01 ~]$ sort short.txt
1 line1
1 xxx
2 xxsdf
3 sdfdshfh
4 rtrtyy
[Test@cpst01 ~]$ sort -k2,2 short.txt
1 line1
4 rtrtyy
3 sdfdshfh
2 xxsdf
1 xxx
#也可多列排序, -k2,2 -k3,3 先按第二列排, 有相同的則以第三列排
|
1
2
3
4
5
6
|
sort -c /etc/passwd
sort /etc/passwd
sort –f /etc/passwd
sort –t ‘:’ –k1,1 /etc/passwd
sort –t ‘:’ –k 4,4n /etc/passwd
sort –t ‘:’ –k 4,4nr –k1,1 /etc/passwd
|
uniq – report or omit repeated lines, 報告或者刪除重複的行,接受的輸入文件必須是已經排序了的。
uniq [OPTION]… [INPUT [OUTPUT]]
1
2
3
4
5
6
7
|
#uniq 常見參數
-c 統計重複行的數量
-d 只打印重複行
-s <N>在比較重複序列時,忽略每行前N各字符
-f <N> 在比較重複序列時,忽略每行前N各字段
-i 在計算重複行時忽略大小寫問題
-u 只打印出現一次的行
|
1
2
3
4
|
[Test@cpst01 ~]$cut -d ":" -f 4 /etc/passwd 1>groups.id.txt 2>errlog
[Test@cpst01 ~]$sort groups.id.txt > groups.id.sort.txt
[Test@cpst01 ~]$sort -k1,1n groups.id.txt -o groups.id.sort.txt
[Test@cpst01 ~]$uniq -c groups.id.sort.txt
|
comm – compare two sorted files line by line, 按行比較兩個已經排好序的文件。主要是用來找文件間的共同行或者獨有行。
comm [OPTION]… FILE1 FILE2
1
2
3
4
|
#comm 常見參數
-1 不輸出FILE1中惟一的行
-2 不輸出FILE2中惟一的行
-3 不輸出FILE1和FILE2中都有的行
|
使用示例:
1
2
3
|
comm -12 FILE1 FILE2 #打印FILE1 FILE2中共有的行
comm -13 FILE1 FILE2 #打印FILE2中d獨有的行
comm -23 FILE1 FILE2 #打印FILE1中d獨有的行
|
通常模式:在Linux終端中輸入「vim 文件名」就進入了通常模式,但不能輸入文字。
編輯模式:在通常模式下按i就會進入編輯模式,此時就能夠寫程式,按Esc可回到通常模式。
末行命令模式:在通常模式下按:就會進入命令模式,左下角會有一個冒號出現,此時能夠敲入命令並執行。
vim的功能遠不止這些,具體能夠看下相應的博客。
http://www.cnblogs.com/tzhangofseu/archive/2011/12/17/2290955.html
http://blog.sina.com.cn/s/blog_50d43ad50100osd6.html
vim打開文件:
vim somefile
編輯模式指令
i | 在光標前進入 | I | 在行首進入 |
a | 在光標後進入 | A | 在行末進入 |
o | 在光標所在行的下一行行首進入 | O | 在光標所在行的上一行行首進入 |
cw | 修改一個單詞 | dw | 刪除一個單詞 |
dd | 刪除一行 | u | 撤銷上一次操做 |
CTRL+r | 從新上一次操做 | GG | 光標快速回到文件底部 |
gg | 光標快速回到文件首部 | 4dd | 刪除光標所在行開始的4行 |
p | 在光標下一行進行粘貼 | yy | 複製一行 |
d$ | 刪除光標後面的字符都行末 |
鍵盤上的HOME END等快捷鍵依舊起做用
末行命令模式
q | 退出 | q! | 強制退出 |
w | 保存 | wq | 保存並退出 |
wq! | 強制保存並退出 | set nonu | 不顯示行數 |
set autoindent | 自動對齊 | set noautoindent | 取消自動對齊 |
1,5s/mail/gmail/g | 全局替換(1-5行) | 1,$s/mail/gmail/g | 全局替換 |
sed – stream editor for filtering and transforming text
sed [-nefr] [動做] 文件
1
2
3
4
5
6
7
8
9
10
|
#sed 常見參數
-n 使用安靜(silent)模式。在通常 sed 的用法中,全部來自
STDIN的數據通常都會被列出到終端上。但若是加上 -n 參數
後,只有通過sed 特殊處理的那一行(或者動做)纔會被列出來。
-e 直接在命令列模式上進行 sed 的動做編輯;
-f 直接將 sed 的動做寫在一個文件內, -f filename 則能夠運行
filename 內的 sed 動做;
-r sed 的動做支持的是延伸型正規表示法的語法。(默認是基礎正
規表示法語法)
-i 直接修改讀取的文件內容,而不是輸出到終端。
|
動做說明: [n1[,n2]]function n1, n2 :不見得會存在,通常表明『選擇進行動做的行數』,舉例來講,若是個人動做是須要在 10 到 20 行之間進行的,則是『 10,20[動做行爲] 』 ,不設置n1 和 n2 則表示是對整個流進行處理。
a :新增, a 的後面能夠接字串,而這些字串會在新的一行出現(目前的下一行)~
c :取代, c 的後面能夠接字串,這些字串能夠取代 n1,n2 之間的行!
d :刪除,由於是刪除啊,因此 d 後面一般不接任何咚咚;
i :插入, i 的後面能夠接字串,而這些字串會在新的一行出現(目前的上一行);
p :列印,亦即將某個選擇的數據印出。一般 p 會與參數 sed -n 一塊兒運行~
s :取代,能夠直接進行取代的工做哩!一般這個 s 的動做能夠搭配正規表示法!例如 1,20s/old/new/g 就是啦! 其中g爲全行匹配替換
使用示例:
1
2
3
4
5
6
7
8
9
10
11
|
sed 4d comm.1.txt 或者 sed ‘4d’ comm.1.txt
刪除
comm.1.txt中的第四行後再打印出來(原文件內容不變)
sed -n '/z/p' comm.1.txt
只打印含有
z的行
sed -e 's/z/replace/' comm.1.txt
替換每行第一個出現的
z爲replace
sed –n ‘1,3p’ comm.1.txt
只打印
1-3行內容
|
5.系統信息操做
fdisk、df、du——查看系統磁盤空間信息
df –h 以人類易懂的方式顯示磁盤使用狀況
du ./ -h以人類易懂的方式顯示當前路徑下的文件大小狀況(明細)
du ./ -sh以人類易懂的方式彙總當前路徑下的文件大小狀況
http://www.cnblogs.com/peida/archive/2012/12/07/2806483.html
http://www.cnblogs.com/peida/archive/2012/12/10/2810755.html
使用示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[Test@cpst01 ~]$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_cpst01-lv_root
84G 7.0G 73G 9% /
tmpfs 16G 300K 16G 1% /dev/shm
/dev/sda1 485M 37M 423M 8% /boot
/dev/mapper/vg_cpst01-lv_home
1.3T 660G 547G 55% /home
/dev/mapper/vg_cpst01-lv_var
433G 7.2G 404G 2% /var
[Test@cpst01 ~]$ du ./ -sh
76K ./
|
lscpu – display information about the CPU architecture
使用示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
[Test@cpst01 ~]$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 24
On-line CPU(s) list: 0-23
Thread(s) per core: 2
Core(s) per socket: 6
Socket(s): 2
NUMA node(s): 2
Vendor ID: GenuineIntel
CPU family: 6
Model: 45
Stepping: 7
CPU MHz: 1900.069
BogoMIPS: 3799.49
Virtualization: VT-x
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 15360K
NUMA node0 CPU(s): 0,2,4,6,8,10,12,14,16,18,20,22
NUMA node1 CPU(s): 1,3,5,7,9,11,13,15,17,19,21,23
|
Display amount of free and used memory in the system, 顯示系統中的中內存和使用的內存。
1
2
3
4
5
|
#free 常見參數
-b 以byte形式展現
-k 以kb形式展現
-m 以mb形式展現
-g 以Gb形式展現
|
使用示例:
1
2
3
4
5
|
[Test@cpst01 ~]$ free -g
total used free shared buffers cached
Mem: 31 3 27 0 0 1
-/+ buffers/cache: 1 29
Swap: 47 0 47
|
有時咱們很關心繫統正在運行什麼程序以及系統如今的存儲內存消耗等,這時就須要top命令來實時監測系統信息。首先來看下top命令的界面,也就是系統的一些狀態信息:
任務區域中每列的具體含義:
PID — 進程id
USER — 進程全部者
PR — 進程優先級
NI — nice值。負值表示高優先級,正值表示低優先級
VIRT — 進程使用的虛擬內存總量,單位kb。VIRT=SWAP+RES
RES — 進程使用的、未被換出的物理內存大小,單位kb。RES=CODE+DATA
SHR — 共享內存大小,單位kb
S — 進程狀態。D=不可中斷的睡眠狀態 R=運行 S=睡眠 T=跟蹤/中止 Z=殭屍進程
%CPU — 上次更新到如今的CPU時間佔用百分比
%MEM — 進程使用的物理內存百分比
TIME+ — 進程使用的CPU時間總計,單位1/100秒
COMMAND — 進程名稱(命令名/命令行)
top命令運行後的相關指令:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
Global_defaults
’A’ - Alt display Off (full-screen)
* ’d’ - Delay time 3.0 seconds
’I’ - Irix mode On (no, ’solaris’ smp)
* ’p’ - PID monitoring Off
* ’s’ - Secure mode Off (unsecured)
’B’ - Bold disable Off
Summary_Area_defaults
’l’ - Load Avg/Uptime On (thus program name)
’t’ - Task/Cpu states On (1+1 lines, see ’1’)
’m’ - Mem/Swap usage On (2 lines worth)
’1’ - Single Cpu On (thus 1 line if smp)
Task_Area_defaults
’b’ - Bold hilite On (not ’reverse’) running pid
* ’c’ - Command line Off (name, not cmdline)
* ’H’ - Threads Off (show all threads)
* ’i’ - Idle tasks On (show all tasks)
’R’ - Reverse sort On (pids high-to-low)
* ’S’ - Cumulative time Off (no, dead children)
’x’ - Column hilite Off (no, sort field)
’y’ - Row hilite On (yes, running tasks)
’z’ - color/mono Off (no, colors)
|
針對top命令掌握經常使用的幾個指令便可,如b,c,1等,更多信息請看下面的這篇博客:http://www.cnblogs.com/peida/archive/2012/12/24/2831353.html。
ps——查看當前運行程序信息
ps – report a snapshot of the current processes. 報告當前進程的快照。ps命令是Process Status的縮寫。
ps [options]
1
2
3
4
5
6
7
8
9
10
11
12
|
#ps 常見參數
a 顯示全部進程
-a 顯示同一終端下的全部程序
-A 顯示全部進程
-e 等於「-A」
e 顯示環境變量
f 顯示程序間的關係
r 顯示當前終端的進程
T 顯示當前終端的全部程序
u 指定用戶的全部進程
-au 顯示較詳細的資訊
-aux (或 aux)顯示全部包含使用者的進程
|
使用示例:
1
2
3
4
|
ps -ef
ps axu
ps -ejH
ps axZ
|
有時須要終止一些錯誤程序的運行就須要利用到kill命令,看其單詞意思就知道它的功能。
格式:
kill [ -s signal | -p ] [ -a ] pid …
kill -l [ signal ]
1
2
3
4
5
6
|
#kill 常見參數
-s:指定發送的信號。
-p:模擬發送信號。
-l:指定信號的名稱列表。
pid:要停止進程的ID號。
signal:表示信號。
|
使用示例:
1
2
3
4
5
6
|
停止一個進程標識號爲
324的進程:
進程標識號(
PID)能夠經過top、ps等命令獲取
#kill 324
強行停止一個進程標識號爲
324的進程:
#kill -9 324
|
date和cal命令平時使用的也不是不少,但有時能夠用來計算程序運行花了多長時間,這裏簡單介紹下:
date – print or set the system date and time
cal – displays a calendar
date [參數]… [+格式]
cal [-smjy13] [[[day] month] year]
詳細內容可參考下面的博客:
http://www.cnblogs.com/peida/archive/2012/12/13/2815687.html
http://www.cnblogs.com/peida/archive/2012/12/14/2817473.html
使用示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[Test@cpst01 ~]$ date
[Test@cpst01 ~]$ date
Thu Jun 4 16:17:44 CST 2015
[Test@cpst01 ~]$ date '+%c'
Thu 04 Jun 2015 04:17:51 PM CST
[Test@cpst01 ~]$ date '+%d'
04
[Test@cpst01 ~]$ date '+%y'
15
[Test@cpst01 ~]$ date '+%m'
06
[Test@cpst01 ~]$ cal -3
May 2015 June 2015 July 2015
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 1 2 3 4 5 6 1 2 3 4
3 4 5 6 7 8 9 7 8 9 10 11 12 13 5 6 7 8 9 10 11
10 11 12 13 14 15 16 14 15 16 17 18 19 20 12 13 14 15 16 17 18
17 18 19 20 21 22 23 21 22 23 24 25 26 27 19 20 21 22 23 24 25
24 25 26 27 28 29 30 28 29 30 26 27 28 29 30 31
31
[Test@cpst01 ~]$
|
bc是一個很實用的命令,簡易的計算器程序。
bc – An arbitrary precision calculator language
用法:bc [ -hlwsqv ] [long-options] [ file … ]
1
2
3
|
#bc 常見參數
-l 加載標準數學庫(三角函數、指數、log等)
-q 不在啓動時打印一些歡迎信息
|
運算符: + – / * % ^
調用標準數學庫:s(x)、c(x)、a(x)、l(x)、e(x) 三角函數 指數函數等。
設置結果保留位數:啓動bc後, 鍵入scale=4回車 設置4位小數點,
退出bc:quit
使用示例:
1
2
3
4
5
6
7
8
9
10
11
12
|
[Test@cpst01 ~]$ bc -lq
scale = 4
12/5
2.4000
s(2)/c(2)
-2.1861
e(2)
7.3890
l(e)
-9999.0000
quit
[Test@cpst01 ~]$
|
這些命令主要是用來查看系統的網絡信息,通常是系統管理員須要掌握的,這裏簡單瞭解下:
使用示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
[Test@cpst01 ~]$ ifconfig
eth0 Link encap:Ethernet HWaddr D4:AE:52:91:F8:10
inet addr:xxx.xxx.xxx.xxx Bcast:xxx.xxx.xxx.xxx Mask:255.255.255.0
inet6 addr: fe80::d6ae:52ff:fe91:f810/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:27195998 errors:0 dropped:0 overruns:0 frame:1
TX packets:2389883 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1988048267 (1.8 GiB) TX bytes:3273975836 (3.0 GiB)
Interrupt:16
eth1 Link encap:Ethernet HWaddr D4:AE:52:91:F8:11
inet addr:192.168.12.12 Bcast:192.168.12.255 Mask:255.255.255.0
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
Interrupt:17
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:16436 Metric:1
RX packets:62 errors:0 dropped:0 overruns:0 frame:0
TX packets:62 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:3828 (3.7 KiB) TX bytes:3828 (3.7 KiB)
[Test@cpst01 ~]$ ping -c 5 www.baidu.com
PING www.a.shifen.com (119.75.217.109) 56(84) bytes of data.
64 bytes from 119.75.217.109: icmp_seq=1 ttl=52 time=40.0 ms
64 bytes from 119.75.217.109: icmp_seq=2 ttl=52 time=20.3 ms
64 bytes from 119.75.217.109: icmp_seq=3 ttl=52 time=19.9 ms
64 bytes from 119.75.217.109: icmp_seq=4 ttl=52 time=43.2 ms
64 bytes from 119.75.217.109: icmp_seq=5 ttl=52 time=25.7 ms
--- www.a.shifen.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4028ms
rtt min/avg/max/mdev = 19.904/29.878/43.257/9.898 ms
|
有人可能會問如何在Linux上從網絡上下載一下文件什麼的呢,Linux下有不少這樣的命令,這裏介紹下經常使用的wget命令。這裏你們掌握了這個命令後,就不用在windows下下載,再從windows上傳到服務器 這麼麻煩了。好比後面作RNA-seq分析須要用到的tophat軟件,其下載地址爲 http://ccb.jhu.edu/software/tophat/downloads/tophat-2.0.14.Linux_x86_64.tar.gz,那麼如何直接下載到服務器呢?
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#wget 常見參數
-q 關閉日誌輸出
-v 打開日誌輸出
-c 接着下載沒下載完的文件
-i [file] 從文件中獲取下載地址,依次進行下載(一行一個地址)
#有的下載可能須要用戶名和密碼,下面對應的參數能夠解決這個問題
--user=user
--password=password
#指明用戶名和密碼,也能夠根據協議不一樣使用不一樣的參數,如ftp和http
--ftp-user ,--ftp-password options for FTP connections
--http-user ,--http-password options for HTTP connections.
|
使用示例:
1
|
wget http://ccb.jhu.edu/software/tophat/downloads/tophat-2.0.14.Linux_x86_64.tar.gz
|
bg:
回車運行某個程序
[Ctrl + z]
參看 stopped前面的 job id, 如 1 2 3等
bg job-id : #[bg 1 回車]
jobs 查看當前被推至後臺運行的程序
fg job-id # [fg 1 回車] 將job id 爲1 的後臺程序切換至前臺運行
使用示例:
which命令是用來查看某個命令的具體位置是在哪裏,好比pwd命令實際上是位於/bin/pwd。
使用方法:which [參數] 命令,這簡單介紹下-a參數,即列舉出全部符合條件的命令的位置,由於有可能系統有多個同命令的命令,只是所在目錄不一樣,後面會在環境變量中介紹。
who、w——顯示誰在登錄、在幹什麼
who – show who is logged on
w – Show who is logged on and what they are doing.
這個命令用的也不是不少,若是你關係系統的運行狀況則可使用該命令查看下。
使用示例:
1
2
3
4
5
6
|
[Test@cpst01 ~]$ w
17:44:28 up 8 days, 2:50, 1 user, load average: 0.49, 0.61, 0.56
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
Test pts/0 122.205.65.107 17:40 0.00s 0.03s 0.02s w
[Test@cpst01 ~]$ who
Test pts/0 2015-06-04 17:40 (122.205.65.107)
|
在Linux中能夠經過上下鍵來一條條地翻閱歷史命令,history看名稱我就能夠猜到它是用於查看歷史記錄的,能夠顯示指定條數的歷史命令,首先來看下經常使用的幾個參數:
1
2
3
|
#history 經常使用參數
history [n] 顯示n條歷史記錄,默認是顯示不少條
history -c clear, 清楚歷史命令記錄
|
例子:
1
2
3
4
5
6
7
8
9
|
[Test@cpst01 ~]$ history 3
5 man history
6 history
7 history 3
[Test@cpst01 ~]$ history -c #清楚以前的命令
[Test@cpst01 ~]$ history #顯示history命令自己
1 history
[Test@cpst01 ~]$
|
你們能夠結合後面講到的管道操做符 和 grep等命令來在history結果中搜索想要的歷史命令。
man – format and display the on-line manual pages, 主要是用來顯示一些命令和函數等的幫助文檔。
man [參數] name
1
2
3
4
|
#man 經常使用參數
1 用戶命令
2 系統調用
3 C語言庫函數
|
使用示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
[Test@cpst01 ~]$ man pwd
PWD(1) User Commands PWD(1)
NAME
pwd - print name of current/working directory
SYNOPSIS
pwd [OPTION]...
DESCRIPTION
Print the full filename of the current working directory.
-L, --logical
use PWD from environment, even if it contains symlinks
-P, --physical
avoid all symlinks
--help display this help and exit
[Test@cpst01 ~]$ man sin #或者man 3 sin
SIN(3) Linux Programmer’s Manual SIN(3)
NAME
sin, sinf, sinl - sine function
SYNOPSIS
#include <math.h>
double sin(double x);
float sinf(float x);
long double sinl(long double x);
Link with -lm.
|
6. 重要備註
alias——命令重命名
alias 爲系統內置命令,用來簡化負責的操做或者組合,依我的習慣不一樣,別名能夠本身定。
好比,咱們常常要利用ls -lh命令來查看文件及目錄的一些狀況,可是每次都這麼輸入就很麻煩,那有沒有一個簡單的辦法來解決這個問題呢? alias就行:
1
2
3
4
5
6
7
|
[Test@cpst01 ~]$ alias lh='ls -lh' #運行這個命令後那麼lh這個虛擬的命令就至關於ls –lh。這個alias能夠定製其它命令別名。
[Test@cpst01 ~]$ lh #lh ./ 至關於ls –lh ./
total 1.4M
-rw-r--r-- 1 Test Test 76K Jan 20 11:35 000_pTRE-IRES.dna
drwxrwxr-x 2 Test Test 4.0K Jun 8 17:55 123
-rw-r--r-- 1 Test Test 54 Jan 17 20:50 1234.txt
drwxrwxr-x 2 Test Test 4.0K Jun 9 20:41 2015
|
管道操做符 | ,道是Linux中很重要的一種通訊方式, 是把一個程序的輸出直接鏈接到另外一個程序的輸入。用於串聯一系列的操做,好比:
1
2
3
4
|
ls –l | sort –k5,5n #對當前目錄下的文件和目錄安裝大小進行升序排序。
grep -i 'cotton' title.txt | head | sort -t ":" -k2,2 | tail
ps aux | grep "du" #查看du進程的信息
ls -l | sort -k5,5n #對當前目錄下的文件目錄按大小排序
|
環境變量——讓它知道,別讓它來猜
1
2
3
4
|
HOME 保存本身的主目錄
PATH 保存各類路徑變量
PWD 保存用戶當前所在目錄
其它變量
|
1
|
echo $HOME
|
1
2
3
|
[Test@cpst01 ~]$ export TMP="MY TMP"
[Test@cpst01 ~]$ echo $TMP
MY TMP
|
而每一個用戶主目錄下都有一個.bashrc隱藏文件(不一樣Linux發行版其文件名稱可能不同)用來記錄用戶的環境變量配置(不止環境變量配置),當用戶登陸遠程服務器時,系統會自動經過執行這個文件來完成會話的一些基本環境配置(也能夠利用source ~/.bashrc 來臨時更新環境配置)。因此能夠將export命令寫入.bashrc文件來保證每次登錄時都能設置變量。
~/.bashrc文件中存放的其實就是一些shell命令
export命令用來設置相應的環境變量, #開頭的行表示是註釋信息
從新登陸或者執行 source ~/.bashrc來應用修改後的配置
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[Test@cpst01 ~]$ vim .bashrc
[Test@cpst01 ~]$ source .bashrc
[Test@cpst01 ~]$ more .bashrc
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific aliases and functions
alias lh="ls -lh"
|
screen、nohup——程序不中斷運行
程序持續運行——別讓我被kill
元兇:SIGHUP 信號
答案:關掉窗口/斷開鏈接會使得正在運行的程序死掉。
咱們作一個試驗:
1
|
find / -name '*http*' &
|
利用ctrl+d 註銷之後 再進入系統 會不會看見這個命令再運行?
答案是 :命令被停止了!!
那麼怎麼去解決上述問題呢?
1、使用nohup讓程序在後臺繼續運行
經常使用命令格式:
nohup ./home/nginx.sh &
注:若是運行成功剛默認會將標準輸出重定向到當前目錄的nohup.out文件中,查看此文件,能夠了解運行的進度;
也能夠重定向標準輸出和標準錯誤輸出:
nohup somepragram [options] >log 2>errlog &
2、使用screen讓程序在後臺執行
screen是一個能夠在多個進程之間多路利用(複用)一個物理終端的窗口管理器。screen中有會話的概念,用戶能夠在一個screen會話中建立多個screen窗口,在每個screen窗口中就像操做一個真實的telnet/SSH鏈接窗口那樣。
screen -S wordname #建立一個名爲wordname的會話
當須要臨時離開時(會話中的程序不會關閉,仍在運行)能夠用快捷鍵Ctrl+a+d(即按住Ctrl,依次再按a,d)
screen –ls
當回來時能夠再執行執行:
screen -r wordname # 或者screen -r screen-id
便可恢復到離開前建立的會話的工做界面。
執行:exit #或者ctrl + D
首先在服務器上執行screen -S test建立一個screen的會話,觀衆能夠連接到 遠程服務器執行screen -x test 這樣觀衆的屏幕上就會出現和演示者同步的內容。
7. 小結
經過上面一系列的生物信息學經常使用Linux操做命令的講解,相信你們對生物信息學的基本分析環境有了一個認識,你們也能體會將命令分門別類的學習效果更加明顯。你們在學習的過程當中也要逐步學會各類命令間的共同之處,這樣能夠觸類旁通,舉一反三。關鍵的一點仍是要多加練習,慢慢領會各類命令的功能與使用訣竅。
From:http://www.iwhgao.com/2-4-linux%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C%E5%91%BD%E4%BB%A4/