使用tar进行压缩

正如我们前面提到的,tar实用程序现在可以自己进行压缩。要向tar实用程序添加压缩,我们可以将tar命令与压缩实用程序结合使用:

j标志可用于指定执行的bzip2压缩。z标志可用于使用gzip压缩实用程序。Z也可以用来使用compress工具。

使用压缩的例子

tar cvfj myarchive.tar.bz2 file1 file2 file3

本示例使用bzip2创建压缩文件。创建了一个名为myarchive.tar.bz2的存档,其中包含file1 file2和file3

tar xvfj myarchive.tar.bz2

我们解压缩归档文件,然后从内部提取文件。

Linux tar命令

Linux的tar命令用于将一组文件转换为档案文件或从现有档案文件中提取文件。归档文件通常是一个单个文件,其中包含多个单独的文件以及相关联的元数据,这些元数据可以使它们恢复为原始格式。存档是存储和分发数据和程序的便捷方法。

名称tar源自术语Tape Archiver,其最初旨在将数据备份到磁带。现在,tar用于在文件系统上的归档中将文件分组在一起。这些归档文件经常被称为tarball

tar命令的基本语法是: tar OPTION... Archive_Name File_Name(s)

基本的tar示例

tar cf myarchive.tar file1 file2

在上面的示例中,我们正在创建和归档名为myarchive.tar的文件,其中将包含文件file1file2

tar tvf myarchive.tar

在此示例中,我们指示tar显示其存档的内容。

tar xvf myarchive.tar

在这里,我们从档案myarchive.tar中详细提取所有文件

tar uvf myarchive.tar file3

在这里,我们向现有档案中添加了一个附加文件。如果档案中已经存在该文件,则仅当档案中的文件早于尝试添加的文件时,才会添加该文件。

tar rf myarchive.tar file4

要将新文件添加到存档中,可以使用r选项。在此示例中,文件file4被添加到归档文件myarchive.tar中。

tar f myarchive.tar --delete file1 file2

此示例将从档案myarchive.tar中删除文件file1file2

tar cvf myarchive.tar ./mytest

在此示例中,我们将目录mytest及其内容添加到存档myarchive.tar

tar xvf myarchive.tar file3

在此示例中,仅从存档中提取了file3。

tar cvf - . |ssh -l john remote_server "cd /tmp/john && tar -xf-"

在此示例中,我们根据标准输入创建档案,然后使用用户名john将其通过管道传输到远程服务器。然后将归档文件放置在远程位置/ tmp/john。如果成功,则提取存档。如果您尝试将大量数据从一个点移动到另一个点,则此特定方法很有用。这个过程是连续的。

bzip2的例子

bzip2 file1

压缩文件file1 ,创建了名为file1.bz2的文件的压缩版本

bzip2 -c file1> file1.bz2

压缩file1文件,保留原始文件

bzip2 -r mydir

此示例将压缩目录mydir下的所有文件。

bzip -d fil1.bz2

将file1.bz2解压缩。这等效于执行bunzip file1.bz2

存档和压缩

存档和压缩实用程序

如何使用tar,cpio,gzip,gunzip,bzip2和compress命令。复制目录结构。

cpio命令

cpio命令用于处理归档文件。其名称源自短语复制进,复制出 Copy in, Copy out

cpio命令的功能可以大致分为三类操作。它们是:将文件复制到存档,从存档中提取文件以及将文件传递到另一个目录树。通常,cpio在创建归档文件时会从标准输入中获取其输入。然后,此信息将发送到标准输出。

cpio基本示例

创建一个基本的cpio存档

john@john-desktop:~/test_examples$ ls -l
total 8
drwxrwxr-x 2 john john 4096 Feb  7 20:47 backup_archives
drwxrwxr-x 2 john john 4096 Feb  7 20:43 testcpio
john@john-desktop:~/test_examples$ cd testcpio/

john@john-desktop:~/test_examples/testcpio$ ls
file1  file2  file3

john@john-desktop:~/test_examples/testcpio$ ls | cpio -ov > ~/test_examples/backup_archives/myarchive.cpio
file1
file2
file3
89 blocks

