Linux shell编程学习笔记76:tar命令——快照 & 备份(上)_shell编程

0 前言

提取系统快照和备份文件都是我们的日常工作。

在Windows中,我们可以创建系统还原点,在需要的时候可以把系统还原回去。

在Linux中,我们可以使用tar命令来实现。

1 tar命令的功能,帮助信息,格式,选项和参数说明

1.1  tar命令的功能

tar 名字来自 "tape archive"(磁带归档),最初用于将文件打包到磁带设备中,但现在已成为Linux 和 Unix 系统中用于归档文件和目录的强大命令行工具,广泛应用于文件系统中打包和压缩文件。

tar通常用于备份个人或系统文件来创建存档,称为 "tarball"(通常带有 .tar 扩展名),特别是在进行可能需要撤消的更改之前,例如,Linux 系统管理员通常会在更改应用之前创建包含一系列配置文件的 tarball,以防必须撤消这些更改。

从 tarball 中解压文件通常比在备份中搜索文件快。

使用tar命令必须带参数或选项。

1.2 tar命令的帮助信息

我们可以使用命令

tar --help

或 

tar --usage

来查看tar命令的帮助信息。

1.2.1 bash下的tar命令帮助信息

[purpleendurer @ bash ~] tar
tar: You must specify one of the `-Acdtrux' or `--test-label'  options
Try `tar --help' or `tar --usage' for more information.
[purpleendurer @ bash ~] tar --help
Usage: tar [OPTION...] [FILE]...
GNU `tar' saves many files together into a single tape or disk archive, and can
restore individual files from the archive.

Examples:
  tar -cf archive.tar foo bar  # Create archive.tar from files foo and bar.
  tar -tvf archive.tar         # List all files in archive.tar verbosely.
  tar -xf archive.tar          # Extract all files from archive.tar.

 Main operation mode:

  -A, --catenate, --concatenate   append tar files to an archive
  -c, --create               create a new archive
  -d, --diff, --compare      find differences between archive and file system
      --delete               delete from the archive (not on mag tapes!)
  -r, --append               append files to the end of an archive
  -t, --list                 list the contents of an archive
      --test-label           test the archive volume label and exit
  -u, --update               only append files newer than copy in archive
  -x, --extract, --get       extract files from an archive

 Operation modifiers:

      --check-device         check device numbers when creating incremental
                             archives (default)
  -g, --listed-incremental=FILE   handle new GNU-format incremental backup
  -G, --incremental          handle old GNU-format incremental backup
      --ignore-failed-read   do not exit with nonzero on unreadable files
      --level=NUMBER         dump level for created listed-incremental archive
  -n, --seek                 archive is seekable
      --no-check-device      do not check device numbers when creating
                             incremental archives
      --no-seek              archive is not seekable
      --occurrence[=NUMBER]  process only the NUMBERth occurrence of each file
                             in the archive; this option is valid only in
                             conjunction with one of the subcommands --delete,
                             --diff, --extract or --list and when a list of
                             files is given either on the command line or via
                             the -T option; NUMBER defaults to 1
      --sparse-version=MAJOR[.MINOR]
                             set version of the sparse format to use (implies
                             --sparse)
  -S, --sparse               handle sparse files efficiently

 Overwrite control:

  -k, --keep-old-files       don't replace existing files when extracting,
                             treat them as errors
      --keep-directory-symlink   preserve existing symlinks to directories when
                             extracting
      --keep-newer-files     don't replace existing files that are newer than
                             their archive copies
      --no-overwrite-dir     preserve metadata of existing directories
      --overwrite            overwrite existing files when extracting
      --overwrite-dir        overwrite metadata of existing directories when
                             extracting (default)
      --recursive-unlink     empty hierarchies prior to extracting directory
      --remove-files         remove files after adding them to the archive
      --skip-old-files       don't replace existing files when extracting,
                             silently skip over them
  -U, --unlink-first         remove each file prior to extracting over it
  -W, --verify               attempt to verify the archive after writing it

 Select output stream:

      --ignore-command-error ignore exit codes of children
      --no-ignore-command-error   treat non-zero exit codes of children as
                             error
  -O, --to-stdout            extract files to standard output
      --to-command=COMMAND   pipe extracted files to another program

 Handling of file attributes:

      --atime-preserve[=METHOD]   preserve access times on dumped files, either
                             by restoring the times after reading
                             (METHOD='replace'; default) or by not setting the
                             times in the first place (METHOD='system')
      --delay-directory-restore   delay setting modification times and
                             permissions of extracted directories until the end
                             of extraction
      --group=NAME           force NAME as group for added files
      --mode=CHANGES         force (symbolic) mode CHANGES for added files
      --mtime=DATE-OR-FILE   set mtime for added files from DATE-OR-FILE
  -m, --touch                don't extract file modified time
      --no-delay-directory-restore
                             cancel the effect of --delay-directory-restore
                             option
      --no-same-owner        extract files as yourself (default for ordinary
                             users)
      --no-same-permissions  apply the user's umask when extracting permissions
                             from the archive (default for ordinary users)
      --numeric-owner        always use numbers for user/group names
      --owner=NAME           force NAME as owner for added files
  -p, --preserve-permissions, --same-permissions
                             extract information about file permissions
                             (default for superuser)
      --preserve             same as both -p and -s
      --same-owner           try extracting files with the same ownership as
                             exists in the archive (default for superuser)
  -s, --preserve-order, --same-order
                             member arguments are listed in the same order as
                             the files in the archive

 Handling of extended file attributes:

      --acls                 Enable the POSIX ACLs support
      --no-acls              Disable the POSIX ACLs support
      --no-selinux           Disable the SELinux context support
      --no-xattrs            Disable extended attributes support
      --selinux              Enable the SELinux context support
      --xattrs               Enable extended attributes support
      --xattrs-exclude=MASK  specify the exclude pattern for xattr keys
      --xattrs-include=MASK  specify the include pattern for xattr keys

 Device selection and switching:

  -f, --file=ARCHIVE         use archive file or device ARCHIVE
      --force-local          archive file is local even if it has a colon
  -F, --info-script=NAME, --new-volume-script=NAME
                             run script at end of each tape (implies -M)
  -L, --tape-length=NUMBER   change tape after writing NUMBER x 1024 bytes
  -M, --multi-volume         create/list/extract multi-volume archive
      --rmt-command=COMMAND  use given rmt COMMAND instead of rmt
      --rsh-command=COMMAND  use remote COMMAND instead of rsh
      --volno-file=FILE      use/update the volume number in FILE

 Device blocking:

  -b, --blocking-factor=BLOCKS   BLOCKS x 512 bytes per record
  -B, --read-full-records    reblock as we read (for 4.2BSD pipes)
  -i, --ignore-zeros         ignore zeroed blocks in archive (means EOF)
      --record-size=NUMBER   NUMBER of bytes per record, multiple of 512

 Archive format selection:

  -H, --format=FORMAT        create archive of the given format

 FORMAT is one of the following:

    gnu                      GNU tar 1.13.x format
    oldgnu                   GNU format as per tar <= 1.12
    pax                      POSIX 1003.1-2001 (pax) format
    posix                    same as pax
    ustar                    POSIX 1003.1-1988 (ustar) format
    v7                       old V7 tar format

      --old-archive, --portability
                             same as --format=v7
      --pax-option=keyword[[:]=value][,keyword[[:]=value]]...
                             control pax keywords
      --posix                same as --format=posix
  -V, --label=TEXT           create archive with volume name TEXT; at
                             list/extract time, use TEXT as a globbing pattern
                             for volume name

 Compression options:

  -a, --auto-compress        use archive suffix to determine the compression
                             program
  -I, --use-compress-program=PROG
                             filter through PROG (must accept -d)
  -j, --bzip2                filter the archive through bzip2
  -J, --xz                   filter the archive through xz
      --lzip                 filter the archive through lzip
      --lzma                 filter the archive through lzma
      --lzop
      --no-auto-compress     do not use archive suffix to determine the
                             compression program
  -z, --gzip, --gunzip, --ungzip   filter the archive through gzip
  -Z, --compress, --uncompress   filter the archive through compress

 Local file selection:

      --add-file=FILE        add given FILE to the archive (useful if its name
                             starts with a dash)
      --backup[=CONTROL]     backup before removal, choose version CONTROL
  -C, --directory=DIR        change to directory DIR
      --exclude=PATTERN      exclude files, given as a PATTERN
      --exclude-backups      exclude backup and lock files
      --exclude-caches       exclude contents of directories containing
                             CACHEDIR.TAG, except for the tag file itself
      --exclude-caches-all   exclude directories containing CACHEDIR.TAG
      --exclude-caches-under exclude everything under directories containing
                             CACHEDIR.TAG
      --exclude-tag=FILE     exclude contents of directories containing FILE,
                             except for FILE itself
      --exclude-tag-all=FILE exclude directories containing FILE
      --exclude-tag-under=FILE   exclude everything under directories
                             containing FILE
      --exclude-vcs          exclude version control system directories
  -h, --dereference          follow symlinks; archive and dump the files they
                             point to
      --hard-dereference     follow hard links; archive and dump the files they
                             refer to
  -K, --starting-file=MEMBER-NAME
                             begin at member MEMBER-NAME when reading the
                             archive
      --newer-mtime=DATE     compare date and time when data changed only
      --no-null              disable the effect of the previous --null option
      --no-recursion         avoid descending automatically in directories
      --no-unquote           do not unquote filenames read with -T
      --null                 -T reads null-terminated names, disable -C
  -N, --newer=DATE-OR-FILE, --after-date=DATE-OR-FILE
                             only store files newer than DATE-OR-FILE
      --one-file-system      stay in local file system when creating archive
  -P, --absolute-names       don't strip leading `/'s from file names
      --recursion            recurse into directories (default)
      --suffix=STRING        backup before removal, override usual suffix ('~'
                             unless overridden by environment variable
                             SIMPLE_BACKUP_SUFFIX)
  -T, --files-from=FILE      get names to extract or create from FILE
      --unquote              unquote filenames read with -T (default)
  -X, --exclude-from=FILE    exclude patterns listed in FILE

 File name transformations:

      --strip-components=NUMBER   strip NUMBER leading components from file
                             names on extraction
      --transform=EXPRESSION, --xform=EXPRESSION
                             use sed replace EXPRESSION to transform file
                             names

 File name matching options (affect both exclude and include patterns):

      --anchored             patterns match file name start
      --ignore-case          ignore case
      --no-anchored          patterns match after any `/' (default for
                             exclusion)
      --no-ignore-case       case sensitive matching (default)
      --no-wildcards         verbatim string matching
      --no-wildcards-match-slash   wildcards do not match `/'
      --wildcards            use wildcards (default)
      --wildcards-match-slash   wildcards match `/' (default for exclusion)

 Informative output:

      --checkpoint[=NUMBER]  display progress messages every NUMBERth record
                             (default 10)
      --checkpoint-action=ACTION   execute ACTION on each checkpoint
      --full-time            print file time to its full resolution
      --index-file=FILE      send verbose output to FILE
  -l, --check-links          print a message if not all links are dumped
      --no-quote-chars=STRING   disable quoting for characters from STRING
      --quote-chars=STRING   additionally quote characters from STRING
      --quoting-style=STYLE  set name quoting style; see below for valid STYLE
                             values
  -R, --block-number         show block number within archive with each message
                            
      --show-defaults        show tar defaults
      --show-omitted-dirs    when listing or extracting, list each directory
                             that does not match search criteria
      --show-transformed-names, --show-stored-names
                             show file or archive names after transformation
      --totals[=SIGNAL]      print total bytes after processing the archive;
                             with an argument - print total bytes when this
                             SIGNAL is delivered; Allowed signals are: SIGHUP,
                             SIGQUIT, SIGINT, SIGUSR1 and SIGUSR2; the names
                             without SIG prefix are also accepted
      --utc                  print file modification times in UTC
  -v, --verbose              verbosely list files processed
      --warning=KEYWORD      warning control
  -w, --interactive, --confirmation
                             ask for confirmation for every action

 Compatibility options:

  -o                         when creating, same as --old-archive; when
                             extracting, same as --no-same-owner

 Other options:

  -?, --help                 give this help list
      --restrict             disable use of some potentially harmful options
      --usage                give a short usage message
      --version              print program version

Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.

The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control may be set with --backup or VERSION_CONTROL, values are:

  none, off       never make backups
  t, numbered     make numbered backups
  nil, existing   numbered if numbered backups exist, simple otherwise
  never, simple   always make simple backups

Valid arguments for the --quoting-style option are:

  literal
  shell
  shell-always
  c
  c-maybe
  escape
  locale
  clocale

*This* tar defaults to:
--format=gnu -f- -b20 --quoting-style=escape --rmt-command=/etc/rmt
--rsh-command=/usr/bin/ssh

Report bugs to <bug-tar@gnu.org>.
[purpleendurer @ bash ~]

1.2.2 银河麒麟(kylin)系统中的tar帮助信息

[purpleendurer @ kylin ~ ] tar --help

用法: tar [选项...] [FILE]...
GNU 'tar' saves many files together into a single tape or disk archive, and can
restore individual files from the archive.

Examples:
  tar -cf archive.tar foo bar  # Create archive.tar from files foo and bar.
  tar -tvf archive.tar         # List all files in archive.tar verbosely.
  tar -xf archive.tar          # Extract all files from archive.tar.

 主操作模式:

  -A, --catenate, --concatenate   追加 tar 文件至归档
  -c, --create               创建一个新归档
  -d, --diff, --compare      找出归档和文件系统的差异
      --delete               从归档(非磁带!)中删除
  -r, --append               追加文件至归档结尾
  -t, --list                 列出归档内容
      --test-label           测试归档卷标并退出
  -u, --update               仅追加比归档中副本更新的文件
  -x, --extract, --get       从归档中解出文件

 操作修饰符:

      --check-device         当创建增量归档时检查设备号(默认)
  -g, --listed-incremental=FILE   处理新式的 GNU 格式的增量备份
  -G, --incremental          处理老式的 GNU 格式的增量备份
      --ignore-failed-read
                             当遇上不可读文件时不要以非零值退出
      --level=NUMBER         所创建的增量列表归档的输出级别
  -n, --seek                 归档可检索
      --no-check-device      当创建增量归档时不要检查设备号
      --no-seek              归档不可检索
      --occurrence[=NUMBER]  仅处理归档中每个文件的第 NUMBER
                             个事件;仅当与以下子命令 --delete,
                             --diff, --extract 或是 --list
                             中的一个联合使用时,此选项才有效。而且不管文件列表是以命令行形式给出或是通过
                             -T 选项指定的;NUMBER 值默认为 1
      --sparse-version=MAJOR[.MINOR]
                             设置所用的离散格式版本(隐含
                             --sparse)
  -S, --sparse               高效处理离散文件

 重写控制:

  -k, --keep-old-files       don't replace existing files when extracting,
                             treat them as errors
      --keep-directory-symlink   preserve existing symlinks to directories when
                             extracting
      --keep-newer-files
                             不要替换比归档中副本更新的已存在的文件
      --no-overwrite-dir     保留已存在目录的元数据
      --one-top-level[=DIR]  create a subdirectory to avoid having loose files
                             extracted
      --overwrite            解压时重写存在的文件
      --overwrite-dir        解压时重写已存在目录的元数据(默认)
                            
      --recursive-unlink     解压目录之前先清除目录层次
      --remove-files         在添加文件至归档后删除它们
      --skip-old-files       don't replace existing files when extracting,
                             silently skip over them
  -U, --unlink-first         在解压要重写的文件之前先删除它们
  -W, --verify               在写入以后尝试校验归档

 选择输出流:

      --ignore-command-error 忽略子进程的退出代码
      --no-ignore-command-error
                             将子进程的非零退出代码认为发生错误
  -O, --to-stdout            解压文件至标准输出
      --to-command=COMMAND
                             将解压的文件通过管道传送至另一个程序

 操作文件属性:

      --atime-preserve[=METHOD]
                             在输出的文件上保留访问时间,要么通过在读取(默认
                             METHOD=‘replace’)后还原时间,要不就不要在第一次(METHOD=‘system’)设置时间
      --clamp-mtime          only set time when the file is more recent than
                             what was given with --mtime
      --delay-directory-restore
                             直到解压结束才设置修改时间和所解目录的权限
      --group=名称         强制将 NAME
                             作为所添加的文件的组所有者
      --mode=CHANGES         强制将所添加的文件(符号)更改为权限
                             CHANGES
      --mtime=DATE-OR-FILE   从 DATE-OR-FILE 中为添加的文件设置
                             mtime
  -m, --touch                不要解压文件的修改时间
      --no-delay-directory-restore
                             取消 --delay-directory-restore 选项的效果
      --no-same-owner
                             将文件解压为您所有(普通用户默认此项)
      --no-same-permissions
                             从归档中解压权限时使用用户的掩码位(默认为普通用户服务)
      --numeric-owner        总是以数字代表用户/组的名称
      --owner=名称         强制将 NAME
                             作为所添加的文件的所有者
  -p, --preserve-permissions, --same-permissions
                             解压文件权限信息(默认只为超级用户服务)
      --preserve             与 -p 和 -s 一样
      --same-owner
                             尝试解压时保持所有者关系一致(超级用户默认此项)
  -s, --preserve-order, --same-order
                             member arguments are listed in the same order as
                             the files in the archive
      --sort=ORDER           directory sorting order: none (default), name or
                             inode

 Handling of extended file attributes:

      --acls                 Enable the POSIX ACLs support
      --no-acls              Disable the POSIX ACLs support
      --no-selinux           Disable the SELinux context support
      --no-xattrs            Disable extended attributes support
      --selinux              Enable the SELinux context support
      --xattrs               Enable extended attributes support
      --xattrs-exclude=MASK  specify the exclude pattern for xattr keys
      --xattrs-include=MASK  specify the include pattern for xattr keys

 设备选择和切换:

  -f, --file=ARCHIVE         使用归档文件或 ARCHIVE 设备
      --force-local
                             即使归档文件存在副本还是把它认为是本地归档
  -F, --info-script=名称, --new-volume-script=名称
                             在每卷磁带最后运行脚本(隐含 -M)
  -L, --tape-length=NUMBER   写入 NUMBER × 1024 字节后更换磁带
  -M, --multi-volume         创建/列出/解压多卷归档文件
      --rmt-command=COMMAND  使用指定的 rmt COMMAND 代替 rmt
      --rsh-command=COMMAND  使用远程 COMMAND 代替 rsh
      --volno-file=FILE      使用/更新 FILE 中的卷数

 设备分块:

  -b, --blocking-factor=BLOCKS   每个记录 BLOCKS x 512 字节
  -B, --read-full-records    读取时重新分块(只对 4.2BSD 管道有效)
  -i, --ignore-zeros         忽略归档中的零字节块(即文件结尾)
      --record-size=NUMBER   每个记录的字节数 NUMBER,乘以 512

 选择归档格式:

  -H, --format=FORMAT        创建指定格式的归档

 FORMAT 是以下格式中的一种:

    gnu                      GNU tar 1.13.x 格式
    oldgnu                   GNU 格式 as per tar <= 1.12
    pax                      POSIX 1003.1-2001 (pax) 格式
    posix                    等同于 pax
    ustar                    POSIX 1003.1-1988 (ustar) 格式
    v7                       old V7 tar 格式

      --old-archive, --portability
                             等同于 --format=v7
      --pax-option=关键字[[:]=值][,关键字[[:]=值]]...
                             控制 pax 关键字
      --posix                等同于 --format=posix
  -V, --label=TEXT           创建带有卷名 TEXT
                             的归档;在列出/解压时,使用 TEXT
                             作为卷名的模式串

 压缩选项:

  -a, --auto-compress        使用归档后缀名来决定压缩程序
  -I, --use-compress-program=PROG
                             通过 PROG 过滤(必须是能接受 -d
                             选项的程序)
  -j, --bzip2                通过 bzip2 过滤归档
  -J, --xz                   通过 xz 过滤归档
      --lzip                 通过 lzip 过滤归档
      --lzma                 通过 xz 过滤归档
      --lzop                 通过 xz 过滤归档
      --no-auto-compress     不使用归档后缀名来决定压缩程序
  -z, --gzip, --gunzip, --ungzip   通过 gzip 过滤归档
  -Z, --compress, --uncompress   通过 compress 过滤归档

 本地文件选择:

      --add-file=FILE        添加指定的 FILE 至归档(如果名字以 -
                             开始会很有用的)
      --backup[=CONTROL]     在删除前备份,选择 CONTROL 版本
  -C, --directory=DIR        改变至目录 DIR
      --exclude=PATTERN      排除以 PATTERN 指定的文件
      --exclude-backups      排除备份和锁文件
      --exclude-caches       除标识文件本身外,排除包含
                             CACHEDIR.TAG 的目录中的内容
      --exclude-caches-all   排除包含 CACHEDIR.TAG 的目录
      --exclude-caches-under 排除包含 CACHEDIR.TAG
                             的目录中所有内容
      --exclude-ignore=FILE  read exclude patterns for each directory from
                             FILE, if it exists
      --exclude-ignore-recursive=FILE
                             read exclude patterns for each directory and its
                             subdirectories from FILE, if it exists
      --exclude-tag=FILE     除 FILE 自身外,排除包含 FILE
                             的目录中的内容
      --exclude-tag-all=FILE 排除包含 FILE 的目录
      --exclude-tag-under=FILE   排除包含 FILE 的目录中的所有内容
      --exclude-vcs          排除版本控制系统目录
      --exclude-vcs-ignores  read exclude patterns from the VCS ignore files
  -h, --dereference
                             跟踪符号链接;将它们所指向的文件归档并输出
      --hard-dereference
                             跟踪硬链接;将它们所指向的文件归档并输出
  -K, --starting-file=MEMBER-NAME
                             begin at member MEMBER-NAME when reading the
                             archive
      --newer-mtime=DATE     当只有数据改变时比较数据和时间
      --no-null              禁用上一次的效果 --null 选项
      --no-recursion         避免目录中的自动降级
      --no-unquote           do not unquote input file or member names
      --null                 -T 读取以空终止的名字,-C 禁用
  -N, --newer=DATE-OR-FILE, --after-date=DATE-OR-FILE
                             只保存比 DATE-OR-FILE 更新的文件
      --one-file-system      创建归档时保存在本地文件系统中
  -P, --absolute-names       don't strip leading '/'s from file names
      --recursion            目录递归(默认)
      --suffix=STRING        在删除前备份,除非被环境变量
                             SIMPLE_BACKUP_SUFFIX
                             覆盖,否则覆盖常用后缀(‘’)
  -T, --files-from=FILE      从 FILE
                             中获取文件名来解压或创建文件
      --unquote              unquote input file or member names (default)
  -X, --exclude-from=FILE    排除 FILE 中列出的模式串

 文件名变换:

      --strip-components=NUMBER   解压时从文件名中清除 NUMBER
                             个引导部分
      --transform=EXPRESSION, --xform=EXPRESSION
                             使用 sed 代替 EXPRESSION
                             来进行文件名变换

 文件名匹配选项(同时影响排除和包括模式串):

      --anchored             模式串匹配文件名头部
      --ignore-case          忽略大小写
      --no-anchored          patterns match after any '/' (default for
                             exclusion)
      --no-ignore-case       匹配大小写(默认)
      --no-wildcards         逐字匹配字符串
      --no-wildcards-match-slash   wildcards do not match '/'
      --wildcards            使用通配符(默认对 exclusion )
      --wildcards-match-slash   wildcards match '/' (default for exclusion)

 提示性输出:

      --checkpoint[=NUMBER]  每隔 NUMBER
                             个记录显示进度信息(默认为 10 个)
      --checkpoint-action=ACTION   在每个检查点上执行 ACTION
      --full-time            按文件原本时间格式打印
      --index-file=FILE      将详细输出发送至 FILE
  -l, --check-links
                             只要不是所有链接都被输出就打印信息
      --no-quote-chars=STRING   禁用来自 STRING 的字符引用
      --quote-chars=STRING   来自 STRING 的额外的引用字符
      --quoting-style=STYLE  设置名称引用风格;有效的 STYLE
                             值请参阅以下说明
  -R, --block-number         每个信息都显示归档内的块数
      --show-defaults        显示 tar 默认选项
      --show-omitted-dirs
                             列表或解压时,列出每个不匹配查找标准的目录
      --show-snapshot-field-ranges
                             show valid ranges for snapshot-file fields
      --show-transformed-names, --show-stored-names
                             显示变换后的文件名或归档名
      --totals[=SIGNAL]      处理归档后打印出总字节数;当此
                             SIGNAL 被触发时带参数 -
                             打印总字节数;允许的信号为:
                             SIGHUP,SIGQUIT,SIGINT,SIGUSR1 和
                             SIGUSR2;同时也接受不带 SIG
                             前缀的信号名称
      --utc                  以 UTC 格式打印文件修改时间
  -v, --verbose              详细地列出处理的文件
      --warning=KEYWORD      警告控制:
  -w, --interactive, --confirmation
                             每次操作都要求确认

 兼容性选项:

  -o                         创建归档时,相当于
                             --old-archive;展开归档时,相当于
                             --no-same-owner

 其它选项:

  -?, --help                 显示此帮助列表
      --restrict             禁用某些潜在的有危险的选项
      --usage                显示简短的用法说明
      --version              打印程序版本

长选项和相应短选项具有相同的强制参数或可选参数。

The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control may be set with --backup or VERSION_CONTROL, values are:

  none, off       never make backups
  t, numbered     make numbered backups
  nil, existing   numbered if numbered backups exist, simple otherwise
  never, simple   always make simple backups

--quoting-style 选项的有效参数为:

  literal
  shell
  shell-always
  c
  c-maybe
  escape
  locale
  clocale

此 tar 默认为:
--format=gnu -f- -b20 --quoting-style=escape --rmt-command=/usr/lib/tar/rmt
--rsh-command=/usr/bin/rsh

将错误报告发送到 <bug-tar@gnu.org>。

1.3 tar 命令格式

tar [选项...] [文件]...

 1.4 tar命令的选项说明 

tar命令的选项很多,分为

1.4.1 主要操作模式选项

Linux shell编程学习笔记76:tar命令——快照 & 备份(上)_shell编程_02

选项

说明

-A, --catenate, --concatenate

将 tar 文件追加到存档

-c, --create

创建新存档

-d, --diff, --compare

查找存档和文件系统之间的差异

--delete

从归档(非磁带!)中删除

-r, --append

追加文件至归档结尾

-t, --list

列出归档内容

--test-label

测试归档卷标并退出

-u, --update

更新副本中的文件(仅追加比归档中副本更新的文件)

-x, --extract, --ge

从归档中提取文件

1.4.2  操作修饰符选项

Linux shell编程学习笔记76:tar命令——快照 & 备份(上)_shell编程_03

选项

说明

--check-device

在创建增量存档时检查设备编号(默认)

-g, --listed-incremental=FILE

处理新的 GNU 格式增量备份

-G, --incremental

处理旧的 GNU 格式增量备份

--ignore-failed-read

当遇上不可读文件时不要以非零值退出

--level=NUMBER

所创建的增量列表归档的输出级别

-n, --seek

归档可检索

--no-check-device

当创建增量归档时不要检查设备号

--no-seek

归档不可检索

--occurrence[=NUMBER]

仅处理归档中每个文件的第 NUMBER个事件;仅当与以下子命令 --delete, --diff, --extract 或是 --list中的一个联合使用时,此选项才有效。而且不管文件列表是以命令行形式给出或是通过-T 选项指定的;NUMBER 值默认为 1

--sparse-version=MAJOR[.MINOR]

设置所用的离散格式版本(隐含--sparse)

-S, --sparse

高效处理离散文件

 1.4.3 重写控制选项

Linux shell编程学习笔记76:tar命令——快照 & 备份(上)_学习笔记_04

选项

说明

-k, --keep-old-files

在解压时不替换已有文件,视为错误

--keep-directory-symlink

在提取时保留指向目录的现有符号链接

--keep-newer-files

不会替换比其存档副本更新的现有文件

--no-overwrite-dir

保留现有目录的元数据

--overwrite

在提取时覆盖现有文件

--overwrite-dir

在提取时覆盖现有目录的元数据(默认)

--recursive-unlink

空层次结构

--remove-files

将文件添加到存档后删除文件

--skip-old-files

在解压时不替换现有文件,静默跳过它们

-U, --unlink-first

在解压要重写的文件之前先删除它们

-W, --verify

尝试在写入存档后对其进行验证

  1.4.4 选择输出流选项

Linux shell编程学习笔记76:tar命令——快照 & 备份(上)_脚本编程_05

选项

说明

--ignore-command-error

忽略子进程的退出代码

--no-ignore-command-error

将子进程的非零退出代码视为错误

-O, --to-stdout

将文件提取到标准输出

--to-command=COMMAND

将提取的文件通过管道传送至另一个程序

 1.4.5  文件属性操作选项

Linux shell编程学习笔记76:tar命令——快照 & 备份(上)_linux_06

选项

说明

--atime-preserve[=METHOD]

在输出的文件上保留访问时间,要么通过在读取(默认METHOD=‘replace’)后还原时间,要不就不要在第一次(METHOD=‘system’)设置时间

--clamp-mtime

仅在文件比 --mtime 给出的时间更新时才设置时间

--delay-directory-restore

直到提取结束才设置修改时间和所解目录的权限

--group=NAME

强制 NAME 作为已添加文件的组所有者

--mode=CHANGES

强制将所添加的文件(符号)权限更改为 CHANGES

--mtime=DATE-OR-FILE

按照 DATE-OR-FILE来设置添加文件的修改时间(mtime)

-m, --touch

不提取修改了时间的文件

--no-delay-directory-restore

取消--delay-directory-restore 选项的效果

--no-same-owner

让您自己成为提取文件的所有者(普通用户默认)

--no-same-permissions

在从存档中提取权限时应用用户的 掩码(umask)(普通用户默认)

--numeric-owner

始终使用数字作为用户/组名称

--owner=NAME

强制 NAME 作为已添加文件的所有者

-p, --preserve-permissions, --same-permissions

提取有关文件权限的信息(超级用户的默认值)

--preserve

与 -p 和 -s 相同

--same-owner

尝试提取与存档中相同的所有权的文件(超级用户默认)

-s、--preserve-order、--same-order

成员参数的列出顺序与存档中的文件相同

 

1.4.6 扩展文件属性的处理选项

Linux shell编程学习笔记76:tar命令——快照 & 备份(上)_hive_07

选项

说明

--acls

启用 POSIX ACL 支持

--no-acls

禁用 POSIX ACL 支持

--no-selinux

停用 SELinux 上下文支持

--no-xattrs

禁用扩展属性支持

--selinux

启用 SELinux 上下文支持

--xattrs

启用扩展属性支持

--xattrs-exclude=MASK

指定 xattr 键的排除模式

--xattrs-include=MASK

指定 xattr 键的包含模式

 1.4.7 设备选择和切换选项

Linux shell编程学习笔记76:tar命令——快照 & 备份(上)_hive_08

选项

说明

-f, --file=ARCHIVE

使用存档文件或设备 ARCHIVE

--force-local

即使归档文件存在副本还是把它认为是本地归档

-F、--info-script=NAME、--new-volume-script=NAME

在每个磁带的末尾运行脚本(隐含 -M)

-L, --tape-length=NUMBER

写入 NUMBER x 1024 字节后更改磁带

-M, --multi-volume

创建/列出/解压多卷归档文件

--rmt-command=COMMAND

使用给定的 rmt COMMAND 代替 rmt

--rsh-command=COMMAND

使用 remote COMMAND 代替 rsh

--volno-file=FILE

使用/更新 FILE 中的卷号

1.4.8  设备分块选项

Linux shell编程学习笔记76:tar命令——快照 & 备份(上)_脚本编程_09

选项

说明

-b, --blocking-factor=BLOCKS

每条记录BLOCKS x 512 字节

-B, --read-full-records

读取时重新分块(只对 4.2BSD 管道有效)

-i, --ignore-zeros

忽略存档中的零块(表示 EOF)

--record-size=NUMBER

每条记录字节数NUMBER,应为512 的倍数

1.4.9  选择归档格式选项

Linux shell编程学习笔记76:tar命令——快照 & 备份(上)_linux_10

Linux shell编程学习笔记76:tar命令——快照 & 备份(上)_脚本编程_11

 

选项

说明

-H, --format=FORMAT

创建指定格式的归档

FORMAT 是以下格式中的一种:

  1.  gnu:GNU tar 1.13.x 格式
  2.  oldgnu:GNU 格式 as per tar <= 1.12
  3.  pax:POSIX 1003.1-2001 (pax) 格式
  4.  posix:等同于 pax
  5.  ustar:POSIX 1003.1-1988 (ustar) 格式
  6.  v7:old V7 tar 格式

--old-archive, --portability

等同于 --format=v7

--pax-option=关键字[[:]=值][,关键字[[:]=值]]...

控制 pax 关键字

--posix

等同于 --format=posix

-V, --label=TEXT

创建带有卷名 TEXT的归档;

在列出/解压时,使用 TEXT作为卷名的模式串

 1.4.a 压缩选项

Linux shell编程学习笔记76:tar命令——快照 & 备份(上)_hive_12

选项

说明

-a, --auto-compress

使用归档后缀名来决定压缩程序

-I, --use-compress-program=PROG

通过 PROG 过滤(必须是能接受 -d选项的程序)

-j, --bzip2

通过 bzip2 过滤归档

-J, --xz

通过 xz 过滤归档

--lzip

通过 lzip 过滤归档

--lzma

通过 xz 过滤归档

--lzop

通过 xz 过滤归档

--no-auto-compress

不使用归档后缀名来决定压缩程序

-z, --gzip, --gunzip, --ungzip

通过 gzip 过滤归档

-Z, --compress, --uncompress

通过 compress 过滤归档

1.4.b  本地文件选择选项 

Linux shell编程学习笔记76:tar命令——快照 & 备份(上)_学习笔记_13

选项

说明

--add-file=FILE

添加指定的 FILE 至归档(如果名字以 - 开始会很有用的)

--backup[=CONTROL]

在删除前备份,选择 CONTROL 版本

CONTROL的有效值:

  1. none, off        不做备份
  2. t, numbered    生成编号备份
  3. nil, existing    若存在编号,则编号备份;否则,简单备份
  4. never, simple    始终生成简单备份

                                                           

-C, --directory=DIR

转至目录 DIR

--exclude=PATTERN

排除以 PATTERN 指定的文件

--exclude-backups

排除备份并锁定文件

--exclude-caches

除标识文件本身外,排除包含CACHEDIR.TAG 的目录中的内容

--exclude-caches-all

排除包含 CACHEDIR.TAG 的目录

--exclude-caches-under

排除包含 CACHEDIR.TAG的目录中所有内容

--exclude-ignore=FILE

从 FILE 中读取每个目录的排除模式(如果存在)

--exclude-ignore-recursive=FILE

从 FILE 中读取每个目录及其子目录的排除模式(如果存在)

--exclude-tag=FILE

除 FILE 自身外,排除包含 FILE的目录中的内容

--exclude-tag-all=FILE

排除包含 FILE 的目录

--exclude-tag-under=FILE

排除包含 FILE 的目录中的所有内容

--exclude-vcs

排除版本控制系统目录

--exclude-vcs-ignores

从 VCS 忽略文件中读取排除模式

-h, --dereference

跟踪符号链接;将它们所指向的文件归档并输出

--hard-dereference

跟踪硬链接;将它们所指向的文件归档并输出

-K, --starting-file=MEMBER-NAME

读取存档时从 MEMBER-NAME 开始

--newer-mtime=DATE

当只有数据改变时比较数据和时间

--no-null

禁用上一次 --null 选项的效果

--no-recursion

避免目录中的自动降级

--no-unquote

不取消引用使用 -T 读取的文件名

--null

-T 读取以空终止的名字,-C 禁用

-N, --newer=DATE-OR-FILE, --after-date=DATE-OR-FILE

只保存比 DATE-OR-FILE 更新的文件

--one-file-system

创建归档时保存在本地文件系统中

-P, --absolute-names

不会从文件名中去除前导 '/'

--recursion

目录递归(默认)

--suffix=STRING

在删除前备份,除非被环境变量SIMPLE_BACKUP_SUFFIX覆盖,否则覆盖常用后缀(‘’)

--suffix=STRING

备份,覆盖通常的后缀('~' 除非被环境变量 SIMPLE_BACKUP_SUFFIX 覆盖)

-T, --files-from=FILE

从 FILE中获取文件名来解压或创建文件

--unquote

使用 -T 读取的文件名取消引号(默认)

-X, --exclude-from=FILE

排除 FILE 中列出的模式串

1.4.c   文件名变换选项

Linux shell编程学习笔记76:tar命令——快照 & 备份(上)_linux_14

 

选项

说明

--strip-components=NUMBER

解压时从文件名中清除 NUMBER个引导部分

--transform=EXPRESSION, --xform=EXPRESSION

使用 sed 代替 EXPRESSION 来进行文件名变换

1.4.d  文件名匹配选项(同时影响排除和包括模式串)

Linux shell编程学习笔记76:tar命令——快照 & 备份(上)_脚本编程_15

 

选项

说明

--anchored

模式串匹配文件名头部

--ignore-case

忽略大小写

--no-anchored

模式在任何 '/' 之后匹配(默认排除)

--no-ignore-case

匹配大小写(默认)

--no-wildcards

逐字匹配字符串

--no-wildcards-match-slash

wildcards do not match '/'

--no-wildcards-match-slash

通配符与 '/' 不匹配

--wildcards

使用通配符(默认)

--wildcards-match-slash

通配符匹配 '/' (默认排除)

1.4.e  信息输出选项

Linux shell编程学习笔记76:tar命令——快照 & 备份(上)_hive_16

选项

说明

--checkpoint[=NUMBER]

每隔 NUMBER个记录显示进度信息(默认为 10 个)

--checkpoint-action=ACTION

在每个检查点上执行 ACTION

--full-time

按文件原本时间格式打印

--index-file=FILE

将详细输出发送至 FILE

-l, --check-links

只要不是所有链接都被输出就打印信息

--no-quote-chars=STRING

禁用来自 STRING 的字符引用

--quote-chars=STRING

附加来自 STRING 的引号字符

--quoting-style=STYLE

设置名称引用风格;有效的 STYLE值请参阅以下说明

STYLE有效值为:

  1. literal
  2. shell
  3. shell-always
  4. c
  5. c-maybe
  6. escape
  7. locale
  8. clocale

-R, --block-number

每个信息都显示归档内的块数

--show-defaults

显示 tar 默认选项

--show-omitted-dirs

列表或解压时,列出每个不匹配查找标准的目录

--show-snapshot-field-ranges

显示快照文件字段的有效范围

--show-transformed-names, --show-stored-names

显示变换后的文件名或归档名

--totals[=SIGNAL]

处理归档后打印出总字节数;当此SIGNAL 被触发时带参数 -打印总字节数;允许的信号为:SIGHUP,SIGQUIT,SIGINT,SIGUSR1 和SIGUSR2;同时也接受不带 SIG前缀的信号名称

--utc

以 UTC 格式打印文件修改时间

-v, --verbose

详细地列出处理的文件

--warning=KEYWORD

警告控制

-w, --interactive, --confirmation

每次操作都要求确认

 1.4.f  兼容性选项

Linux shell编程学习笔记76:tar命令——快照 & 备份(上)_shell编程_17

选项

说明

-o

创建归档时,相当于--old-archive

展开归档时,相当于--no-same-owner

1.4.g   其它选项

Linux shell编程学习笔记76:tar命令——快照 & 备份(上)_shell编程_18

选项

说明

-?, --help

显示此帮助列表

--restrict

禁用某些潜在的有危险的选项

--usage

显示简短的用法说明

--version

打印程序版本

 1.4.h  相关说明

Linux shell编程学习笔记76:tar命令——快照 & 备份(上)_hive_19

长选项和相应短选项具有相同的强制参数或可选参数。

备份后缀为 '~',除非使用 --suffix 或 SIMPLE_BACKUP_SUFFIX 设置。

可以使用 --backup 或 VERSION_CONTROL 设置版本控制,值为:

  1. none, off        不做备份
  2. t, numbered    生成编号备份
  3. nil, existing    若存在编号,则编号备份;否则,简单备份
  4. never, simple    始终生成简单备份

 

--quoting-style 选项的有效参数为:

  1.   literal
  2.   shell
  3.   shell-always
  4.   c
  5.   c-maybe
  6.   escape
  7.   locale
  8.   clocale

此 tar 默认为:
--format=gnu -f- -b20 --quoting-style=escape --rmt-command=/usr/lib/tar/rmt
--rsh-command=/usr/bin/rsh

将错误报告发送到 <bug-tar@gnu.org>。