Fedora 和 Red Hat 系統管理/存檔和壓縮
| 此頁面最後編輯於 37 個月前,可能已被放棄 此頁面自 2021 年 8 月 11 日起未經編輯,但本書中的其他頁面可能已更新。檢視 相關更改 以瞭解本書的狀態。 您可以透過編輯和更新本書來提供幫助。如果此頁面未被積極編輯,請移除 {{正在建設中}}。在 WB:PROJECTS 尋求幫助。 |
正在建設中
正在建設中
正在建設中
要建立未壓縮的 tar 存檔,需要提供 c 和 f 選項(可以理解為 '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 .
要壓縮存檔的內容,可以使用 z、J 或 j 選項之一。每個選項對應於不同的壓縮演算法,它們分別是:gzip、xz 和 bzip2。
例如,要建立當前目錄的 gzip 壓縮存檔,可以使用以下命令:
[user@station user]$ tar cfz foo.tar.gz .
正在建設中
正在建設中
通常,tar 是建立存檔的首選方法,但在某些情況下,可能需要特定的存檔格式,而 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
要檢視存檔的內容,需要提供 -i 選項,以告知 cpio 在它的標準輸入中期望存檔資料。還要使用 -t 選項,以告知 cpio 不要提取,而只是簡單地列出存檔的內容。
[user@station user]$ cpio -it <archive playground/ playground/AUTHORS playground/ChangeLog playground/COPYING playground/foo.txt playground/newfile 1212 blocks
-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
[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
[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
[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
正在建設中