Linux文件归档、备份与正则表达式使用指南
发布时间: 2025-08-14 01:38:26 阅读量: 7 订阅数: 11 


Linux命令行完全入门指南
# Linux 文件归档、备份与正则表达式使用指南
## 1. 文件压缩与归档工具
### 1.1 tar 命令的压缩功能
在之前,我们可能会外部使用 gzip 程序来生成压缩存档。不过,现代版本的 GNU tar 直接支持 gzip 和 bzip2 压缩,分别通过 z 和 j 选项实现。
例如,以之前的例子为基础,我们可以这样简化操作:
```bash
[me@linuxbox ~]$ find playground -name 'file-A' | tar czf playground.tgz -T -
```
如果我们想创建一个 bzip2 压缩的存档,可以这样做:
```bash
[me@linuxbox ~]$ find playground -name 'file-A' | tar cjf playground.tbz -T -
```
只需将压缩选项从 z 改为 j(并将输出文件的扩展名改为 .tbz 以表示是 bzip2 压缩的文件),就启用了 bzip2 压缩。
### 1.2 tar 命令通过网络传输文件
tar 命令结合标准输入和输出还有一个有趣的用途,就是通过网络在系统之间传输文件。假设我们有两台运行类 Unix 系统的机器,都安装了 tar 和 ssh。在这种情况下,我们可以将远程系统(在这个例子中名为 remote-sys)的一个目录传输到本地系统:
```bash
[me@linuxbox ~]$ mkdir remote-stuff
[me@linuxbox ~]$ cd remote-stuff
[me@linuxbox remote-stuff]$ ssh remote-sys 'tar cf - Documents' | tar xf -
me@remote-sys's password:
[me@linuxbox remote-stuff]$ ls
Documents
```
这里我们成功地将远程系统 remote-sys 上名为 Documents 的目录复制到了本地系统中 remote-stuff 目录下的一个目录中。具体操作步骤如下:
1. 首先,使用 ssh 在远程系统上启动 tar 程序。ssh 允许我们在联网的计算机上远程执行程序,并在本地系统上“看到”结果,即远程系统产生的标准输出会发送到本地系统进行查看。
2. 让 tar 创建一个存档(c 模式)并将其发送到标准输出(f 选项后跟破折号参数),从而通过 ssh 提供的加密隧道将存档传输到本地系统。
3. 在本地系统上,执行 tar 并让它展开从标准输入提供的存档(x 模式,同样 f 选项后跟破折号参数)。
### 1.3 zip 程序的使用
zip 程序既是一个压缩工具,也是一个归档器。该程序使用的文件格式对 Windows 用户来说很熟悉,因为它可以读写 .zip 文件。在 Linux 中,gzip 是主要的压缩程序,bzip2 紧随其后。Linux 用户主要使用 zip 与 Windows 系统交换文件,而不是进行压缩和归档。
#### 1.3.1 创建 zip 存档
zip 的基本用法如下:
```bash
zip options zipfile file...
```
例如,要创建我们 playground 目录的 zip 存档,我们可以这样做:
```bash
[me@linuxbox ~]$ zip -r playground.zip playground
```
除非我们包含 -r 选项进行递归操作,否则只会存储 playground 目录(而不存储其内容)。虽然 .zip 扩展名会自动添加,但为了清晰起见,我们还是会明确写出文件扩展名。
在创建 zip 存档期间,zip 通常会显示一系列消息,如下所示:
```
adding: playground/dir-020/file-Z (stored 0%)
adding: playground/dir-020/file-Y (stored 0%)
adding: playground/dir-020/file-X (stored 0%)
adding: playground/dir-087/ (stored 0%)
adding: playground/dir-087/file-S (stored 0%)
```
这些消息显示了添加到存档中的每个文件的状态。zip 会使用两种存储方法之一将文件添加到存档中:要么不压缩直接“存储”文件,就像这里显示的那样;要么对文件进行“deflate”操作,即进行压缩。存储方法后面显示的数值表示实现的压缩量。由于我们的 playground 目录只包含空文件,所以其内容没有进行压缩。
#### 1.3.2 提取 zip 文件内容
使用 unzip 程序提取 zip 文件的内容很简单:
```bash
[me@linuxbox ~]$ cd foo
[me@linuxbox foo]$ unzip ../playground.zip
```
需要注意的是,与 tar 不同,如果指定了一个现有的存档,zip 会更新它而不是替换它。这意味着现有存档会被保留,但会添加新文件并替换匹配的文件。
#### 1.3.3 选择性列出和提取文件
我们可以通过指定文件来选择性地列出和提取 zip 存档中的文件:
```bash
[me@linuxbox ~]$ unzip -l playground.zip playground/dir-087/file-Z
Archive: ./playground.zip
Length Date Time Name
-------- ---- ---- ----
0 10-05-12 09:25 playground/dir-087/file-Z
-------- -------
0 1 file
[me@linuxbox ~]$ cd foo
[me@linuxbox foo]$ unzip ../playground.zip playground/dir-087/file-Z
Archive: ../playground.zip
replace playground/dir-087/file-Z? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
extracting: playground/dir-087/file-Z
```
使用 -l 选项会使 unzip 仅列出存档的内容而不提取文件。如果未指定文件,unzip 将列出存档中的所有文件。可以添加 -v 选项来增加列表的详细程度。注意,当存档提取与现有文件冲突时,在替换文件之前会提示用户。
#### 1.3.4 zip 使用标准输入和输出
像 tar 一样,zip 也可以使用标准输入和输出,不过其实现的实用性稍低。可以通过 -@ 选项将文件名列表通过管道传递给 zip:
```bash
[me@linuxbox foo]$ cd
[me@linuxbox ~]$ find playground -name "file-A" | zip -@ file-A.zip
```
这里我们使用 find 生成一个与 -name "file-A" 匹配的文件列表,然后将该列表通过管道传递给 zip,它会创建包含所选文件的存档 file-A.zip。
zip 也支持将其输出写入标准输出,但由于很少有程序能使用该输出,所以其用途有限。不幸的是,unzip 程序不接受标准输入,这使得 zip 和 unzip 不能像 tar 那样一起用于网络文件复制。不过,zip 可以接受标准输入,因此可以用于压缩其他程序的输出:
```bash
[me@linuxbox ~]$ ls -l /etc/ | zip ls-etc.zip -
adding: - (deflated 80%)
```
在这个例子中,我们将 ls 的输出通过管道传递给 zip。和 tar 一样,zip 将尾随的破折号解释为“使用标准输入作为输入文件”。
当指定 -p(用于管道)选项时,unzip 程序允许将其输出发送到标准输出:
```bash
[me@linuxbox ~]$ unzip -p ls
```
0
0
相关推荐










