tar工具的使用

---------------------------------------------------------------------------------------
使用 man tar 或是 info tar 可以查看tar命令的详细信息
---------------------------------------------------------------------------------------

文章目录

一、tar工具简介

  1. 基本概念
  2. tar用途
  3. tar命令参数
  4. tar退出码

二、tar操作模式

  1. 基本操作
  2. 高级操作

三、tar选项

  1. 最常用的两个选项: --file 和 --verbose
  2. 文件相关选项
  3. 压缩相关选项
  4. 文件覆盖选项
  5. 其他选项

三、tar简单实例

  1. 简单打包
  2. 压缩与解压缩

一、tar工具简介


tar原本的意思是 tape archiver ,表示磁带存档器,用来把一大堆文件整理存档到磁带中。GNU tar 最早是由 John Gilmore 开发完成,并且得到了持续的改进和更新,也就是现在我们在Linux中使用的 tar 工具,它不仅可以对文件打包,还可以对其进行压缩,查看,添加以及解压等一系列操作。

  1. 几个基本概念

    • 存档(archive):或者是归档,指包含很多文件的单个文件,并且存档前后这些内部文件的名称,用户组等信息保持不变
    • 成员(member):指存档内部的文件
    • 提取(extraction):指从存档中将成员复制出来的过程
  2. tar的用途很广,比较常见的有

    • 存储(storage):tar广泛应用在网络中的文件传输,并且也适用于长期储存
    • 备份(backup):tar能够保留文件信息和目录结构,因此也常常出现在磁盘备份或增量备份中
    • 传输(transportation):在不同系统之间进行数据传输时也可以大显身手
  3. tar命令参数可以分为两类

    • 操作(operation):也可以称作操作模式(operating modes),共有八种,必须且只能定义一个
    • 选项(option):如果参数属于选项类,那么它是可选的,并且可以组合,常用的有 --file, --verbose

    参数的书写方式有三种,其中旧格式不推荐使用,一般我们使用长格式短格式即可,例如

    # 从file1和file2两个文件创建存档:长格式,短格式
    tar --create -verbose --file=afiles.tar file1 file2
    tar -c -v -f afiles.tar file1 file2
    
  4. tar命令的退出码

    • 0 - 成功终止
    • 1 - 对于--compare来说,表示存档内容与磁盘上对应文件不同,对于--create, --append 或者 --update来说,表示一些文件在存档过程中被改变了,也就是说存档中包含的不是这个文件的精确拷贝。
    • 2 - 严重错误,常常表示一些严重却不可恢复的错误发生

二、tar的操作模式


GNU tar has a total of eight operating modes which allow you to perform a variety of tasks. You are required to choose one operating mode each time you employ the tar program by specifying one, and only one operation as an argument to the tar command.

在info的帮助文档中我们可以看到tar一共有8个操作模式,可以分为基本操作(Basic)和高级操作(advanced):

各种操作的说明信息列表如下:

OperationsDescription
--append  -rAppends files to the end of the archive.
--catenate  -ASame as --concatenate.
--compare  -dCompares archive members with their counterparts in the file system, and reports differences in file size, mode, owner, modification date and contents.
--concatenate  -AAppends other tar archives to the end of the archive.
--create  -cCreates a new tar archive.
--deleteDeletes members from the archive. Don't try this on an archive on a tape!
--diff  -dSame --compare.
--extract  -xExtracts members from the archive into the file system.
--get  -xSame as --extract.
--list  -tLists the members in an archive.
--update  -uAdds files to the end of the archive, but only if they are newer than their counterparts already in the archive, or if they do not already exist in the archive.

注:上表共有11项,其中3项重复

tar基本操作


包含的三个基本操作是:createlistextract

创建tar文件


tar命令使用--create-c操作创建存档文件。默认情况下,--file指定的存档文件会对同名文件进行覆盖,因此在创建前应该确保不会造成数据丢失。

1. 多个文件的归档
如果我们要创建一个collection.tar的文件,它包含了bluesfolkjazz三个文件,命令如下

tar --create --verbose --file=collection.tar blues folk jazz
#短格式
tar -cvf collection.tar blues folk jazz

2. 目录归档
首先保证当前工作目录位于要归档的目录结构之外,例如当前位于practice目录的上一级,归档practice目录的命令如下

tar --create --verbose --file=music.tar practice

该命令会将practice及其下所有内容打包成music.tar文件。有时我们会想到以下写法的命令

tar --create --file=foo.tar .

