由
gzip和bzip2的介绍可知,这两种压缩命令只能针对单个文件进行压缩,并不能压缩多个文件形成一个压缩文件。
为了实现对多个文件或目录的压缩操作,可以使用tar命令将多个文件或目录归档为一个文件,然后再使用压缩命令对该归档文件进行压缩。
tar命令本身只具有打包文件的功能,并不提供压缩文件的功能,对打包后文件的压缩是通过参数调用其他压缩命令完成的。
【1】打包多个文件
tmpuser:test/ $ ls a b directory tmpuser:test/ $ ls directory c d tmpuser:test/ $ tar -cvf file.tar a b directory a b directory/ directory/c directory/d tmpuser:test/ $ ls a b directory file.tar
由该示例可知,tar命令可以将多个文件打包为一个归档文件,只需要将多个文件排列在命令后即可。这里的三个参数意思为:
-c:create创建归档文件-v:verbose显示创建过程-f:file指定归档文件名称
这里需要注意的是,
-f参数后需要直接加归档文件名,即无论是建立归档文件,或者从归档文件提取文件,-f参数都是作为最后一个参数,出现在指定的归档名称之前。
tar命令的主要操作类型:
(1)-c:create创建归档文件
(2)-x:extract从归档文件中提取文件
(3)-t:list查看归档文件
(4)-r:append添加文件到归档文件中
(5)-u:update添加新文件到归档文件中
(6)-A:catenate添加归档文件到归档文件中
(7)--delete:从归档文件中删除文件
(8)--exclude:归档文件过程中排除指定文件
需要注意,上面提到的几种操作类型,在一个
tar命令中只能同时做其中一种操作。
前面的示例展示了-c参数表示的创建归档文件操作,下面介绍其他几种主要操作。
【2】从归档文件中提取文件
tmpuser:test/ $ ls file.tar tmpuser:test/ $ tar -xvf file.tar a b directory/ directory/c directory/d tmpuser:test/ $ ls a b directory file.tar tmpuser:test/ $ ls directory c d
由示例可知,-x参数表示从归档文件中提取文件,这里-x表示extract提取文件意思。
【3】查看归档文件
tmpuser:test/ $ tar -tf file.tar a b directory/ directory/c directory/d
由示例可知,-t参数表示查看归档文件,这里-t表示list查看文件列表意思。
【4】添加文件到归档文件中
tmpuser:test/ $ ls a b directory file.tar new_file tmpuser:test/ $ tar -tf file.tar a b directory/ directory/c directory/d tmpuser:test/ $ tar -rvf file.tar a new_file a new_file tmpuser:test/ $ tar -tvf file.tar a b directory/ directory/c directory/d a new_file
由示例可知,这里创建了一个新文件new_file,使用-r参数,添加旧文件a和新文件new_file到归档文件中,使用-t参数查看归档文件可知,两个文件都可以添加到归档文件中,其中同名文件在执行提取操作时,会被后加入的同名文件覆盖掉。使用-r参数添加文件到归档文件中时,不会去判断该文件是否已经存在于归档文件中,或者该文件是否发生了更新,都会直接添加进归档文件中。
【5】添加新文件到归档文件中
tmpuser:test/ $ ls a another_new_file b directory file.tar new_file tmpuser:test/ $ tar -tf file.tar a b directory/ directory/c directory/d a new_file tmpuser:test/ $ tar -uvf file.tar b another_new_file another_new_file tmpuser:test/ $ tar -tvf file.tar a b directory/ directory/c directory/d a new_file another_new_file
由该示例可知,使用-u参数添加文件时,会判断该文件是否已作了更新,或该文件是否已经存在于归档文件中,若文件已存在于归档文件中,且文件内容并未发生更新的话,则不会添加该文件到归档文件中,若文件发生了更新,则会添加到归档文件中,在执行提取操作时,会被后加入的同名文件覆盖掉,即归档文件中文件发生了更新update。
【6】添加归档文件到归档文件中
tmpuser:test/ $ tar -tf file.tar a b directory/ directory/c directory/d a new_file another_new_file tmpuser:test/ $ tar -tf b.tar b tmpuser:test/ $ tar -Af file.tar b.tar tmpuser:test/ $ tar -tf file.tar a b directory/ directory/c directory/d a new_file another_new_file b
由该示例可知,使用-A参数可以将一个归档文件中的文件移动到另一个归档文件中,其中-A表示catenate连接的意思。
【7】从归档文件中删除文件
tmpuser:test/ $ tar -tf file.tar a b directory/ directory/c directory/d a new_file another_new_file b tmpuser:test/ $ tar -f file.tar --delete a b tmpuser:test/ $ tar -tf file.tar directory/ directory/c directory/d new_file another_new_file
由该示例可知,使用--delete参数可以从归档文件中删除指定文件。
【8】归档文件过程中排除指定文件
tmpuser:test/ $ ls directory c d tmpuser:test/ $ tar -cvf directory.tar directory --exclude directory/c directory/ directory/d tmpuser:test/ $ tar -tf directory.tar directory/ directory/d
由该示例可知,使用--exclude参数可以在归档文件过程中排除指定文件。
前面已经提过,
tar命令本身只提供打包文件的功能,但是可以调用其他压缩命令完成对多个文件或目录的打包后压缩。
tar命令调用的主要压缩类型:
(1)-z:gzip压缩
(2)-j:bzip2压缩
(3)-a:auto-compress根据指定的后缀自动识别压缩方式
【1】打包并用gzip压缩归档文件
tmpuser:test/ $ ls a b directory tmpuser:test/ $ tar -zcvf file.tar.gz a b directory a b directory/ directory/c directory/d tmpuser:test/ $ ls a b directory file.tar.gz
由该示例可知,使用-z参数表示在打包后使用gzip压缩归档文件。
【2】打包并用bzip2压缩归档文件
tmpuser:test/ $ ls a b directory file.tar.gz tmpuser:test/ $ tar -jcvf file.tar.bz2 a b directory a b directory/ directory/c directory/d tmpuser:test/ $ ls a b directory file.tar.bz2 file.tar.gz
由该示例可知,使用-j参数表示在打包后使用bzip2压缩归档文件。
【3】打包并自动识别压缩方式进行归档文件压缩
tmpuser:test/ $ ls a b directory tmpuser:test/ $ tar -acvf file.tar.gz a b directory a b directory/ directory/c directory/d tmpuser:test/ $ ls a b directory file.tar.gz tmpuser:test/ $ tar -acvf file.tar.bz2 a b directory a b directory/ directory/c directory/d tmpuser:test/ $ ls a b directory file.tar.bz2 file.tar.gz
关于tar命令使用过程中的目录切换,可以使用-C参数切换执行打包时的目录,以及切换提取文件到指定的目录下。
【1】对指定目录下的文件进行打包
tmpuser:test/ $ tar -cvf file.tar -C directory/ c c tmpuser:test/ $ ls a b directory file.tar tmpuser:test/ $ tar -tf file.tar c
由该示例可知,使用-C参数可以对指定目录的文件进行打包。
【2】提取归档文件到指定目录下
tmpuser:test/ $ rm directory/c tmpuser:test/ $ ls directory d tmpuser:test/ $ tar -xvf file.tar -C directory c tmpuser:test/ $ ls directory c d
由该示例可知,使用-C参数可以提取归档文件到指定目录下。
这里需要注意打包过程中相对或者绝对目录的使用,提取文件时需要注意提取的目录,若直接在当前目录下进行提取操作,则会创建不同等级的目录结构。
【3】打包与提取时的目录设置
tmpuser:test/ $ ls b directory tmpuser:test/ $ tar -cvf file.tar ../test/b tar: 从成员名中删除开头的“../” ../test/b tmpuser:test/ $ rm b tmpuser:test/ $ ls directory file.tar tmpuser:test/ $ tar -xvf file.tartest/b tmpuser:test/ $ ls directory file.tar testtmpuser:test/ $ ls testb tmpuser:test/ $ tar -xvf file.tar -C ../test/b tmpuser:test/ $ ls b directory file.tar test
作者:zhipingChen
链接:https://www.jianshu.com/p/7a15c238acd2
随时随地看视频