文件的壓縮和打包
1.常用壓縮指令:
• *.bz2 #bzip2 程序壓縮文件
• *.gz #gzip 程序壓縮文件
• *.tar #tar 程序打包文件但並未壓縮
• *.tar.gz #tar 程序打包文件且經過 gzip 壓縮
·compress
[root@linux ~]# compress [-dcr] 文件或目錄
-d :解壓縮
-r :遞歸壓縮,即目錄下的文件全部壓縮
-c :將壓縮產生的信息輸出到 standard output
[root@linux tmp]# compress man.config
[root@linux tmp]# compress -d man.config.Z
[root@linux tmp]# compress -c man.config > man.config.back.Z
·gzip, zcat
[root@linux ~]# gzip [-cdt-] file #壓縮指定文件
[root@linux ~]# zcat file.gz #查看壓縮文件內容
-c :將壓縮數據輸出到指定設備,不改變原文件,用重導向功能
-d :解壓縮
-r :遞歸壓縮
-t :檢驗壓縮文件一致性
-- :壓縮等級,-1 最快,但是壓縮比最低、-9 最慢,但是壓縮比最高!默認是-6
[root@linux ~]# cd /tmp
[root@linux tmp]# cp /etc/man.config .
[root@linux tmp]# gzip man.config
[root@linux tmp]# zcat man.config.gz
[root@linux tmp]# gzip -d man.config.gz
[root@linux tmp]# gzip -9 -c man.config > man.config.gz
·bzip2, bzcat
[root@linux ~]# bzip2 [-cdz-] file
[root@linux ~]# bzcat file.bz2
-c :將壓縮數據輸出到指定設備,不改變原文件,用重導向功能
-d :解壓縮
-z :強制壓縮
-- :壓縮等級,同gzip
[root@linux tmp]# bzip2 -z man.config
[root@linux tmp]# bzcat man.config.bz2
[root@linux tmp]# bzip2 -d man.config.bz2
[root@linux tmp]# bzip2 -9 -c man.config > man.config.bz2
·tar
[root@linux ~]# tar [-cxtzjvfpPN] 文件或目錄
-c :建立壓縮文件
-x :解開壓縮文件
-t :查看 tarfile 文件
-z :是否使用 gzip 壓縮
-j :是否使用 bzip2 壓縮
-v :顯示命令執行過程
-f :指定壓縮文件名,在 f 後要立即接文件名
-p :保留原文件屬性、權限
-P :使用絕對路徑壓縮
-N :只壓縮比指定日期新的文件
--exclude FILE:在壓縮時,排除這些文件
[root@linux ~]# tar -cvf /tmp/etc.tar /etc
#打包但不壓縮/etc目錄下的所有文件,指定打包文件名爲etc.tar並放在/tmp目錄下
[root@linux ~]# tar -zcvf /tmp/etc.tar.gz /etc
#打包並用gzip壓縮
[root@linux ~]# tar -jcvf /tmp/etc.tar.bz2 /etc
#打包並用bzip2壓縮
[root@linux ~]# tar -ztvf /tmp/etc.tar.gz
#查看打包文件,如果是gzip壓縮的必須加-z選項
[root@linux ~]# cd /usr/local/src
[root@linux src]# tar -zxvf /tmp/etc.tar.gz
#將 /tmp/etc.tar.gz 解壓到 /usr/local/src 目錄下
[root@linux ~]# cd /tmp
[root@linux tmp]# tar -zxvf /tmp/etc.tar.gz etc/passwd
#在 /tmp 目錄下,只將 /tmp/etc.tar.gz 文件內的 etc/passwd 解壓縮,沒有 "/"
[root@linux ~]# tar -zcvpf /tmp/etc.tar.gz /etc
#壓縮/etc目錄下的所有文件並保留原始屬性
[root@linux ~]# tar -N '2005/06/01' -zcvf home.tar.gz /home
#只壓縮比2005/06/01日期新的文件
[root@linux ~]# tar --exclude /home/dmtsai -zcvf myfile.tar.gz /home/* /etc
#壓縮 /home, /etc ,但排除 /home/dmtsai
[root@linux ~]# cd /tmp
[root@linux tmp]# tar -cvf - /etc | tar -xvf -
#將 /etc/ 壓縮後直接解壓到 /tmp 目錄下,不產生中間文件。輸出文件變成 - 而輸入文件
#也變成 - 。其實就是複製的過程。
2.絕對路徑和權限的問題
請特別注意tar參數中的 -P,-p ,tar將 /etc 目錄的 / 刪除了,因爲在 tar 裏面的文件如果具有『絕對路徑』,那麼當解壓這個文件時將一定會解壓到該絕對路徑下,即/etc,而不是相對路徑。如果不刪除,萬一別的系統上也有 /etc 目錄,那麼就會被覆蓋。因此,在默認情況下,如果是以絕對路徑建立壓縮文件,那麼 tar 將自動刪除 / 。如果需要以絕對路徑來建立壓縮文件,就加入 -P 參數。解壓時也要加上-P 參數,就會以絕對路徑解壓。
-p 就是權限,使用參數 -p 後,被壓縮的文件將保留原有權限。
3.tarfile 與 tarball
tarball :通過 tar 打包並壓縮的文件
tarfile :通過 tar 打包但不壓縮的文件
tar 可用於備份到儲存設備上,假如磁帶機代號爲 /dev/st0,備份/home目錄下的數據:
[root@linux ~]# tar /dev/st0 /home
Linux 系統中,gzip 已經被整合到 tar 。但是 Sun 或者其它較舊的 Unix 版本中, 當中的 tar 並沒有整合 gzip ,如果需要解壓縮,就需要:
[root@linux ~]# gzip -d testing.tar.gz
[root@linux ~]# tar -xvf testing.tar
在沒有加入特殊參數的情況下 bzip2, gzip,compress 會把原始文件替代,但是 tar 會保留原始文件。
4.dd
[root@linux ~]#dd if="input_file" of="outptu_file" bs="block_size" count="number"
if :輸入文件或設備
of :輸出文件或設備
bs :block大小,默認是 512 bytes
count:bs的數量
[root@linux ~]# dd if=/etc/passwd of=/tmp/passwd.back
3+1 records in
3+1 records out
3+1 表示 3 個完整的512 bytes,以及未滿 512 bytes 的另一個 block
[root@linux ~]# dd if=/dev/hda of=/tmp/mbr.back bs=512 count=1
#備份 /dev/hda 的 MBR
[root@linux ~]# dd if=/dev/hda1 of=/some/path/filename
#備份 /dev/hda1 整個分區
5.cpio
[root@linux ~]# cpio -covB > [file|device] #備份
[root@linux ~]# cpio -icduv < [file|device] #還原
-o :將數據複製輸出到文件或設備上,建立備份
-i :將數據從文件或設備複製到系統中,還原備份
-t :列出備份文件的內容列表
-c :使用舊的ASCII備份格式
-v :顯示命令詳細執行過程
-B :修改默認的 Blocks 增加到 5120 bytes ,默認是 512 bytes,加快存儲速度
-d :自動建立目錄
-u :自動將較新的文件覆蓋較舊的文件
[root@linux ~]# find / -print | cpio -covB > /dev/st0
#將系統上所有的數據全部寫入磁帶機
[root@linux ~]# cpio -icdvt < /dev/st0
[root@linux ~]# cpio -icdvt < /dev/st0 > /tmp/content
#查看磁帶機上的備份文件
[root@linux ~]# cpio -icduv < /dev/st0
#還原磁帶機上的備份文件
[root@linux ~]# find /etc -type f | cpio -o > /root/etc.cpio
#將/etc目錄下的文件備份到/root/etc.cpio
[root@linux ~]# cpio -i < /root/etc.cpio
#還原備份
由於 cpio 無法直接讀取文件,需要『每一個文件或目錄的路徑連同文件名一起』纔可以被記錄下來。因此,cpio 經常與 find 指令搭配使用。