从命令内容来看我们似乎是想把当前所在目录打包到foo.tar文件里,但令人感到迷惑的是要生成的foo.tar文件就位于当前目录,这样岂不是陷入无尽的循环??? 实际上,tar命令在运行时会首先创建好归档文件foo.tar,之后再对文件或目录一个一个地进行归档,当碰到foo.tar文件时,由于对自己进行归档毫无意义,GNU tar工具会跳过它继续运行,但对其他tar工具则不一定,因此推荐最好避免这种做法。

查看tar文件


tar使用--list-t选项来查看存档文件的内容,示例如下

#使用类似于`ls -l`的格式详细显示
tar --list --verbose --file=collection.tar
#短格式
tar -tvf collection.tar

值得注意的是:

1. GNU tar工具在创建(create)和查看(list)模式下显示是不一样的,运行示例

$ tar --create --verbose --file archive /etc/mail
 tar: Removing leading '/' from member names
 /etc/mail/
 /etc/mail/sendmail.cf
 /etc/mail/aliases
 
$ tar --list --file archive
 etc/mail/
 etc/mail/sendmail.cf
 etc/mail/aliases

可以看到tar在创建存档时会移除文件的一些目录前缀,上例中便是将mail目录所在路径的根目录前缀(斜杠/)去掉了。如果希望在创建tar存档时显示真实的内部文件信息,即移除前缀之后的文件名列表,可以使用--show-stored-names选项。

2. 查看tar存档文件内容时,可以指定一个或多个成员名,如果指定的成员是一个目录,那么该目录下的所有内容,包括子目录,都会被列出。在指定成员名时,必须保证你指定的名称与tar文件内部显示的名称一字不差,否则会出错,示例

#由于bfiles.tar中显示的成员名是`./birds`,因此如果指定显示`birds`会提示找不到
$ tar --list --file=bfiles.tar birds

另外,在指定成员名时可以使用通配符,示例

#列出名称中包含字母`b`的所有成员
$ tar --list --file=bfiles.tar --wildcards '*b*'

提取tar文件


tar使用--extract--get-x来实现存档文件的提取。一般地,对于压缩后的存档文件的提取可以简单地称为解压缩。如果在提取时不改变工作目录,tar会将存档内的文件提取到当前工作目录下,下面是常见的几种用法

#提取整个存档
$ tar -xvf collection.tar
#提取指定成员
tar --extract --file=bfiles.tar ./birds

注意:如果你想要提取存档内成员的下一层成员,必须指定完整的名称,例如,查看music.tar存档内容,如下

$ tar -tvf music.tar
 practice/blues
 practice/folk
 practice/jazz

如果只想提取其中的folk,jazz文件,应该怎样写呢?如下

$ tar -xvvf music.tar practice/folk practice/jazz

这时就会在当前目录下创建一个practice目录,并将提取出的两个文件放到里面,而如果使用 tar -xvf music.tar folk jazz 的写法,如果当前工作目录不是practice目录,就会出错。

tar高级操作


包含的五个高级操作是:append,update,concatenate,delete,compare

:下面仍然使用practice目录,它包含

  • 三个文件:jazz, blues, folk
  • collection.tar包含blues, folk, jazz
  • music.tar包含practice/, practice/blues, practice/folk, practice/jazz 和practice/collection.tar
  • afiles.tar包含apple, angst 和aspic
  • bfiles.tar包含./birds, baboon 和./box

在tar存档中添加文件


一般情况下,我们直接使用--append-r操作向一个已有的存档中添加文件,并且必须指定添加的文件,示例如下

#新创建一个`rock`文件,然后添加到collection.tar
tar --append --file=collection.tar rock

但有一个复杂点的情况:如果你使用--append添加文件时,存档中已经存在了一个同名成员,这时候会发生什么?

  • append操作:tar实际上允许无限多个重名文件出现在存档中,因此这种情况下append操作仍成功
  • list操作:list操作会老老实实地列出存档文件中的所有成员,包括重名的
  • extract操作:你会发现extract操作后,看起来只有你最后添加的那个重名文件被提取出来了,其他的貌似没有被提取,想一想为什么? 也可以通过--occurence选项来控制要提取哪一个(按添加时间排序)重名文件,请查看tar帮助文档。

对于多个成员文件重名的情况,我们应当尽量避免,如果在打包之后更新了某个文件,同时也希望存档中的对应文件也更新,那么使用--update选项更好,而不应该是使用--append导致重名文件的出现。多个同名成员的情况如下示例,

