第 179 / 187 篇

17.8. 归档与压缩文件

说明

本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/Appendix2/tar.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。

17.8. Archiving and Compressing Files

tar(tape archive,磁带归档)工具用来把一组文件打包成单个文件(归档文件,archive file,或 tar 文件)。tar 命令也用来从 tar 文件中解出文件。归档文件(或 tar 文件)包含组成该 tar 文件的所有文件的数据,以及描述其内容的额外元数据。tar 需要这些元数据才能从 tar 文件中解出单个文件。

The tar (tape archive) utility is used to package up a set of files into a single file (an archive file or a tar file). The tar command is also used to extract the files out of a tar file. An archive file (or tar file) contains all the file data from the files that make up the tar file plus additional meta-data that describe its contents. The meta-data are necessary for tar to extract the individual file(s) out of a tar file.

tar 文件常用来分发一组文件,或者把一组文件从一台机器传到另一台机器。它们也可以用来存放成组文件的备份(文件归档)。例如,如果用户想把一组文件从一个系统传到另一个系统,他会先用 -c 命令行选项运行 tar,创建一个包含所有这些文件的单个 tar 文件,然后用 scp 把得到的 tar 文件传到远程机器上。在远程机器上,他再用 -x 运行 tar,从 tar 文件中解出文件。

Tar files are often used to distribute a set of files or to transfer a set of files from one machine to another. They also may be used to store backups of sets of files (file archives). For example, if a user who wants to transfer a set of files from one system to another, they would first run tar with the -c command line option to create a single tar file containing all of the files, then scp the resulting tar file to the remote machine. On the remote machine they would then run tar with -x to extract files from the tar file.

tar 命令有许多命令行选项,可以用不同的方式配置它的行为。表 1 列出了一些例子。

The tar command has many command line options for configuring its behavior in different ways. Table 1 lists some examples.

表 1. tar 的命令行选项

Table 1. tar command line options

选项 说明
-c 从一组文件创建一个 tar 文件
-x 解包(即 untar)一个 tar 文件
-t 列出 tar 文件的内容
-v 详细模式,在运行命令时打印出信息
-f <tar file name> 指定 tar 文件的名字
Option Description
-c create a tar file from a set of files
-x extract, or untar, a tar file
-t list the contents of a tar file
-v verbose mode, prints out information as runs command
-f <tar file name> specifies the name of the tar file

要创建 tar 文件,需要使用 -c,用 -f 指定输出 tar 文件的名字,然后给出要包含在 tar 文件中的文件(和目录)列表(目录的全部内容都会被包含进来)。

To create a tar file you need to use -c, specify the name of the tar output file with -f and then give the list of files (and directories) to include in the tar file (full directory contents will be included).

下面是一个例子,展示如何从三个普通文件(main.c、process.c、Makefile)和一个目录的内容(inputfiles/)创建一个 tar 文件:

Here is an example of how to create a tar file from three regular files (main.c, process.c, Makefile) and the contents of a directory (inputfiles/):

1
$ tar -c -v -f mytarfile.tar  main.c process.c Makefile intputfiles/

-v 是详细输出的命令行选项。它会在 tar 执行时列出被打包的文件的一些信息。

-v is the verbose command line option. It lists some information about the files being tar’ed up as tar executes.

tar 的命令行选项也可以用另一种更紧凑的语法来指定(你看到的 tar 例子中常常用这种写法):

The command line options to tar can also be specified using an alternate, more compact, syntax (this is often used in tar examples you see):

1
2
3
$ tar -cvf mytarfile.tar  main.c process.c Makefile intputfiles/

$ tar cvf mytarfile.tar main.c process.c Makefile intputfiles/

或者,你可以创建一个目录,把想打包的文件复制进去,然后从这个目录生成一个 tar 文件。例如:

Alternatively, you can create a directory and copy the files you want to tar into it, and generate a tar file from that directory. For example:

1
2
3
4
$ mkdir mytardir
$ cp *.c mytardir/.
$ cp -r inputfiles mytardir/.
$ tar cvf mytarfile.tar mytardir

要列出 tar 文件中的文件,使用 -t 命令行选项:

To list the files in a tar file, use the -t command line option:

1
$ tar -tvf mytarfile.tar

要从 tar 文件中解出文件,使用 -x 命令行选项:

To extract files from a tar file, use the -x command line option:

1
$ tar xvf mytarfile.tar

17.8.1. 文件压缩

17.8.1. file compression

文件压缩常常与 tar 一起使用,以减小 tar 文件的大小。这既能减少存放 tar 文件所需的存储空间,也能在用 scp 把 tar 文件复制到远程机器时减少通过网络发送的数据量。gzip 和 gunzip 用 gzip 压缩算法压缩和解压文件,bzip2 和 bunzip2 用 bzip 压缩算法压缩和解压文件。下面是一些调用示例:

Often times file compression is used along with tar to reduce the size of the tar file. This is useful for both reducing the storage space required for the tar file, and also to reduce the amount of data that are sent over the network when a tar file is copied to a remote machine using scp. The gzip and gunzip are used to compress and uncompress a file using gzip compression, and bzip2 and bunzip2 are used to compress and uncompress a file using bzip compression. Here are some example calls:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ ls -l
-rw-r--r-- 1 sarita users 40840 Apr 12 16:55 file

$ gzip file # compress using gzip
$ ls -l
-rw-r--r-- 1 sarita users 13258 Apr 12 16:55 file.gz

$ gunzip file.gz # uncompress a gziped file
$ ls -l
-rw-r--r-- 1 sarita users 40840 Apr 12 16:55 file

$ bzip2 file # compress using bzip2
$ ls -l
-rw-r--r-- 1 sarita users 12568 Apr 12 16:55 file.bz2

$ bunzip2 file.bz2 # uncompress a bzip2ed file
$ ls -l
-rw-r--r-- 1 sarita users 40840 Apr 12 16:55 file

tar 与文件压缩

tar and file compression

文件压缩常常用在 tar 文件上。用户可以在 tar 创建 tar 文件之后再对它运行文件压缩工具,并在运行 tar 从 tar 文件中解出文件之前先运行工具解压它。不过,由于文件压缩与 tar 文件一起使用得非常普遍,tar 有一些命令行选项,可以用不同的压缩算法创建压缩的 tar 文件(以及从压缩的 tar 文件中解出文件)(例如,-j 使用 bzip2,-z 使用 gzip)。

File compression is often used on tar files. A user can run file compression utilities on a tar file after tar creates it and run utilities to uncompress the tar file before running tar to extract files from the tar file. However, because file compression is so commonly used with tar files, tar has command line options to create compressed tar files (and to extract files from compressed tar files) using different compression algorithms (for example, -j uses bzip2, and -z uses gzip).

下面是给 tar 加上 -z 选项(使用 gzip)或 -j 选项(使用 bzip)一步完成 tar 文件压缩的一些例子:

Here are some examples of compressing the tar file in one step using the -z option to gzip or -j option for bzip: to tar:

1
2
3
4
5
6
7
8
9
10
# use -z option to compress tar file using gzip:
$ tar -czvf myproj.tar.gz myproj/
$ ls
myproj/ myproj.tar.gz


# or use -j option to compress tar file using bzip2:
$ tar -cjvf myproj.tar.bz2 myproj/
$ ls
myproj/ myproj.tar.bz2

或者,下面是把创建 tar 文件和压缩分成两步的例子(先创建 tar 文件,再用 gzip(或 bzip2)压缩):

