1.本節學習的命令
gzip | Compress or expand files |
bzip2 | A block sorting file compressor |
tar | Tape archiving utility |
zip | Package and compress files |
rsync | Remote file and directory synchronization |
2.知識點
知識點1:gzip選項詳解。
選項 | 說明 |
---|---|
-c | 把輸出寫入到標準輸出,並且保留原始文件。也有可能用--stdout 和--to-stdout 選項來指定。 |
-d | 解壓縮。就等同於gunzip |
-f | 強制壓縮,即使原始文件的壓縮文件已經存在了,也要執行。也可以用--force 選項來指定。 |
-h | 顯示用法信息。也可用--help 選項來指定。 |
-l | 列出每個被壓縮文件的壓縮數據。也可用--list 選項。 |
-r | 若命令的一個或多個參數是目錄,則遞歸地壓縮目錄中的文件。也可用--recursive 選項來指定。 |
-t | 測試壓縮文件的完整性。也可用--test 選項來指定。就是看一下壓縮文件是否完整。比如gzip -t foo.txt.gz 返回ok. |
-v | 顯示壓縮過程中的信息。也可用--verbose 選項來指定。 |
-number | 設置壓縮指數。範圍1到9。數值1和9也可以各自用--fast (快)和--best(最大壓縮,慢) 選項來表示。默認值是6。 |
知識點2:tar模式有哪些?和選項有什麼區別?
c | 爲文件和/或目錄列表創建歸檔文件。 就是把文件夾變成歸檔文件。 |
x | 抽取歸檔文件。 就是把裏面的內容給取出來。可能是文件夾 |
r | 追加具體的路徑到歸檔文件的末尾。 |
t | 列出歸檔文件的內容。 就是把歸檔裏面的內容全部列出來 |
模式和選項可以寫在一起,而且不需要開頭的短橫線。但是一定要先是模式再是選項。
3.案例實操
[me@linuxbox ~]$ ls -l /etc > foo.txt
[me@linuxbox ~]$ ls -l foo.*
-rw-r--r-- 1 me me 15738 2008-10-14 07:15 foo.txt
[me@linuxbox ~]$ gzip foo.txt
[me@linuxbox ~]$ ls -l foo.*
-rw-r--r-- 1 me me 3230 2008-10-14 07:15 foo.txt.gz
[me@linuxbox ~]$ gunzip foo.txt.gz
[me@linuxbox ~]$ ls -l foo.*
-rw-r--r-- 1 me me 15738 2008-10-14 07:15 foo.txt
注:gzip和gunzip原文件都會消失。後綴爲.gz
[me@linuxbox ~]$ gzip foo.txt
[me@linuxbox ~]$ gzip -tv foo.txt.gz
foo.txt.gz: OK
[me@linuxbox ~]$ gzip -d foo.txt.gz
[me@linuxbox ~]$ gunzip foo.txt.gz
[me@linuxbox ~]$ ls -l /etc | gzip > foo.txt.gz
注:將內容壓縮後,再放入foo.txt.gz的壓縮包裏。
[me@linuxbox ~]$ gunzip -c foo.txt.gz | less
[me@linuxbox ~]$ zcat foo.txt.gz | less
注:將foo.txt.gz的內容解壓,由於有-c,所以保留原始foo.txt.gz文件。且將內容傳給less查看。zcat等同
於gunzip -c
[me@linuxbox ~]$ ls -l /etc > foo.txt
[me@linuxbox ~]$ ls -l foo.txt
-rw-r--r-- 1 me me 15738 2008-10-17 13:51 foo.txt
[me@linuxbox ~]$ bzip2 foo.txt
[me@linuxbox ~]$ ls -l foo.txt.bz2
-rw-r--r-- 1 me me 2792 2008-10-17 13:51 foo.txt.bz2
[me@linuxbox ~]$ bunzip2 foo.txt.bz2
注:bzip用法等同於gzip,只是使用了不同的壓縮算法。
[me@linuxbox ~]$ tar cf playground.tar playground
注:創建playground的tar包,模式是C 創建歸檔文件,選線是f
[me@linuxbox ~]$ tar tf playground.tar
me@linuxbox ~]$ tar tvf playground.tar
注:模式是t 列出歸檔文件的內容
[me@linuxbox ~]$ mkdir foo
[me@linuxbox ~]$ cd foo
[me@linuxbox ~]$ tar xf ../playground.tar
[me@linuxbox ~]$ ls
playground
注:模式是x,抽取歸檔文件。
[me@linuxbox foo]$ tar xf ../playground2.tar --wildcards 'home/me/playground/dir-\*/file-A'
注:這個命令將只會抽取匹配特定路徑名的文件,路徑名中包含了通配符 dir-*。
[me@linuxbox ~]$ find playground -name 'file-A' -exec tar rf playground.tar '{}' '+'
注:與tar的結合 追加模式(r) 這樣每一個就在一個文件裏面了。
[me@linuxbox ~]$ find playground -name 'file-A' | tar cf - --files-from=-
| gzip > playground.tgz
注:指定了文件名“-”,則其被看作是標準輸入或輸出。這個 --file-from 選項(也可以用 -T 來指定)導致
tar 命令從一個文件而不是命令行來讀入它的路徑名列表。最後,這個由 tar 命令產生的歸檔 文件被管道到
gzip 命令中,然後創建了壓縮歸檔文件 playground.tgz。
[me@linuxbox ~]$ find playground -name 'file-A' | tar czf playground.tgz -T -
注:tar - 進入playground.tgz
[me@linuxbox ~]$ find playground -name 'file-A' | tar cjf playground.tbz -T -
注:z爲gzip j爲bzip2
[me@linuxbox ~]$ zip -r playground.zip playground
[me@linuxbox foo]$ unzip ../playground.zip
注;注意一點,就是如果指定了一個已經存在的文件包,其被更新 而不是被替代。
[me@linuxbox ~]$ unzip -l playground.zip playground/dir-87/file-Z
Archive: ../playground.zip
Length Date Time Name
0 10-05-08 09:25 playground/dir-87/file-Z
0 1 file
[me@linuxbox ~]$ cd foo
[me@linuxbox foo]$ unzip ./playground.zip playground/dir-87/file-Z
Archive: ../playground.zip
replace playground/dir-87/file-Z? [y]es, [n]o, [A]ll, [N]one,
[r]ename: y
extracting: playground/dir-87/file-Z
[me@linuxbox ~]$ ls -l /etc/ | zip ls-etc.zip -
adding: - (deflated 80%)
[me@linuxbox ~]$ unzip -p ls-etc.zip | less
[me@linuxbox ~]$ rsync -av playground foo
注:同步 playground 目錄和它在 foo 目錄中相對應的副本
[me@linuxbox ~]$ sudo rsync -av --delete /etc /home /usr/local /media/BigDisk/backup
注:把/etc,/home,和/usr/local 目錄從我們的系統中複製到假想的存儲設備中。 我們包含了–delete 這
個選項,來刪除可能在備份設備中已經存在但卻不再存在於源設備中的文件。
[me@linuxbox ~]$ sudo rsync -av --delete --rsh=ssh /etc /home /usr/local remote-sys:/backup
注:--rsh=ssh 選項,其指示 rsync 使用 ssh 程序作爲它的遠程 shell。其次,通過在目標路徑名前加上
遠端主機的名字(在這種情況下, 遠端主機名爲 remote-sys),來指定遠端主機。即ssh remote-sys
[me@linuxbox ~]$ rsync -av -delete rsync://rsync.gtlib.gatech.edu/fedora-linux-
core/development/i386/os fedora-devel
注:在這個例子裏,我們使用了遠端 rsync 服務器的 URI,其由協議(rsync://),遠端主機名和軟件倉庫
的路徑名組成。(rsync.gtlib.gatech.edu),