#首先再次添加文件`blues`
$ tar --append --verbose --file=collection.tar blues
#查看现在collection.tar存档中的内容
$ tar --list --verbose --file=collection.tar
 -rw-r--r-- me/user          28 1996-10-18 16:31 jazz
 -rw-r--r-- me/user          21 1996-09-23 16:44 blues
 -rw-r--r-- me/user          20 1996-09-23 16:44 folk
 -rw-r--r-- me/user          20 1996-09-23 16:44 rock
 -rw-r--r-- me/user          58 1996-10-24 18:30 blues

更新tar成员文件


除了可以向tar存档添加文件外,我们还可以使用--update-u选项来更新其中的文件。可以分为两种情况来讨论

1. 存档中没有指定文件 - 也就是说在存档中不存在和你指定文件同名的成员文件,那么实际上它和--append一样,添加该文件到存档。
2. 存档中存在同名文件 - 这种情况下,update操作通过比较文件的修改日期来决定是否更新,如果指定文件比存档中的同名成员日期新,那么新文件会被加入存档,但旧成员文件仍然存在。[注意update与append还是有一些小区别的,append不会进行日期比较,会直接添加,而update则会比较日期,只有新版本才能加入,即使你指定的版本存档里没有]

--update操作实际上并不适合做数据备份,因为它会使存档文件不断增大,若要进行数据备份,可查看相关的backup选项。

合并tar存档


tar操作--concatenate--catenate-A可以将多个存档文件合并在一起,使用--file-f指定第一个存档,后接多个存档文件。其中第一个存档名将作为合并后的存档名,示例如下

$ tar --concatenate --file=bluesrock.tar jazzfolk.tar

注意,要合并的存档必须已经存在,并且文件格式兼容,否则毫无意义。

移除tar成员文件


tar使用--delete从存档中移除某个成员文件,和--extract操作方式类似,你必须指定成员在存档中的准确名称来实现删除,示例

#删除存档中所有名字是`blues`的成员
$ tar --delete --verbose --file=collection.tar blues

注意,tar的删除操作有时会比较慢,另外如果要删除的成员不存在,tar会报错退出。

比较tar成员文件


tar使用--compare--diff-d来实现存档内的成员与外部同名文件的比较,示例

#只需要指定要比较的存档成员名即可,默认是整个存档
$ tar --compare --file=bluesrock.tar rock blues funk
 rock
 blues
 tar: funk not found in archive

从以上命令输出可以看出,rock,blues成员文件和磁盘上对应文件没有区别,而funk在存档中不存在。如果当前目录下不存在该成员对应文件,也会显示类似的警告信息,tar会忽略其他文件。

三、tar选项


除了上面的8个重要操作选项外,tar命令还有多达几十个选项对命令的细节进行控制,下面列出了一些常用的选项及其简述

最常用的两个选项: --file 和 --verbose


  • --file

    --file=ARCHIVE -f ARCHIVE
    tar will use the file ARCHIVE as the tar archive it performs operations on, rather than tar's compilation dependent default.

    任何时候都可以使用这个选项,它表示tar所操作的存档文件名。

  • --verbose

    --verbose -v
    Specifies that tar should be more verbose about the operations it is performing. This option can be specified multiple times for some operations to increase the amount of information displayed.

    使用这个选项可以更加详细地显示tar命令的运行结果,示例

      #详细显示
      tar -cvf afiles.tar apple angst aspic
      #更详细显示
      tar -cvvf afiles.tar apple angst aspic
    

文件相关选项


主要包含:--directory 用于改变tar存档输出目录,--exclude 用于在打包或其他tar操作时排除一些文件,以及其他与文件相关的选项,如下

  • --directory

    --directory=DIR -C DIR
    When this option is specified, tar will change its current directory to DIR before performing any operations. When this option is used during archive creation, it is order sensitive.

  • --exclude

    --exclude=PATTERN
    When performing operations, tar will skip files that match PATTERN.
    --exclude-from=FILE -X FILE
    Similar to --exclude, except tar will use the list of patterns in the file FILE.

  • --ignore-case

    --ignore-case - Ignore case when matching member or file names with patterns.

    如果希望匹配时大小写敏感,则可以使用--no-ignore-case选项。

  • --wildcards

    --wildcards - Use wildcards when matching member names with patterns.
    --no-wildcards - Do not use wildcards.

  • --files-from

    --files-from=FILE -T FILE
    tar will use the contents of FILE as a list of archive members or files to operate on, in addition to those specified on the command-line.

  • --index-file

    --index-file=FILE
    Send verbose output to FILE instead of to standard output.

压缩相关选项