Alternatively, here is an example of separating the tar file creation and compression into two steps (create tar file, then compress using gzip (or bzip2):

1
2
3
4
5
6
7
8
9
10
11
12
$ tar cvf myproj.tar myproj/   # create a tar file from myproj
$ ls
myproj/ myproj.tar

$ gzip myproj.tar # gzip the tar file
$ ls
myproj/ myproj.tar.gz

# OR could use bzip
$ bzip2 myproj.tar # bzip the tar file
$ ls
myproj/ myproj.tar.bz2

你也可以用一步法或两步法从压缩的 tar 文件中解出文件。例如,下面是一步完成的方法(-z 指定先用 gzip 解压文件):

You can also use the one-step or two-step method to extract files from a compressed tar file. For example, here is the single step way (-z specifies to uncompress the file first using gzip):

1
2
3
$ tar -xzvf myproj.tar.gz
$ ls
myproj/ myproj.tar.gz

或者,下面是从压缩的 tar 文件中解出文件的两步法(先用 gunzip 或 bunzip2 解压,再 untar):

Alternatively, here is the two-step method to extract files from a compressed tar file (first uncompress with gunzip or bunzip2, then untar):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ ls
myproj.tar.gz
$ gunzip myproj.tar.gz
$ ls
myproj.tar
$ tar -xvf myproj.tar
$ ls
myproj/ myproj.tar


# or using bunzip2 for bzip'ed file
$ ls
myproj.tar.bz2
$ bunzip myproj.tar.bz2
$ ls
myproj.tar
$ tar -xvf myproj.tar
$ ls
myproj/ myproj.tar

17.8.2. 综合运用

17.8.2. putting it all together

下面的例子展示了如何用 tar 把一个目录的内容从一台机器(laptop)传到一台远程机器(ada.cs.college.edu)。它用 scp 命令在两台机器之间远程复制文件(关于 scp 的更多信息,参见第 17.3 节)。

Below is an example of using tar to transfer a directory’s contents from one machine (laptop) to a remote machine (ada.cs.college.edu). It uses the scp command to remotely copy the file between the two machines (see Section 17.3 for more information about scp).

  1. 首先,用户在 laptop 上创建压缩的 tar 文件(注意:我们用两步法来创建并压缩 tar 文件,是为了展示 tar 文件中文件的列表,但压缩的 tar 文件也可以用 -cjvf 命令行选项一步创建出来):
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    laptop$  ls myproj/
    Makefile input1.txt input2.txt main.c mux.c mux.h

    laptop$ tar -cvf myproj.tar myproj/ # create a tar file from myproj
    laptop$ ls
    myproj/ myproj.tar

    laptop$ tar -tf myproj.tar # list tar file contents
    myproj/
    myproj/Makefile
    myproj/input1.txt
    myproj/input2.txt
    myproj/main.c
    myproj/mux.c
    myproj/mux.h

    laptop$ gzip myproj.tar # compress the file
  1. First, the user creates the compressed tar file on laptop (note: we show the two-step method to create and compress the tar file in order to show the listing of the files in the tar file, but the compressed tar file could also be created in one-step using the -cjvf command line options):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    laptop$  ls myproj/
    Makefile input1.txt input2.txt main.c mux.c mux.h

    laptop$ tar -cvf myproj.tar myproj/ # create a tar file from myproj
    laptop$ ls
    myproj/ myproj.tar

    laptop$ tar -tf myproj.tar # list tar file contents
    myproj/
    myproj/Makefile
    myproj/input1.txt
    myproj/input2.txt
    myproj/main.c
    myproj/mux.c
    myproj/mux.h

    laptop$ gzip myproj.tar # compress the file
  1. 然后,他用 scp 把这个单个的 tar 文件传到远程机器上:
    1
    2
    laptop$ scp myproj.tar sam@ada.cs.college.edu:.   # scp single tar file
    sam@ada.cs.college.edu's password:
  1. Then, they scp the single tar file to the remote machine:

    1
    2
    laptop$ scp myproj.tar sam@ada.cs.college.edu:.   # scp single tar file
    sam@ada.cs.college.edu's password:
  1. 然后,用户可以 ssh 登录到远程机器,并对 myproj.tar 文件 untar 以解出文件:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    laptop$ ssh sam@ada.cs.college.edu
    sam@ada.cs.college.edu's password:

    ada$ ls
    classes/ letters/ myproj.tar projects/

    ada$ tar -xjvf myproj.tar # extract contents of tar file

    ada$ ls
    classes/ letters/ myproj/ myproj.tar projects/
  1. Then the user can then ssh into the remote machine, and untar the myproj.tar file to extract the files:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    laptop$ ssh sam@ada.cs.college.edu
    sam@ada.cs.college.edu's password:

    ada$ ls
    classes/ letters/ myproj.tar projects/

    ada$ tar -xjvf myproj.tar # extract contents of tar file

    ada$ ls
    classes/ letters/ myproj/ myproj.tar projects/

译注 · 文件名与压缩选项前后不一致

第 1 步最后执行的是 gzip myproj.tar,它会把文件变成 myproj.tar.gz(gzip 格式),原来的 myproj.tar 就不存在了。所以第 2 步应当传送 myproj.tar.gz,第 3 步应当用对应 gzip 的 -z 选项解包:tar -xzvf myproj.tar.gz。书中第 3 步用的 -j 对应的是 bzip2;同理,第 1 步括号中提到的一步法若要与这里一致,应使用 -czvf。

如果用户想把目录 untar 到主目录以外的某个地方,可以用 mv 把目录移到想要的位置(或者也可以在 untar 之前先把 tar 文件移到想要的位置)。例如,把它移到 project 目录下作为子目录:

If the user wanted to untar the directory somewhere other than in their home directory, they could use mv to move the directory where they want (or they could have also moved the tar file to where they want it before untaring it). For example, to move it as a subdirectory under the project directory:

1
2
3
4
5
6
7
8
ada$ ls
classes/ letters/ myproj/ myproj.tar projects/
ada$ mv myproj project/.
ada$ ls
classes/ letters/ myproj.tar projects/
ada$ cd project
ada$ ls
myproj/

untar 一个 tar 文件之后,用户常常会删除那个 .tar 文件:

Often after untaring a tar file, the user removes the .tar file:

1
2
rm myproj.tar
ls

17.8.3. 参考资料

17.8.3. References

更多信息参见:

For more information see:

  • 这些命令的手册页(例如 man tar、man gunzip)
  • The man pages for these commands (e.g., man tar, man gunzip)