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
正在建设中