tar可以与压缩软件结合使用,最常用的当属gzip了,我们经常看到的以.tar.gz结尾的文件就是采用gzip工具生成的。另外还有bz2xz等格式,如下

  • --gzip

    --gzip --gunzip --ungzip -z
    This option tells tar to read or write archives through gzip, allowing tar to directly operate on several kinds of compressed archives transparently.

  • --bzip2

    --bzip2 -j
    This option tells tar to read or write archives through bzip2.

  • --xz

    --xz -J
    Use xz for compressing or decompressing the archives.

  • 其他类型

    --lzip - This option tells tar to read or write archives through lzip.
    --lzma - This option tells tar to read or write archives through lzma.
    --lzop - This option tells tar to read or write archives through lzop.

文件覆盖选项


默认情况下,tar在提取成员时,如果成员文件名已经存在,那么tar会进行文件覆盖。如果你觉得打包压缩,或是提取解压缩时可能有重名文件,但不想覆盖原有文件,怎么办呢?很简单,查看下面的选项说明,然后组合适合你的选项就可以了。

  • --interactive

    --interactive --confirmation -w
    Specifies that tar should ask the user for confirmation before performing potentially destructive options, such as overwriting files.

  • --keep-newer-files

    --keep-newer-files
    Do not replace existing files that are newer than their archive copies when extracting files from an archive.
    --keep-old-files -k
    Do not overwrite existing files when extracting files from an archive. Return error if such files exist.

  • --overwrite

    --overwrite
    Overwrite existing files and directory metadata when extracting files from an archive.
    --overwrite-dir
    Overwrite the metadata of existing directories when extracting files from an archive.
    --no-overwrite-dir
    Preserve metadata of existing directories when extracting files from an archive.

  • --skip-old-files

    Do not overwrite existing files when extracting files from an archive. This option differs from --keep-old-files in that it does not treat such files as an error, instead it just silently avoids overwriting them. The --warning=existing-file option can be used together with this option to produce warning messages about existing old files.

其他选项


下表列出了其他一些可能用到的选项

选项格式说明
--absolute-names -P默认情况下,tar会把成员名中的/符号移除,这个选项会关闭这种操作
--recursion对目录进行循环操作,默认开启,可以使用--no-recursion关闭
--show-defaults显示tar选项的默认值
--totals[=SIGNO]显示在处理存档文件时传输的字节数
--warning=KEYWORD根据 KEYWORD 指示开启或关闭警告信息,例如 KEYWORD 以no-开头就不会显示警告
--touch -m在提取存档成员时,设置其修改时间为提取时间
--help -h帮助文档

四、tar简单实例


虽然选项的完整格式便于记忆,但当你对tar熟悉之后可能更喜欢短格式,因此实际中我一般选择短格式,方便书写和使用,下面的实例讲解也是优先选择短格。另外,实际使用中存档大多附带压缩效果,因此下面使用压缩包的概念。

tar命令简要列表:

-c: 创建压缩包 -x:解压缩 -t:查看内容 -r:向压缩包末尾追加文件 -u:更新压缩包中的文件 -A:合并压缩包 -d:比较压缩包内文件和磁盘上的文件 --delete:删除压缩包中的文件

-f:指定压缩包名称,并且后面只能接压缩包名称 -v:显示详细过程 -z:附带gzip效果 -j:附带bz2效果

数据的简单打包整理


例一:将整个 /home/user1 目录下的文件全部打包到 /tmp/user1_backup.tar

$ tar -cf /tmp/user1_backup.tar /home/user1

例二:查阅 /tmp/etc.tar.gz 文件内有哪些文件?

$ tar -tzvf /tmp/etc.tar.gz

例三:将 /tmp/user1_backup.tar.gz 文件解压在 /home/user1 底下

$ tar -xzvf /tmp/user1_backup.tar.gz -C /home/user1

注意,解压缩user1_backup.tar.gz文件时,会将里面的内容目录结构放到 /home/user1 目录下,即在它下面创建 home/user1 的目录。

压缩与解压缩


最常用的使用方式如下:

# 压缩
$ tar -czvf notes.tar.gz *.txt
# 解压缩
$ tar -xzvf notes.tar.gz

其他压缩包格式对应如下表:

文件类型工具
*.tartar
*.gzgzip
*.tar.gztar
*.bz2bzip2
*.tar.bz2tar
*.Zuncompress
*.tar.Ztar
*.rarunrar
*.zipunzip

注意:对于rar和zip压缩文件就不能使用tar了,而其他的都可以使用tar的不同选项来完成。

转载于:https://my.oschina.net/wqli/blog/821700

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值