跳轉到內容

Fedora 和 Red Hat 系統管理/存檔和壓縮

來自華夏公益教科書,開放世界的開放書籍

壓縮資料

[編輯 | 編輯原始碼]

gzipgunzip

[編輯 | 編輯原始碼]

正在建設中

bzip2bunzip2

[編輯 | 編輯原始碼]

正在建設中

tar - *NIX 存檔

[編輯 | 編輯原始碼]

正在建設中

建立 tar 存檔

[編輯 | 編輯原始碼]

要建立未壓縮的 tar 存檔,需要提供 cf 選項(可以理解為 'c' 代表建立,'f' 代表檔名)。

[user@station user]$ tar cf foo.tar file1 file2 file3

第一個引數是存檔的名稱,本例中為 foo.tar。任何後續引數都是要存檔的檔案(例如,file1 file2 file3)。通常使用萬用字元來存檔所有特定型別的檔案(例如,tar cf foo.tar *.cpp 會存檔當前目錄中的所有 .cpp 檔案)。


要存檔目錄中的所有檔案和子目錄,可以提供相同的選項,但第二個引數是 . 字元,它代表當前工作目錄。

[user@station user]$ tar cf foo.tar .


要壓縮存檔的內容,可以使用 zJj 選項之一。每個選項對應於不同的壓縮演算法,它們分別是:gzipxzbzip2

例如,要建立當前目錄的 gzip 壓縮存檔,可以使用以下命令:

[user@station user]$ tar cfz foo.tar.gz .


檢查 tar 存檔

[編輯 | 編輯原始碼]

正在建設中

提取 tar 存檔

[編輯 | 編輯原始碼]

正在建設中


cpio - 靈活的歸檔工具

[編輯 | 編輯原始碼]

通常,tar 是建立存檔的首選方法,但在某些情況下,可能需要特定的存檔格式,而 cpio 提供了更大的控制,可以更好地控制存檔的生成方式,但代價是複雜性更高。

建立 cpio 存檔

[編輯 | 編輯原始碼]

要建立 cpio 存檔,需要提供 -o 選項(可以理解為 'o' 代表“存檔正在out出”)。cpio 期望將檔案列表提供給它的標準輸入。檔案列表通常透過管道從 find 命令的結果提供。可以使用 -H 選項指定存檔格式,有關更多資訊,請參閱 手冊頁

[user@station user]$ find playground/
playground/
playground/AUTHORS
playground/ChangeLog
playground/COPYING
playground/foo.txt
playground/newfile
[user@station user]$ find playground/ | cpio -o >archive
1212 blocks
[user@station user]$ ls -l archive
-rw-rw-r--    1 user     user       620544 Jan  5 08:49 archive
[user@station user]$ file archive
archive: cpio archive

使用 cpio 列出存檔內容

[編輯 | 編輯原始碼]

要檢視存檔的內容,需要提供 -i 選項,以告知 cpio 在它的標準輸入中期望存檔資料。還要使用 -t 選項,以告知 cpio 不要提取,而只是簡單地列出存檔的內容。

[user@station user]$ cpio -it <archive
playground/
playground/AUTHORS
playground/ChangeLog
playground/COPYING
playground/foo.txt
playground/newfile
1212 blocks

使用 cpio 提取存檔

[編輯 | 編輯原始碼]

-i 選項與其他選項結合使用,以告知它如何提取。常見的選項包括:-d,以告知 cpio 按需建立目錄。-m,以重置檔案修改時間。-R,以更改檔案所有權。

[user@station user]$ cd /tmp
[user@station tmp]$ cpio -idm <archive
1212 blocks
[user@station tmp]$ find playground/
playground/
playground/AUTHORS
playground/ChangeLog
playground/COPYING
playground/foo.txt
playground/newfile

zip - PKZIP 風格存檔

[編輯 | 編輯原始碼]

建立 zip 存檔

[編輯 | 編輯原始碼]
[user@station user]$ zip -r playground.zip playground
  adding: playground/ (stored 0%)
  adding: playground/AUTHORS (deflated 63%)
  adding: playground/COPYING (deflated 62%)
  adding: playground/foo.txt (stored 0%)
  adding: playground/newfile (deflated 39%)
  adding: playground/ChangeLog (deflated 70%)
[user@station user]$ ls -l playground.zip
-rw-r--r--  1 user     user     71607 Jan 11 14:33 playground.zip

列出 zip 存檔內容

[編輯 | 編輯原始碼]
[user@station user]$ unzip -l playground.zip
Archive:  playground.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
        0  01-11-05 14:33   playground/
     2110  01-11-05 14:32   playground/AUTHORS
    17992  01-11-05 14:33   playground/COPYING
       22  01-11-05 14:33   playground/foo.txt
       44  01-11-05 14:33   playground/newfile
   212169  01-11-05 14:32   playground/ChangeLog
 --------                   -------
   232337                   6 files

解壓 zip 存檔

[編輯 | 編輯原始碼]
[user@station user]$ rm -rf playground
[user@station user]$ unzip playground.zip
Archive:  playground.zip
   creating: playground/
  inflating: playground/AUTHORS
  inflating: playground/COPYING
 extracting: playground/foo.txt
  inflating: playground/newfile
  inflating: playground/ChangeLog

dump - 檔案系統級備份

[編輯 | 編輯原始碼]

正在建設中

華夏公益教科書