在上面的示例中,我们将ls命令的输出通过管道传递到cpio。然后,此输出将重定向到您的目标区域。

从cpio存档中提取文件

john@john-desktop:~/test_examples$ mkdir recover

john@john-desktop:~/test_examples$ ls -l
total 12
drwxrwxr-x 2 john john 4096 Feb  7 20:48 backup_archives
drwxrwxr-x 2 john john 4096 Feb  7 20:53 recover
drwxrwxr-x 2 john john 4096 Feb  7 20:43 testcpio

john@john-desktop:~/test_examples$ cd recover/

john@john-desktop:~/test_examples/recover$ cpio -idv < ~/test_examples/backup_archives/myarchive.cpio
file1
file2
file3
89 blocks

john@john-desktop:~/test_examples/recover$ ls -l
total 48
-rw-rw-r-- 1 john john 15078 Feb  7 20:56 file1
-rw-rw-r-- 1 john john 15078 Feb  7 20:56 file2
-rw-rw-r-- 1 john john 15078 Feb  7 20:56 file3

在上面的示例中,我们将文件从cpio存档myarchive.cpio提取到当前目录中。

使用与特定类型匹配的文件创建cpio归档文件

john@john-desktop:~/test_examples/testcpio$ ls -l
total 60
-rw-rw-r-- 1 john john   321 Feb  7 21:12 file1.bak
-rw-rw-r-- 1 john john 15078 Feb  7 20:43 file1.txt
-rw-rw-r-- 1 john john   321 Feb  7 21:12 file2.bak
-rw-rw-r-- 1 john john 15078 Feb  7 20:43 file2.txt
-rw-rw-r-- 1 john john   321 Feb  7 21:12 file3.bak
-rw-rw-r-- 1 john john 15078 Feb  7 20:43 file3.txt

$ find . -iname "*.txt" -print | cpio -ov > ~/test_examples/backup_archives/mytxtfiles.cpio
./file2.txt
./file3.txt
./file1.txt
89 blocks

john@john-desktop:~/test_examples/testcpio$ cd ../backup_archives/

john@john-desktop:~/test_examples/backup_archives$ ls -l
total 48
-rw-rw-r-- 1 john john 45568 Feb  7 21:19 mytxtfiles.cpio

仅选择与模式* .txt匹配的文件进行归档。

使用cpio -F创建.tar归档文件

$ ls | cpio -ov -H tar -F mytar.tar

在此示例中,我们指定-F参数以使用档案mytar.tar

使用cpio提取.tar档案

$ cpio -idv -F mytar.tar

使用cpio命令查看.tar文件的内容

$ cpio -it -F mytar.tar

compress命令

compress命令使用Lempel-Ziv编码减少命名文件的大小。在可能的情况下,每个文件都用扩展名为.Z的文件替换。进行此压缩时要保留相同的所有权模式,访问和修改时间。如果未指定文件,则将标准输入压缩为标准输出。压缩例程将仅尝试压缩常规文件。要将文件解压缩为原始格式,将执行uncompress命令。有关compress命令的更多信息,请从命令行执行compress --help

compress命令示例

john@john-desktop:~/test_examples$ ls -l
total 16
-rw-rw-r-- 1 john john  48 Feb  6 22:07 file1
-rw-rw-r-- 1 john john  48 Feb  3 20:50 file2
-rw-rw-r-- 1 john john 135 Feb  3 21:00 file3
-rw-rw-r-- 1 john john 135 Feb  6 21:41 file4

john@john-desktop:~/test_examples$ compress file1

john@john-desktop:~/test_examples$ ls -l
total 16
-rw-rw-r-- 1 john john  41 Feb  6 22:07 file1.Z
-rw-rw-r-- 1 john john  48 Feb  3 20:50 file2
-rw-rw-r-- 1 john john 135 Feb  3 21:00 file3
-rw-rw-r-- 1 john john 135 Feb  6 21:41 file4

john@john-desktop:~/test_examples$ compress file2 file3 file4

john@john-desktop:~/test_examples$ ls -l
total 16
-rw-rw-r-- 1 john john  41 Feb  6 22:07 file1.Z
-rw-rw-r-- 1 john john  41 Feb  3 20:50 file2.Z
-rw-rw-r-- 1 john john 100 Feb  3 21:00 file3.Z
-rw-rw-r-- 1 john john 100 Feb  6 21:41 file4.Z

