1. 程式人生 > >Linux基礎命令(2)

Linux基礎命令(2)

(2) 備份與恢復文件
1.使用壓縮和解壓縮工具
(1)gzip
預設會刪除原檔案
-d 解壓縮
-#: 指定壓縮比,壓縮比越小,速度越大
解壓:gunzip
例:
壓縮文字a.txt
[[email protected]~]#gzip a.txt
[[email protected]~]# ls -lh a.txt.gz
-rw-r--r-- 1 root root 30 Nov 11 20:07 a.txt.gz
解壓文件a.txt
[[email protected]~]#gzip -d a.txt
-rw-r--r-- 1 root root 4 Nov 11 20:07 a.txt
[[email protected]

~]#gunzip a.txt.gz
[[email protected]~]# ls -lh a.txt
-rw-r--r-- 1 root root 4 Nov 11 20:07 a.txt(2)bzip2

(2)bzip2
-d:解壓
-#:指定壓縮比
-k:保留原檔案
解壓縮:bunzip2
例:
壓縮文件a.txt
[email protected]~]# bzip2 a.txt
[[email protected]~]# ls -lh a.txt.bz2
-rw-r--r-- 1 root root 96 Nov 11 20:16 a.txt.bz2
解壓文件a.txt
[[email protected]

~]# bzip2 -d a.txt.bz2
[[email protected]~]# ls -lh a.txt
-rw-r—r—1 root root 1.4K Nov 11 20:16 a.txt
[[email protected]~]# bunzip2 a.txt.bz2
[[email protected]~]# ls -lh a.txt
-rw-r—r—1 root root 1.4K Nov 11 20:16 a.txt

2.使用tar 歸檔和釋放工具.
Tar :既可以打包,又可以壓縮
tar 選項 包名 打包的檔案或目錄 (切記:一定要注意語法格式,先是打包後的名字,然後才是要打包的東西)
引數:
-c(--create): 建立歸檔檔案
-f(FILE.tar): 操作的歸檔檔案,指定檔案,一般後面跟包名
-x: 展開歸檔
--xattrs: 歸檔時,保留檔案的擴充套件屬性資訊
-t: 不展開歸檔,直接檢視歸檔了哪些檔案,不解包檢視包中的內容
-C:解壓時指定路徑
-r:向包中追加檔案
-v:顯示詳細過程
-z:表示使用gzip壓縮方式壓縮或者解壓縮
-j:表示使用bzip2壓縮方式壓縮或者解壓縮
例:
製作.gz格式的歸檔包
[

[email protected]~]# tar zcvf etc.tar.gz /etc
tar: Removing leading /' from member names<br/>/etc/<br/>/etc/fstab<br/>//省略部分內容<br/>[[email protected]~]# ls -lh etc.tar.gz <br/>-rw-r--r-- 1 root root 11M Nov 11 20:24 etc.tar.gz<br/>製作,bz2格式歸檔包<br/>[[email protected]~]# tar jcvf etc.tar.bz2 /etc<br/>tar: Removing leading/' from member names
/etc/
/etc/fstab
//省略部分內容
[[email protected]~]# ls -lh etc.tar.bz2
-rw-r--r-- 1 root root 8.8M Nov 11 20:27 etc.tar.bz2

(2)從歸檔文件中恢復資料
①.gz
[root[email protected]~]# tar zxf etc.tar.gz -C /
[[email protected]~]# ls -lh /etc
total 1.4M
//省略部分內容
②.bz2
[[email protected]~]# tar jxf etc.tar.bz2 -C /
[[email protected]~]# ls -lh /etc
total 1.4M
//省略部分內容

3.使用vi文字編輯工具
在vi編輯器中的常用命令
按i進入輸入模式,按esc回到命令模式,按:進入末行模式,按esc回到命令模式

(1)命令模式的基本操作
1)模式切換
a:在當前游標位置之後插入內容
A:在當前游標所在行尾插入內容
o:在當前游標位置之後插入內容
O:在當前游標位置行首插入內容
i:在當前游標的後面插入新行
I:在當前游標的前面插入新行
2)移動游標
 游標方向移動:↑ ↓ ← →(同樣可在末行模式中使用)

 翻頁移動:Page Down或Crtl+F向下翻頁 PageUp或Crtl+B向上翻頁(同樣可在末行模式中使用)
 行內快速跳轉:Home鍵或^跳轉到本行行首,End鍵或$跳轉到本行行末(同樣可在末行模式中使用)
 行間快速跳轉:
1g或gg:首行
G:末行
#g:第#行
3)複製,黏貼和刪除
複製:
yy:複製當前整行
#yy:複製當前游標開始得#行內容
p:黏貼
刪除:
Del鍵:刪除當前游標處內容
dd:刪除當前整行
#dd:刪除從當前游標開始的#行內容
d^:當前游標到行首的所有字元
d$:當前游標到行尾的所有字元
4)查詢檔案內容
/+指定字串:查詢指定字串
n:移動到下一個查詢結果
5)撤銷編輯及儲存和退出
u:取消最近一次操作並恢復操作結果
U:取消對當前行所做的所有操作
ZZ:儲存當前的檔案內容並退出vi編輯器
(3) 末行模式中的基本操作
1) 儲存檔案及退出vi編輯器
2) 開啟新檔案或者讀取其他檔案內容
3) 替換檔案內容