打包和壓縮的概念
tar命令可以爲linux的文件和目錄創建檔案
利用tar,可以爲某一特定文件創建檔案(備份文件),也可以在檔案中改變文件,或者向檔案中加入新的文件
tar最初被用來在磁帶上創建檔案,現在,用戶可以在任何設備上創建檔案。
利用tar命令,可以把一大堆的文件和目錄全部打包成一個文件,這對於備份文件或將幾個文件組合成爲一個文件以便於網絡傳輸是非常有用的。首先要弄清兩個概念:打包和壓縮。 打包是指將一大堆文件或目錄變成一個總的文件; 壓縮則是將一個大的文件通過一些壓縮算法變成一個小文件。
爲什麼要區分這兩個概念呢?
這源於Linux中很多壓縮程序只能針對一個文件進行壓縮,這樣當你想要壓縮一大堆文件時,你得先將這一大堆文件先打成一個包(tar命令),然後再用壓縮程序進行壓縮(gzip
bzip2命令)。
tar
SYNOPSIS
tar [-] A --catenate --concatenate | c --create | d --diff --compare | --delete | r --append | t --list | --test-label | u --update | x --extract --get [options] [pathname ...]
DESCRIPTION
Tar stores and extracts files from a tape or disk archive.
Tar從磁帶或磁盤存檔文件中存儲和提取文件。
常用獨立命令
FUNCTION LETTERS
Main operation mode:
-A, --catenate, --concatenate
append tar files to an archive
將文件附加到存檔文件中
-c, --create
create a new archive
創建一個新的存檔
-d, --diff, --compare
find differences between archive and file system
查找歸檔和文件系統之間的差異
--delete
delete from the archive (not on mag tapes!)
從存檔中刪除(不要在mag磁帶上刪除!)
-r, --append
append files to the end of an archive
將文件附加到歸檔文件的末尾
-t, --list
list the contents of an archive
列出歸檔文件的內容
--test-label
test the archive volume label and exit
-u, --update
only append files newer than copy in archive
只添加比存檔中的副本更新的文件(—更新
-x, --extract, --get
extract files from an archive
從歸檔文件中提取文件
重要:上面的命令是獨立的命令,壓縮解壓都要用到其中一個,可以和別的命令連用但只能用其中一個。
下面的參數是根據需要在壓縮或解壓檔案時可選的。(其中-f命令是必選的)
打包壓縮
tar -cvf log.tar log2012.log 僅打包,不壓縮!
tar -zcvf log.tar.gz log2012.log 打包後,以 gzip 壓縮
tar -jcvf log.tar.bz2 log2012.log 打包後,以 bzip2 壓縮
在選項f之後的文件檔名是自己取的,我們習慣上都用 .tar 來作爲辨識。
如果加z選項,則以.tar.gz或.tgz來代表gzip壓縮過的tar包;
如果加j選項,則以.tar.bz2來作爲tar包名。
-j, --bzip2
-z, --gzip, --gunzip --ungzip
-v, --verbose
verbosely list files processed
詳細列出處理過的文件
-f, --file ARCHIVE
use archive file or device ARCHIVE
使用存檔文件或設備存檔(指定存檔文件)
切記,這個參數是最後一個參數,後面只能接檔案名。
tar 命令 必須和-f命令連用
在選項f之後的文件檔名是自己取的,我們習慣上都用 .tar 來作爲辨識。 如果加z選項,則以.tar.gz或.tgz來代表gzip壓縮過的tar包;如果加j選項,則以.tar.bz2來作爲tar包名。
查閱tar包內有哪些文件
tar -ztvf log.tar.gz
由於我們使用 gzip 壓縮的log.tar.gz,所以要查閱log.tar.gz包內的文件時,就得要加上z這個選項了。t的意思是查看文檔內容
解壓縮到指定目錄(默認是當前目錄)
tar -xvf archive.tar -C /tmp 將壓縮包釋放到 /tmp目錄下
jz選項依據壓縮屬性需要加上
-C, --directory DIR
change to directory DIR
只將tar內的部分文件解壓出來
其他命令(其他)
-[0-7][lmh]
specify drive and density
指定驅動器和密度
-a, --auto-compress
use archive suffix to determine the compression program
使用archive後綴確定壓縮程序
--backup
backup before removal, choose version CONTROL
--exclude=PATTERN
exclude files, given as a PATTERN
--exclude-ignore=FILE
read exclude patterns for each directory from FILE, if it exists
從文件中排除每個目錄的模式(如果存在的話)
--exclude-ignore-recursive=FILE
read exclude patterns for each directory and its subdirectories from FILE, if it exists
read從文件中排除每個目錄及其子目錄的模式(如果存在的話)
--exclude-tag=FILE
exclude contents of directories containing FILE, except for FILE itself
排除包含文件的目錄的內容,文件本身除外
--exclude-tag-all=FILE
exclude directories containing FILE
排除包含文件的目錄
--exclude-tag-under=FILE
exclude everything under directories containing FILE
排除包含文件的目錄下的所有內容
-k, --keep-old-files
don't replace existing files when extracting, treat them as errors
解壓時不要替換現有文件,將它們視爲錯誤
-m, --touch
don't extract file modified time
不要提取文件修改時間
-N, --newer, --after-date DATE-OR-FILE
only store files newer than DATE-OR-FILE
只存儲比DATE-OR-FILE更新的文件
-O, --to-stdout
extract files to standard output
將文件解壓縮到標準輸出
-p, --preserve-permissions, --same-permissions 保留權限,相同權限
extract information about file permissions (default for superuser)
提取有關文件權限的信息(超級用戶默認)
--recursion
recurse into directories (default)
遞歸到目錄(默認)
-U, --unlink-first
remove each file prior to extracting over it
在提取每個文件之前刪除它
--unquote
unquote input file or member names (default)
不用引號 對輸入文件或成員名稱(默認)
--utc
print file modification times in UTC
用UTC打印文件修改時間
-w, --interactive, --confirmation
ask for confirmation for every action
對每一個動作都要確認
-W, --verify
attempt to verify the archive after writing it
嘗試在編寫存檔之後驗證它
-Z, --compress, --uncompress
——壓縮、解壓縮
EXAMPLES
Create archive.tar from files foo and bar.
tar -cf archive.tar foo bar
List all files in archive.tar verbosely.
tar -tvf archive.tar
Extract all files from archive.tar.
tar -xf archive.tar
zip和unzip
zip命令可以用來解壓縮文件,或者對文件進行打包操作。zip是個使用廣泛的壓縮程序,文件經它壓縮後會另外產生具有“.zip”擴展名的壓縮文件。
安裝
zip all.zip *.jpg 這條命令是將所有.jpg的文件壓縮成一個zip包
unzip all.zip 這條命令是將all.zip中的所有文件解壓出來
zip -r file1.zip file1 file2 dir1 將幾個文件和目錄同時壓縮成一個zip格式的壓縮包
-r:遞歸處理,將指定目錄下的所有文件和子目錄一併處理;
-v:顯示指令執行過程或顯示版本信息;
rar和unrar
安裝: sudo apt-get install rar unrar
或者可從http://www.rarsoft.com/download.htm下載 RAR for Linux 編譯安裝
# tar -xzpvf rarlinux-3.8.0.tar.gz
# cd rar
# make
這樣就安裝好了,安裝之後就有了rar和unrar這兩個程序,rar是壓縮程序,unrar是解壓程序。它們的參數選項很多,可用命令rar -help和unrar -help查看,這裏亦簡單舉例說明一下其用法:
a Add files to archive
e Extract files without archived paths
# rar a test.rar test.jpg test.png
這條命令是將test.jpg和test.png壓縮成一個rar包
# rar a test.rar *.jpg
這條命令是將所有.jpg的文件壓縮成一個rar包
# rar a test.rar test
這條命令是將文件夾test壓縮成一個rar包
# unrar e test.rar
這條命令是將test.rar中的所有文件解壓出來
也可以用 rar e test.rar