john@john-desktop:~/test_examples$ uncompress file3

john@john-desktop:~/test_examples$ ls -l
total 16
-rw-rw-r-- 1 john john  41 Feb  6 22:07 file1.Z
-rw-rw-r-- 1 john john  41 Feb  3 20:50 file2.Z
-rw-rw-r-- 1 john john 135 Feb  3 21:00 file3
-rw-rw-r-- 1 john john 100 Feb  6 21:41 file4.Z

john@john-desktop:~/test_examples$ uncompress file1.Z file2.Z file4.Z

john@john-desktop:~/test_examples$ ls -l
total 16
-rw-rw-r-- 1 john john  48 Feb  6 22:07 file1
-rw-rw-r-- 1 john john  48 Feb  3 20:50 file2
-rw-rw-r-- 1 john john 135 Feb  3 21:00 file3
-rw-rw-r-- 1 john john 135 Feb  6 21:41 file4

tar,cpio,gzip,gunzip,bzip2实用程序

有许多不同的工具可用于在Linux系统中归档数据。许多流行的工具都是基于命令行的,这使管理员可以更轻松地将它们合并到脚本中。
最受欢迎的命令行工具是tarcpiogzipbzip2
这些工具中的第一个tar不是压缩实用程序,但是,它经常与压缩实用程序(例如gzipbzip2)结合使用。

gzip和gunzip命令

gzip是一种流行的压缩工具,可以使用Lempel-Ziv算法减少命名文件的大小。压缩文件时,将创建扩展名为.gz的压缩版本。gzip将仅压缩常规文件,而不压缩符号链接。默认情况下,gzip将原始文件名和时间戳保留在压缩文件中。

gunzip能够从gzipcompress解压缩文件。gunzip可以从命令行获取扩展名结尾的文件名列表:。gz,-gz,.z,-z,_z或.Z。gunzip还可以处理特殊的扩展名:。tgz和.taz分别是.tar.gz和.tar.Z的简写形式

gzip示例

gzip file1

file1被压缩,以创建名为file1.gz的文件的压缩版本。现在,原始文件仅以压缩格式存在!

john@john-desktop:~/test_examples/testcpio$ ls -l
total 48
-rw-rw-r-- 1 john john 15078 Feb  7 21:35 file1
-rw-rw-r-- 1 john john 15078 Feb  7 21:35 file2
-rw-rw-r-- 1 john john 15078 Feb  7 21:36 file3

john@john-desktop:~/test_examples/testcpio$ gzip file1

john@john-desktop:~/test_examples/testcpio$ ls -l
total 36
-rw-rw-r-- 1 john john  3229 Feb  7 21:35 file1.gz
-rw-rw-r-- 1 john john 15078 Feb  7 21:35 file2
-rw-rw-r-- 1 john john 15078 Feb  7 21:36 file3
gzip -c file1> file1.gz

此示例压缩文件后,将保留原始文件。

john@john-desktop:~/test_examples/testcpio$ ls -l
total 48
-rw-rw-r-- 1 john john 15078 Feb  7 21:35 file1
-rw-rw-r-- 1 john john 15078 Feb  7 21:35 file2
-rw-rw-r-- 1 john john 15078 Feb  7 21:36 file3

john@john-desktop:~/test_examples/testcpio$ gzip -c file1 > file1.gz

john@john-desktop:~/test_examples/testcpio$ ls -l
total 52
-rw-rw-r-- 1 john john 15078 Feb  7 21:35 file1
-rw-rw-r-- 1 john john  3229 Feb  7 21:39 file1.gz
-rw-rw-r-- 1 john john 15078 Feb  7 21:35 file2
-rw-rw-r-- 1 john john 15078 Feb  7 21:36 file3
gzip -r mydir

此示例将压缩给定目录下的所有文件。

gzip -d file1.gz

这将解压缩file1.gz。这等效于执行gunzip file1.gz

日期:2019-04-29 03:17:28 来源:oir作者:oir