Ubuntu 22.04 tar-gzip-unzip 安装与命令参数详解

在 Ubuntu 22.04 上从零安装 tar 1.34、gzip 1.10、UnZip 6.00 与 zip 3.0(在线 apt 与离线 deb 两种方式),并逐条实测三个工具全部常用命令参数与真实输出,附版本差异与命令速查表

Linux 文件管理与传输tar-gzip-unzipUbuntu 22.042026/10/07

Ubuntu 22.04 tar-gzip-unzip 安装与命令参数详解

目标:说明 tar、gzip、unzip 三大打包压缩工具在 Ubuntu 22.04(amd64 / x86_64)上的完整安装(在线 apt 与离线 deb 两种方式)、全部常用命令行参数与扩展选项的逐条实测结果,以及 GNU tar 1.34、gzip 1.10、UnZip 6.00 与旧版(tar 1.30、gzip 1.9)的真实行为差异,已在真实环境中逐条实测通过。

一、环境信息

  • 操作系统:Ubuntu 22.04.5 LTS(amd64 / x86_64,PRETTY_NAME="Ubuntu 22.04.5 LTS")
  • 工具版本:
    • tar:GNU tar 1.34(Debian 包版本 1.34+dfsg-1ubuntu0.1.22.04.6,实测 tar --version 输出 tar (GNU tar) 1.34)
    • gzip:gzip 1.10(Debian 包版本 1.10-4ubuntu4.2,实测 gzip --version 输出 gzip 1.10)
    • unzip:UnZip 6.00 of 20 April 2009(Debian 再打包,包版本 6.0-26ubuntu3.2)
    • zip:Zip 3.0(包版本 3.0-12build2,默认未安装,本文档实测时已在线安装)
  • 安装来源仓库:Ubuntu 官方仓库 archive.ubuntu.com(tar / gzip / unzip / zip 位于 jammy/main 与 jammy-updates)
  • 说明:实测中为补全 tar -J(xz 压缩)与 tar -j(bzip2 压缩)功能,另行安装了 xz-utils 与 bzip2(Ubuntu 22.04 精简环境默认均未内置);所有命令以 root 用户在本机终端执行。

二、工具概览

tar、gzip、unzip 是 Linux 系统最基础、最常用的三个打包压缩 / 解压缩工具,相互配合覆盖了绝大多数归档与压缩场景:

  • tar(GNU tar):Tape Archive 的缩写,本身只负责"打包归档"——把多个文件与目录聚合成单个归档文件,不压缩。归档后缀通常为 .tar。与压缩工具配合(-z gzip、-j bzip2、-J xz)可生成 .tar.gz、.tar.bz2、.tar.xz 等压缩归档,-a 还可按后缀名自动选择压缩程序;
  • gzip(GNU gzip):基于 DEFLATE 算法的单文件压缩器,只能压缩单个文件,压缩后生成 .gz 后缀文件(默认删除原文件,-k 可保留)。配套命令 gunzip 负责解压,zcat 直接查看压缩文件内容;
  • unzip(Info-ZIP):.zip 格式的解压器,配合 zip(Info-ZIP zip)创建 zip 归档。zip 格式自带目录结构与压缩,适合跨平台(Windows / macOS / Linux)分发。

三者典型组合:tar czf backup.tar.gz /data(打包并压缩)、tar xzf backup.tar.gz(解压)、unzip dist.zip(解压 zip)。本文档下文所有命令均以 root 用户在本机终端执行,所有回显均为逐字实测。

三、在线安装

3.1 查看当前包状态

命令:dpkg -l tar gzip unzip zip 查看四个包是否已安装及精确版本(实测 zip 默认未安装,状态为 un)。

dpkg -l tar gzip unzip zip
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name           Version                      Architecture Description
+++-==============-============================-============-========================================
ii  gzip           1.10-4ubuntu4.2              amd64        GNU compression utilities
ii  tar            1.34+dfsg-1ubuntu0.1.22.04.6 amd64        GNU version of the tar archiving utility
ii  unzip          6.0-26ubuntu3.2              amd64        De-archiver for .zip files
un  zip            <none>                       <none>       (no description available)

说明:ii 表示已安装,un 表示未安装(zip);各包版本号末尾的 ubuntu0.1.22.04.6 / 4.2 / 3.2 为 Ubuntu 安全更新(jammy-updates)的再打包后缀。

3.2 在线安装 / 重装

命令:apt-get install 安装;已安装时用 --reinstall 强制重装。先看 zip 首次安装的真实回显:

apt-get install -y zip
Reading package lists...
Building dependency tree...
Reading state information...
The following NEW packages will be installed:
  zip
0 upgraded, 1 newly installed, 0 to remove and 10 not upgraded.
Need to get 176 kB of archives.
After this operation, 544 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu jammy/main amd64 zip amd64 3.0-12build2 [176 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 176 kB in 2s (108 kB/s)
Selecting previously unselected package zip.
(Reading database ... 10598 files and directories currently installed.)
Preparing to unpack .../zip_3.0-12build2_amd64.deb ...
Unpacking zip (3.0-12build2) ...
Setting up zip (3.0-12build2) ...

再抓四包统一重装的回显:

apt-get install -y --reinstall tar gzip unzip zip
Reading package lists...
Building dependency tree...
Reading state information...
0 upgraded, 0 newly installed, 4 reinstalled, 0 to remove and 10 not upgraded.
Need to get 745 kB of archives.
After this operation, 0 B of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 gzip amd64 1.10-4ubuntu4.2 [96.1 kB]
Get:2 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 tar amd64 1.34+dfsg-1ubuntu0.1.22.04.6 [298 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 unzip amd64 6.0-26ubuntu3.2 [175 kB]
Get:4 http://archive.ubuntu.com/ubuntu jammy/main amd64 zip amd64 3.0-12build2 [176 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 745 kB in 5s (142 kB/s)
(Reading database ... 10612 files and directories currently installed.)
Preparing to unpack .../gzip_1.10-4ubuntu4.2_amd64.deb ...
Unpacking gzip (1.10-4ubuntu4.2) over (1.10-4ubuntu4.2) ...
Setting up gzip (1.10-4ubuntu4.2) ...
(Reading database ... 10612 files and directories currently installed.)
Preparing to unpack .../tar_1.34+dfsg-1ubuntu0.1.22.04.6_amd64.deb ...
Unpacking tar (1.34+dfsg-1ubuntu0.1.22.04.6) over (1.34+dfsg-1ubuntu0.1.22.04.6) ...
Setting up tar (1.34+dfsg-1ubuntu0.1.22.04.6) ...
(Reading database ... 10612 files and directories currently installed.)
Preparing to unpack .../unzip_6.0-26ubuntu3.2_amd64.deb ...
Unpacking unzip (6.0-26ubuntu3.2) over (6.0-26ubuntu3.2) ...
Preparing to unpack .../zip_3.0-12build2_amd64.deb ...
Unpacking zip (3.0-12build2) over (3.0-12build2) ...
Setting up unzip (6.0-26ubuntu3.2) ...
Setting up zip (3.0-12build2) ...

说明:0 upgraded, 0 newly installed, 4 reinstalled 表示四包全部强制重装;Unpacking ... over (同版本) ... 即覆盖式重装。debconf: delaying package configuration, since apt-utils is not installed 是本机未安装 apt-utils 时的正常提示,不影响安装结果。

3.3 在线安装后的版本验证

tar --version | head -1
gzip --version | head -1
unzip -v | head -1
dpkg -l tar gzip unzip zip | tail -4
tar (GNU tar) 1.34
gzip 1.10
UnZip 6.00 of 20 April 2009, by Debian. Original by Info-ZIP.
ii  gzip           1.10-4ubuntu4.2              amd64        GNU compression utilities
ii  tar            1.34+dfsg-1ubuntu0.1.22.04.6 amd64        GNU version of the tar archiving utility
ii  unzip          6.0-26ubuntu3.2              amd64        De-archiver for .zip files
ii  zip            3.0-12build2                 amd64        Archiver for .zip files

四、离线安装

离线场景(目标机无法联网)下,可在可联网机器上先用 apt-get install --download-only 把 deb 包下载到指定缓存目录,再拷贝到目标机用 dpkg -i 安装。tar / gzip / unzip / zip 依赖极少,主包即可独立安装。

4.1 下载 deb 包到指定目录

命令:apt-get install -y --download-only -o Dir::Cache::archives=/root/off-tar tar gzip unzip zip 只下载不安装。注意:当包已是最新且已安装时,apt 判定 0 upgraded 而不会下载任何文件(实测首次执行目录内仅生成 lock 与 partial 占位)。需配合 --reinstall 强制按重装流程拉包:

mkdir -p /root/off-tar
apt-get install -y --download-only --reinstall -o Dir::Cache::archives=/root/off-tar tar gzip unzip zip
Reading package lists...
Building dependency tree...
Reading state information...
0 upgraded, 0 newly installed, 4 reinstalled, 0 to remove and 10 not upgraded.
Need to get 745 kB of archives.
After this operation, 0 B of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 gzip amd64 1.10-4ubuntu4.2 [96.1 kB]
Get:2 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 tar amd64 1.34+dfsg-1ubuntu0.1.22.04.6 [298 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 unzip amd64 6.0-26ubuntu3.2 [175 kB]
Get:4 http://archive.ubuntu.com/ubuntu jammy/main amd64 zip amd64 3.0-12build2 [176 kB]
Fetched 745 kB in 3s (237 kB/s)
Download complete and in download only mode
W: Download is performed unsandboxed as root as file '/root/off-tar/partial/gzip_1.10-4ubuntu4.2_amd64.deb' couldn't be accessed by user '_apt'. - pkgAcquire::Run (13: Permission denied)

说明:Download complete and in download only mode 表示只下载未安装;末尾 W: 提示为 root 下载时 _apt 用户无法访问缓存目录的权限告警,不影响下载结果。

4.2 离线包清单

命令:ls -l /root/off-tar/*.deb 查看下载目录,确认四个 deb 已就位。

ls -l /root/off-tar/*.deb
-rw-r--r-- 1 root root  96114 Jul  6 12:59 /root/off-tar/gzip_1.10-4ubuntu4.2_amd64.deb
-rw-r--r-- 1 root root 298302 Jul 22 15:00 /root/off-tar/tar_1.34+dfsg-1ubuntu0.1.22.04.6_amd64.deb
-rw-r--r-- 1 root root 174540 Feb  7  2024 /root/off-tar/unzip_6.0-26ubuntu3.2_amd64.deb
-rw-r--r-- 1 root root 175730 Mar 25  2022 /root/off-tar/zip_3.0-12build2_amd64.deb

4.3 离线安装

命令:dpkg -i 直接安装本地 deb 包(单个包逐个执行,真实回显如下)。

cd /root/off-tar && dpkg -i tar_1.34+dfsg-1ubuntu0.1.22.04.6_amd64.deb
(Reading database ... 10612 files and directories currently installed.)
Preparing to unpack tar_1.34+dfsg-1ubuntu0.1.22.04.6_amd64.deb ...
Unpacking tar (1.34+dfsg-1ubuntu0.1.22.04.6) over (1.34+dfsg-1ubuntu0.1.22.04.6) ...
Setting up tar (1.34+dfsg-1ubuntu0.1.22.04.6) ...
dpkg -i gzip_1.10-4ubuntu4.2_amd64.deb
(Reading database ... 10612 files and directories currently installed.)
Preparing to unpack gzip_1.10-4ubuntu4.2_amd64.deb ...
Unpacking gzip (1.10-4ubuntu4.2) over (1.10-4ubuntu4.2) ...
Setting up gzip (1.10-4ubuntu4.2) ...
dpkg -i unzip_6.0-26ubuntu3.2_amd64.deb
(Reading database ... 10612 files and directories currently installed.)
Preparing to unpack unzip_6.0-26ubuntu3.2_amd64.deb ...
Unpacking unzip (6.0-26ubuntu3.2) over (6.0-26ubuntu3.2) ...
Setting up unzip (6.0-26ubuntu3.2) ...
dpkg -i zip_3.0-12build2_amd64.deb
(Reading database ... 10612 files and directories currently installed.)
Preparing to unpack zip_3.0-12build2_amd64.deb ...
Unpacking zip (3.0-12build2) over (3.0-12build2) ...
Setting up zip (3.0-12build2) ...

说明:Preparing to unpack → Unpacking ... over (同版本) ... → Setting up ... 三步完成覆盖式重装。

4.4 离线安装后的版本验证

tar --version | head -1
gzip --version | head -1
unzip -v | head -1
dpkg -l tar gzip unzip zip | tail -4
tar (GNU tar) 1.34
gzip 1.10
UnZip 6.00 of 20 April 2009, by Debian. Original by Info-ZIP.
ii  gzip           1.10-4ubuntu4.2              amd64        GNU compression utilities
ii  tar            1.34+dfsg-1ubuntu0.1.22.04.6 amd64        GNU version of the tar archiving utility
ii  unzip          6.0-26ubuntu3.2              amd64        De-archiver for .zip files
ii  zip            3.0-12build2                 amd64        Archiver for .zip files

五、全部命令参数实测

5.1 tar(GNU tar 1.34)

5.1.1 tar --help 全文

作用:打印完整帮助,列出 1.34 支持的全部选项(与 1.30 的差异见第六章对照表,如新增 --zstd)。

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
      --delete               delete from the archive (not on mag tapes!)
  -d, --diff, --compare      find differences between archive and file system
  -r, --append               append files to the end of an archive
      --test-label           test the archive volume label and exit
  -t, --list                 list the contents of an archive
  -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
      --hole-detection=TYPE  technique to detect holes
      --ignore-failed-read   do not exit with nonzero on unreadable files
      --level=NUMBER         dump level for created listed-incremental archive
      --no-check-device      do not check device numbers when creating
                             incremental archives
      --no-seek              archive is not seekable
  -n, --seek                 archive is 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

 Local file name selection:
      --add-file=FILE        add given FILE to the archive (useful if its name
                             starts with a dash)
  -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-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     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
      --exclude-vcs-ignores  read exclude patterns from the VCS ignore files
      --no-null              disable the effect of the previous --null option
      --no-recursion         avoid descending automatically in directories
      --no-unquote           do not unquote input file or member names
      --no-verbatim-files-from   -T treats file names starting with dash as
                             options (default)
      --null                 -T reads null-terminated names; implies
                             --verbatim-files-from
      --recursion            recurse into directories (default)
  -T, --files-from=FILE      get names to extract or create from FILE
      --unquote              unquote input file or member names (default)
      --verbatim-files-from  -T reads file names verbatim (no escape or option
                             handling)
  -X, --exclude-from=FILE    exclude patterns listed in FILE

 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 for exclusion)
      --wildcards-match-slash   wildcards match '/' (default for exclusion)

 Overwrite control:

      --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
  -k, --keep-old-files       don't replace existing files when extracting,
                             treat them as errors
      --no-overwrite-dir     preserve metadata of existing directories
      --one-top-level[=DIR]  create a subdirectory to avoid having loose files
                             extracted
      --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')
      --clamp-mtime          only set time when the file is more recent than
                             what was given with --mtime
      --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
      --group-map=FILE       use FILE to map file owner GIDs and names
      --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
      --owner-map=FILE       use FILE to map file owner UIDs and names
  -p, --preserve-permissions, --same-permissions
                             extract information about file permissions
                             (default for superuser)
      --same-owner           try extracting files with the same ownership as
                             exists in the archive (default for superuser)
      --sort=ORDER           directory sorting order: none (default), name or
                             inode
  -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:

      --force-local          archive file is local even if it has a colon
  -f, --file=ARCHIVE         use archive file or device ARCHIVE
  -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 xz
      --lzop                 filter the archive through lzop
      --no-auto-compress     do not use archive suffix to determine the
                             compression program
      --zstd                 filter the archive through zstd
  -z, --gzip, --gunzip, --ungzip   filter the archive through gzip
  -Z, --compress, --uncompress   filter the archive through compress

 Local file selection:

      --backup[=CONTROL]     backup before removal, choose version CONTROL
      --hard-dereference     follow hard links; archive and dump the files they
                             refer to
  -h, --dereference          follow symlinks; archive and dump the files they
                             point 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
  -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
      --suffix=STRING        backup before removal, override usual suffix ('~'
                             unless overridden by environment variable
                             SIMPLE_BACKUP_SUFFIX)

 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

 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-snapshot-field-ranges
                             show valid ranges for snapshot-file fields
      --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
  shell-escape
  shell-escape-always
  c
  c-maybe
  escape
  locale
  clocale

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

说明:与 tar 1.30 的帮助相比,1.34 的差异见第六章对照表:新增 --zstd(zstd 压缩)、--wildcards 帮助文案改为 use wildcards (default for exclusion)、默认 rmt / rsh 命令分别为 /usr/sbin/rmt 与 /usr/bin/rsh、帮助小节顺序略有不同。

5.1.2 -c / -x / -t / -v / -f:创建、解包、列表、详细、归档文件

作用:-c 创建归档,-x 解包,-t 列出内容,-v 详细输出,-f ARCHIVE 指定归档文件名。实测前先建测试目录 /root/t-tar(含 file1.txt、file2.txt、1M 随机文件 random.bin、子目录 subdir/file3.txt):

mkdir -p /root/t-tar/subdir && cd /root/t-tar && tar czf /root/test.tar.gz .
ls -l /root/test.tar.gz
-rw-r--r-- 1 root root 1049214 Oct  6 15:37 /root/test.tar.gz
tar tzf /root/test.tar.gz
./
./file1.txt
./file2.txt
./random.bin
./subdir/
./subdir/file3.txt
tar tvzf /root/test.tar.gz
drwxr-xr-x root/root         0 2026-10-06 15:37 ./
-rw-r--r-- root/root        10 2026-10-06 15:37 ./file1.txt
-rw-r--r-- root/root        12 2026-10-06 15:37 ./file2.txt
-rw-r--r-- root/root   1048576 2026-10-06 15:37 ./random.bin
drwxr-xr-x root/root         0 2026-10-06 15:37 ./subdir/
-rw-r--r-- root/root        10 2026-10-06 15:37 ./subdir/file3.txt
mkdir -p /root/t-tar-x && cd /root/t-tar-x && tar xzf /root/test.tar.gz
find . -type f | sort
./file1.txt
./file2.txt
./random.bin
./subdir/file3.txt

说明:-t / -x 均可直接作用于 .tar.gz;tar tvzf 每行依次为类型权限 / 属主:属组 / 大小 / 修改时间 / 成员名。归档内成员名带 ./ 前缀。

5.1.3 -z / -j / -J:gzip / bzip2 / xz 三种压缩算法对比

作用:-z 通过 gzip 过滤,-j 通过 bzip2 过滤,-J 通过 xz 过滤。同一目录三种压缩的实测体积对比:

cd /root/t-tar && tar czf /root/test.tar.gz . && tar cjf /root/t-bz2.tar.bz2 . && tar cJf /root/t-xz.tar.xz .
ls -l /root/test.tar.gz /root/t-bz2.tar.bz2 /root/t-xz.tar.xz
-rw-r--r-- 1 root root 1049214 Oct  6 15:38 /root/test.tar.gz
-rw-r--r-- 1 root root 1054752 Oct  6 15:38 /root/t-bz2.tar.bz2
-rw-r--r-- 1 root root 1066224 Oct  6 15:38 /root/t-xz.tar.xz

说明:测试数据含 1M 随机数据(几乎不可压缩),三种算法体积接近:gzip 1049214 < bzip2 1054752 < xz 1066224。注意 Ubuntu 22.04 精简环境默认未安装 bzip2 与 xz,直接执行会报错(真实回显见第六章 6.2 / 6.3),本机执行前已 apt-get install -y bzip2 xz-utils。

5.1.4 --exclude:打包时排除文件

作用:按 PATTERN 排除不需要打包的文件 / 目录。

cd /root/t-tar && tar czf /root/t-ex.tar.gz --exclude='*.bin' --exclude='subdir' .
tar tzf /root/t-ex.tar.gz
./
./file1.txt
./file2.txt

说明:排除 random.bin(*.bin)与整个 subdir 后,归档仅剩两个文本文件。

5.1.5 -C:打包 / 解包时切换目录

作用:打包时先 cd 到指定目录再归档;解包时先切到目标目录再释放。

cd /root && tar czf /root/t-C.tar.gz -C /root/t-tar file1.txt subdir/file3.txt
tar tzf /root/t-C.tar.gz
file1.txt
subdir/file3.txt
mkdir -p /root/t-cx && tar xzf /root/test.tar.gz -C /root/t-cx ./subdir/file3.txt
find /root/t-cx -type f | sort
/root/t-cx/subdir/file3.txt

说明:打包配合 -C 后成员名为相对名(无 ./ 前缀);解包时 -C 决定落盘目录。注意:归档内成员名为 ./subdir/file3.txt 时命令行必须一致才能精确匹配(写 subdir/file3.txt 报 Not found in archive,见 6.7)。

5.1.6 -p:解包时保留权限

作用:-p 解包时按归档记录恢复权限(root 默认保留,普通用户需显式加 -p)。

chmod 640 /root/t-tar/file1.txt && tar cf /root/t-p.tar -C /root/t-tar file1.txt
mkdir -p /root/t-px && tar xf /root/t-p.tar -C /root/t-px
ls -l /root/t-px/file1.txt
-rw-r----- 1 root root 10 Oct  6 15:37 /root/t-px/file1.txt

说明:-rw-r-----(640)被完整恢复。

5.1.7 -P:保留绝对路径

作用:-P 打包时保留成员名的前导 /(默认剥离)。

tar czf /root/t-P.tar.gz -P /root/t-tar/file1.txt
tar tzf /root/t-P.tar.gz
/root/t-tar/file1.txt
tar: Removing leading `/' from member names

说明:归档内完整保留 /root/t-tar/file1.txt;Removing leading '/' 提示在 list(-t)阶段打印(创建阶段无提示),与 CentOS 8 的 tar 1.30 行为一致。

5.1.8 --strip-components:解包时剥离路径层级

作用:解包时剥掉成员名前 N 个路径分量。

mkdir -p /root/t-s1 && tar xzf /root/test.tar.gz --strip-components=1 -C /root/t-s1
find /root/t-s1 -type f | sort
/root/t-s1/file1.txt
/root/t-s1/file2.txt
/root/t-s1/random.bin
/root/t-s1/subdir/file3.txt

说明:剥离 1 层后 ./ 消失,文件直接落到 /root/t-s1。

5.1.9 -k / --keep-old-files:解包不覆盖已存在文件

作用:解包时目标文件已存在则不覆盖并视为错误(exit 2)。

cd /root/t-kx && tar xzf /root/test.tar.gz ./file1.txt && echo MODIFIED > file1.txt
tar xkzf /root/test.tar.gz ./file1.txt
tar: ./file1.txt: Cannot open: File exists
tar: Exiting with failure status due to previous errors
echo "exit=$?"; cat file1.txt
exit=2
MODIFIED

说明:-k 与 --keep-old-files 等价;文件保持 MODIFIED 不变,退出码 2。

5.1.10 -u:只追加更新的文件

作用:-u 归档中不存在或比归档副本更新的文件才会被追加。

tar cf /root/t-u.tar -C /root/t-tar file1.txt && cd /root/t-tar
tar uf /root/t-u.tar file2.txt subdir/file3.txt
tar tvf /root/t-u.tar
-rw-r----- root/root        10 2026-10-06 15:37 file1.txt
-rw-r--r-- root/root        12 2026-10-06 15:37 file2.txt
-rw-r--r-- root/root        10 2026-10-06 15:37 subdir/file3.txt

说明:file2.txt 与 subdir/file3.txt 不在原归档中,被追加;file1.txt 已存在且未变更,不重复追加。

5.1.11 -r:向归档追加文件

作用:-r 无条件把新文件追加到归档末尾。

tar cf /root/t-r.tar -C /root/t-tar file1.txt && tar rf /root/t-r.tar -C /root/t-tar file2.txt
tar tf /root/t-r.tar
file1.txt
file2.txt

说明:-r 不检查时间戳;-u 仅追加比归档新的文件。

5.1.12 -A:拼接归档

作用:-A 把多个 tar 归档拼接成一个。

tar cf /root/t-a1.tar -C /root/t-tar file1.txt && tar cf /root/t-a2.tar -C /root/t-tar file2.txt
tar Af /root/t-a1.tar /root/t-a2.tar
tar tf /root/t-a1.tar
file1.txt
file2.txt

5.1.13 -d:比较归档与文件系统的差异

作用:-d 逐个成员比较归档与文件系统的内容、时间、大小、权限差异。

tar df /root/test.tar.gz
./file1.txt: Mode differs
echo "exit=$?"
exit=1
mkdir -p /root/t-dx && cd /root/t-dx && tar xzf /root/test.tar.gz && echo CHANGED > random.bin
tar dzf /root/test.tar.gz
./random.bin: Mod time differs
./random.bin: Size differs
echo "exit=$?"
exit=1

说明:第一次比较因 chmod 640 改过 file1.txt 权限报 Mode differs;第二次改短 random.bin 报 Mod time differs 与 Size differs。存在差异时退出码 1。

5.1.14 --totals:统计归档总字节数

作用:归档写完后打印总写入字节数与速率。

cd /root/t-tar && tar czf /root/t-tot.tar.gz --totals . 2>&1 | tail -1
Total bytes written: 1054720 (1.1MiB, 22MiB/s)

说明:共写入 1054720 字节(1.1MiB,22MiB/s);输出在 stderr。

5.1.15 --sparse:高效处理稀疏文件

作用:-S / --sparse 检测全零空洞,归档时只记录实际数据。

dd if=/dev/zero of=/root/t-tar/sparse.bin bs=1M count=5 seek=100 2>/dev/null
tar cf /root/t-nosp.tar -C /root/t-tar sparse.bin
tar cf /root/t-sp.tar --sparse -C /root/t-tar sparse.bin
ls -l /root/t-nosp.tar /root/t-sp.tar
-rw-r--r-- 1 root root 110110720 Oct  6 15:38 /root/t-nosp.tar
-rw-r--r-- 1 root root   5253120 Oct  6 15:38 /root/t-sp.tar

说明:sparse.bin 逻辑 105M、实际占用 5M;普通打包 110110720 字节,--sparse 打包仅 5253120 字节。

5.1.16 -a:按扩展名自动选择压缩程序

作用:-a 依据归档文件后缀自动选用 gzip / bzip2 / xz 等。Ubuntu 22.04 精简环境默认未装 xz,先补装再实测:

apt-get install -y xz-utils >/dev/null 2>&1
tar caf /root/t-auto.tar.gz -C /root/t-tar file1.txt
tar caf /root/t-auto.tar.xz -C /root/t-tar file1.txt
ls -l /root/t-auto.tar.gz /root/t-auto.tar.xz
-rw-r--r-- 1 root root 128 Oct  6 15:38 /root/t-auto.tar.gz
-rw-r--r-- 1 root root 188 Oct  6 15:38 /root/t-auto.tar.xz

说明:同一文件按后缀自动生成 .tar.gz(128 字节)与 .tar.xz(188 字节)。

5.1.17 --xattrs:打包 / 解包扩展属性

作用:--xattrs 启用扩展属性存取。Ubuntu 精简环境默认无 setfattr,先补装 attr 再实测:

apt-get install -y attr >/dev/null 2>&1
setfattr -n user.comment -v test /root/t-tar/file1.txt
tar cf /root/t-xattr.tar --xattrs -C /root/t-tar file1.txt
mkdir -p /root/t-xa && tar xf /root/t-xattr.tar --xattrs -C /root/t-xa
getfattr -n user.comment /root/t-xa/file1.txt
getfattr: Removing leading '/' from absolute path names
# file: root/t-xa/file1.txt
user.comment="test"

说明:打包(--xattrs)再解包(--xattrs)可完整恢复 user.comment="test"。解包时必须同样带 --xattrs,否则属性不恢复。首行提示为 getfattr 自身输出。

5.1.18 --no-same-owner:解包时不恢复属主

作用:解包时用当前用户作为属主(普通用户默认行为)。

mkdir -p /root/t-no && tar xf /root/t-m.tar --no-same-owner -C /root/t-no
ls -l /root/t-no/file2.txt
-rw-r--r-- 1 root root 12 Jan  1  2020 /root/t-no/file2.txt

说明:本机以 root 解包,属主无可见差异;该参数主要在非 root 解包他人归档时生效。

5.1.19 -m:解包时不恢复修改时间

作用:-m 解包时文件 mtime 用解包时刻而非归档记录(file2.txt 归档 mtime 为 2020-01-01):

tar xf /root/t-m.tar -C /root/t-m2 && tar xmf /root/t-m.tar -C /root/t-m3
stat -c '%y' /root/t-m2/file2.txt
stat -c '%y' /root/t-m3/file2.txt
2020-01-01 12:00:00.000000000 +0000
2026-10-06 15:38:19.096319577 +0000

说明:普通解包恢复归档时间(2020-01-01);加 -m 后为解包时刻(2026-10-06 15:38:19)。

5.1.20 --one-top-level:解包时收纳到单个子目录

作用:解包时自动创建以归档名(去扩展名)命名的子目录收纳所有成员。

mkdir -p /root/t-otl && tar xzf /root/test.tar.gz --one-top-level -C /root/t-otl
find /root/t-otl -maxdepth 2 | sort | head -8
/root/t-otl
/root/t-otl/test
/root/t-otl/test/file1.txt
/root/t-otl/test/file2.txt
/root/t-otl/test/random.bin
/root/t-otl/test/subdir

说明:test.tar.gz 派生目录名 test,成员全部收进 /root/t-otl/test/;--one-top-level=DIR 可自定义目录名。

5.1.21 --transform:解包时转换成员名

作用:--transform=EXPRESSION 用 sed 表达式改写成员名。

mkdir -p /root/t-tr && tar xzf /root/test.tar.gz --transform='s/file/renamed/' -C /root/t-tr ./file1.txt
find /root/t-tr -type f | sort
/root/t-tr/renamed1.txt

说明:./file1.txt 解包为 renamed1.txt。

5.1.22 -h:打包时跟随符号链接

作用:默认归档符号链接本身;-h 归档链接指向的真实文件内容。

ln -sf /root/t-tar/file1.txt /root/t-tar/link1
tar cf /root/t-h1.tar -C /root/t-tar link1 && tar tvf /root/t-h1.tar
tar cf /root/t-h2.tar -h -C /root/t-tar link1 && tar tvf /root/t-h2.tar
lrwxrwxrwx root/root         0 2026-10-06 15:38 link1 -> /root/t-tar/file1.txt
-rw-r----- root/root        10 2026-10-06 15:37 link1

说明:第一行无 -h 归档链接自身(类型 l、0 字节、含 ->);第二行加 -h 归档真实内容(普通文件、10 字节)。

5.1.23 --wildcards:通配符匹配成员

作用:列表 / 解包时用通配符模式匹配成员名(1.34 帮助标注 use wildcards (default for exclusion))。

tar tf /root/test.tar.gz --wildcards '*.txt'
./file1.txt
./file2.txt
./subdir/file3.txt

说明:*.txt 匹配三个文本文件。

5.1.24 -T:从文件读取成员列表

作用:-T FILE 从文本文件按行读取要打包的成员名。

printf 'file1.txt\nsubdir/file3.txt\n' > /root/t-list.txt
cd /root/t-tar && tar czf /root/t-T.tar.gz -T /root/t-list.txt
tar tzf /root/t-T.tar.gz
file1.txt
subdir/file3.txt

5.1.25 --checkpoint:进度提示

作用:--checkpoint=NUMBER 每处理 NUMBER 条记录向 stderr 打印一次进度。

cd /root/t-tar && tar czf /root/t-chk.tar.gz --checkpoint=500 . 2>&1 | head -4
tar: Write checkpoint 500
tar: Write checkpoint 1000
tar: Write checkpoint 1500
tar: Write checkpoint 2000

5.2 gzip(gzip 1.10)

5.2.1 gzip --help 全文

作用:打印完整帮助。gzip 1.10 与 1.9 的帮助输出完全一致(参数集相同)。

gzip --help
Usage: gzip [OPTION]... [FILE]...
Compress or uncompress FILEs (by default, compress FILES in-place).

Mandatory arguments to long options are mandatory for short options too.

  -c, --stdout      write on standard output, keep original files unchanged
  -d, --decompress  decompress
  -f, --force       force overwrite of output file and compress links
  -h, --help        give this help
  -k, --keep        keep (don't delete) input files
  -l, --list        list compressed file contents
  -L, --license     display software license
  -n, --no-name     do not save or restore the original name and timestamp
  -N, --name        save or restore the original name and timestamp
  -q, --quiet       suppress all warnings
  -r, --recursive   operate recursively on directories
      --rsyncable   make rsync-friendly archive
  -S, --suffix=SUF  use suffix SUF on compressed files
      --synchronous synchronous output (safer if system crashes, but slower)
  -t, --test        test compressed file integrity
  -v, --verbose     verbose mode
  -V, --version     display version number
  -1, --fast        compress faster
  -9, --best        compress better

With no FILE, or when FILE is -, read standard input.

Report bugs to <bug-gzip@gnu.org>.

5.2.2 gzip / gunzip / -d / -k / -c / -f:压缩与解压基本操作

作用:gzip FILE 原地压缩并删除原文件;gunzip 等价 gzip -d;-k 保留原文件;-c 输出到 stdout;-f 强制覆盖。

mkdir -p /root/t-gz && cd /root/t-gz
yes 'The quick brown fox jumps over the lazy dog.' | head -c 2000000 > data.txt
gzip data.txt && ls -l
-rw-r--r-- 1 root root 5905 Oct  6 15:39 data.txt.gz
gunzip data.txt.gz && ls -l
-rw-r--r-- 1 root root 2000000 Oct  6 15:39 data.txt
gzip -k data.txt && ls -l
-rw-r--r-- 1 root root 2000000 Oct  6 15:39 data.txt
-rw-r--r-- 1 root root    5905 Oct  6 15:39 data.txt.gz
gzip -d data.txt.gz
gzip: data.txt already exists;  not overwritten
gzip -df data.txt.gz && ls -l
-rw-r--r-- 1 root root 2000000 Oct  6 15:39 data.txt
gzip -c data.txt > out.txt.gz && ls -l out.txt.gz
-rw-r--r-- 1 root root 5905 Oct  6 15:39 out.txt.gz

说明:2M 文本压缩到 5905 字节(99.7%);-d 解压时同名文件已存在报 already exists; not overwritten 且退出码 2,需 -f 强制;-c 写 stdout 不改动原文件。

5.2.3 -1 ~ -9:压缩级别对比

作用:-1 最快到 -9 最高压缩;默认 -6。实测同内容分别用 -1 / -6 / -9 压缩:

cp data.txt d1.txt && cp data.txt d6.txt && cp data.txt d9.txt
gzip -1 d1.txt && gzip -6 d6.txt && gzip -9 d9.txt
ls -l d1.txt.gz d6.txt.gz d9.txt.gz
-rw-r--r-- 1 root root 11725 Oct  6 15:39 d1.txt.gz
-rw-r--r-- 1 root root  5903 Oct  6 15:39 d6.txt.gz
-rw-r--r-- 1 root root  5903 Oct  6 15:39 d9.txt.gz

说明:-1 11725 字节、-6 5903 字节、-9 5903 字节,与 CentOS 8 的 gzip 1.9 实测值完全一致;默认级别 -6。

5.2.4 -t / -l / -v:完整性测试、列表、详细

作用:-t 校验完整性;-l 列出压缩信息;-v 详细输出。

gzip -t data.txt.gz && echo "exit=$?"
exit=0
gzip -l data.txt.gz
         compressed        uncompressed  ratio uncompressed_name
               5905             2000000  99.7% data.txt
gzip -v data.txt
data.txt:        99.7% -- replaced with data.txt.gz

5.2.5 zcat:直接查看压缩文件内容

作用:zcat 等价 gzip -dc,解压到 stdout。

zcat data.txt.gz | head -1
The quick brown fox jumps over the lazy dog.

5.2.6 -n / -N:是否保存原始名与时间戳

作用:-n 压缩时不在头部保存原始文件名与时间戳;-N 保存(默认行为)。用 od 查看头部字节验证:

gzip -n data.txt && od -c data.txt.gz | head -1
0000000 037 213  \b  \0  \0  \0  \0  \0  \0 003 355 312 311 025 202   0
gzip -N data.txt && od -c data.txt.gz | head -1
0000000 037 213  \b  \b   A 026 305   j  \0 003   d   a   t   a   .   t

说明:-n 时第 5~8 字节时间戳全 0 且无文件名(FNAME);-N 保存时间戳与文件名 data.txt。解压时 -n 用 .gz 基名、-N 恢复头部原始名。

5.2.7 -r:递归压缩目录

作用:-r 递归压缩目录内所有文件。

mkdir -p sub1/sub2 && echo a > sub1/a.txt && echo b > sub1/sub2/b.txt
gzip -r sub1 && find sub1 -type f | sort
sub1/a.txt.gz
sub1/sub2/b.txt.gz

5.2.8 -S / --suffix:自定义压缩后缀

作用:-S SUF 用自定义后缀替代默认 .gz。

echo hi > s.txt && gzip -S .gz2 s.txt && ls s.txt.gz2
gunzip -S .gz2 s.txt.gz2 && ls s.txt
s.txt.gz2
s.txt

5.2.9 -q:安静模式

作用:-q 抑制警告,但不抑制错误。实测用"尾部追加垃圾字节"构造警告:

echo GARBAGE >> data.txt.gz && gunzip data.txt.gz
gzip: data.txt.gz: decompression OK, trailing garbage ignored
gzip -q data.txt && echo GARBAGE >> data.txt.gz && gunzip -q data.txt.gz; echo "exit=$?"
exit=2

说明:无 -q 打印 trailing garbage ignored 警告;加 -q 后警告消失但退出码仍为 2。-q 对 already exists; not overwritten 类错误无效(属 error 而非 warning)。

5.2.10 其他:stdin 管道与 -V

echo hello | gzip | gunzip
hello
gzip -V | head -1
gzip 1.10

5.3 unzip / zip(UnZip 6.00 + Zip 3.0)

5.3.1 zip:创建 zip 归档

作用:zip 归档自带目录结构且默认压缩。实测创建 /root/t-zip 测试目录并打包:

mkdir -p /root/t-zip/subd && cd /root/t-zip
echo 'file one content' > f1.txt && echo 'file two content' > f2.txt
echo 'sub file content' > subd/f3.txt
zip -r archive.zip f1.txt f2.txt subd
  adding: f1.txt (stored 0%)
  adding: f2.txt (stored 0%)
  adding: subd/ (stored 0%)
  adding: subd/f3.txt (stored 0%)

说明:zip -r 递归包含目录;小文件无压缩空间,标注 (stored 0%)。

5.3.2 unzip -v:版本信息

unzip -v | head -3
UnZip 6.00 of 20 April 2009, by Debian. Original by Info-ZIP.

Latest sources and executables are at ftp://ftp.info-zip.org/pub/infozip/ ;

说明:Ubuntu 的 unzip 为 Debian 再打包版本(CentOS 8 为 Info-ZIP 原版,见第六章对照表)。

5.3.3 unzip 解压 / -l / -t / -d

作用:unzip FILE.zip 解压到当前目录;-l 列出;-t 测试;-d DIR 解压到指定目录。

mkdir -p /root/t-zip-x && cd /root/t-zip-x && unzip /root/t-zip/archive.zip
Archive:  /root/t-zip/archive.zip
 extracting: f1.txt
 extracting: f2.txt
   creating: subd/
 extracting: subd/f3.txt
unzip -l /root/t-zip/archive.zip
Archive:  /root/t-zip/archive.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       17  2026-10-06 15:40   f1.txt
       17  2026-10-06 15:40   f2.txt
        0  2026-10-06 15:40   subd/
       17  2026-10-06 15:40   subd/f3.txt
---------                     -------
       51                     4 files
unzip -t /root/t-zip/archive.zip
Archive:  /root/t-zip/archive.zip
    testing: f1.txt                   OK
    testing: f2.txt                   OK
    testing: subd/                    OK
    testing: subd/f3.txt              OK
No errors detected in compressed data of /root/t-zip/archive.zip.
rm -rf /root/t-zip-d && unzip /root/t-zip/archive.zip -d /root/t-zip-d >/dev/null
find /root/t-zip-d -type f | sort
/root/t-zip-d/f1.txt
/root/t-zip-d/f2.txt
/root/t-zip-d/subd/f3.txt

说明:注意 -l 的日期格式为 2026-10-06(YYYY-MM-DD,ISO 风格),与 CentOS 8 的 10-06-2026(MM-DD-YYYY)不同,这是 Debian 版 unzip 与 Info-ZIP 原版的显示差异。

5.3.4 -o / -n / -q:覆盖、不覆盖、安静

cd /root/t-zip-x && echo CHANGED > f1.txt && unzip -o /root/t-zip/archive.zip f1.txt
cat f1.txt
Archive:  /root/t-zip/archive.zip
 extracting: f1.txt
file one content
echo AGAIN > f1.txt && unzip -n /root/t-zip/archive.zip f1.txt && cat f1.txt
Archive:  /root/t-zip/archive.zip
AGAIN
rm -rf /root/t-zip-q && mkdir -p /root/t-zip-q
unzip -q /root/t-zip/archive.zip -d /root/t-zip-q; echo "exit=$?"
exit=0

说明:-o 覆盖为归档内容;-n 保留磁盘文件;-q 无输出且 exit=0。

5.3.5 -x:排除解压

unzip -l /root/t-zip/archive.zip -x 'f2.txt' | tail -3
       17  2026-10-06 15:40   subd/f3.txt
---------                     -------
       34                     3 files

说明:排除 f2.txt 后剩余 3 个成员(34 字节)。

5.3.6 -P:密码加密 / 解密

作用:zip -P 密码 创建加密 zip;解压时 unzip -P 密码 提供口令。

cd /root/t-zip && zip -P test -r secret.zip f1.txt
unzip -o secret.zip f1.txt
Archive:  secret.zip
   skipping: f1.txt                  unable to get password
cd /root/t-zip-x && rm -f f1.txt && unzip -P test -o /root/t-zip/secret.zip f1.txt
cat f1.txt
Archive:  /root/t-zip/secret.zip
 extracting: f1.txt
file one content

说明:无密码解压报 unable to get password;-P test 提供口令后正常解出。

5.3.7 -Z:zipinfo 模式

作用:unzip -Z 以 zipinfo 格式列出内容,-Z -v 打印内部结构诊断信息。

unzip -Z /root/t-zip/archive.zip
Archive:  /root/t-zip/archive.zip
Zip file size: 641 bytes, number of entries: 4
-rw-r--r--  3.0 unx       17 tx stor 26-Oct-06 15:40 f1.txt
-rw-r--r--  3.0 unx       17 tx stor 26-Oct-06 15:40 f2.txt
drwxr-xr-x  3.0 unx        0 bx stor 26-Oct-06 15:40 subd/
-rw-r--r--  3.0 unx       17 tx stor 26-Oct-06 15:40 subd/f3.txt
4 files, 51 bytes uncompressed, 51 bytes compressed:  0.0%
unzip -Z -v /root/t-zip/archive.zip | head -9
Archive:  /root/t-zip/archive.zip
There is no zipfile comment.

End-of-central-directory record:
-------------------------------

  Zip archive file size:                       641 (0000000000000281h)
  Actual end-cent-dir record offset:           619 (000000000000026Bh)
  Expected end-cent-dir record offset:         619 (000000000000026Bh)

说明:-Z 输出含 Unix 权限、压缩方法(stor)与文本标记(tx/bx);-Z -v 输出中央目录结构诊断。

5.3.8 -v:详细列表

作用:unzip -v ARCHIVE 长格式列出(含 CRC-32)。

unzip -v /root/t-zip/archive.zip | head -9
Archive:  /root/t-zip/archive.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
      17  Stored       17   0% 2026-10-06 15:40 5095d002  f1.txt
      17  Stored       17   0% 2026-10-06 15:40 ef86f377  f2.txt
       0  Stored        0   0% 2026-10-06 15:40 00000000  subd/
      17  Stored       17   0% 2026-10-06 15:40 703f9b1b  subd/f3.txt
--------          -------  ---                            -------
      51               51   0%                            4 files

5.3.9 -j:丢弃路径解压

mkdir -p /root/t-junk && cd /root/t-junk
unzip -j /root/t-zip/archive.zip subd/f3.txt >/dev/null && ls -l
-rw-r--r-- 1 root root 17 Oct  6 15:36 f3.txt

说明:subd/f3.txt 路径被丢弃,f3.txt 直接落在当前目录。

5.3.10 -a / -b:ASCII 转换 / 二进制

作用:-a 把文本行尾 CRLF 转为 LF;-b 二进制模式不做转换。

cd /root/t-zip && printf 'line1\r\nline2\r\n' > crlf.txt && zip -r crlf.zip crlf.txt >/dev/null
unzip -p crlf.zip crlf.txt | od -c | head -2
0000000   l   i   n   e   1  \r  \n   l   i   n   e   2  \r  \n
0000016
unzip -a -p crlf.zip crlf.txt | od -c | head -2
0000000   l   i   n   e   1  \n   l   i   n   e   2  \n
0000014
unzip -b -p crlf.zip crlf.txt | od -c | head -2
0000000   l   i   n   e   1  \r  \n   l   i   n   e   2  \r  \n
0000016

说明:默认与 -b 保留 \r\n;-a 转换为 \n。

六、常见问题与真实行为

6.1 现象:apt-get install --download-only 只打印 0 upgraded 且不下载任何文件

原因:四个包已是最新且已安装,apt 判定无升级需求,即使指定 --download-only 也不会拉取 deb(目录内仅生成 lock 与 partial 占位)。解决:加 --reinstall 强制按重装流程下载,实测回显 Download complete and in download only mode。

6.2 现象:tar cjf 报 /bin/sh: bzip2: not found

原因:Ubuntu 22.04 精简环境默认未安装 bzip2,-j 需要外部 bzip2 程序。实测回显:

/bin/sh: 1: bzip2: not found
tar: /root/t-bz2.tar.bz2: Wrote only 4096 of 10240 bytes
tar: Child returned status 127
tar: Error is not recoverable: exiting now

解决:apt-get install -y bzip2 后 tar cjf 正常(实测 1054752 字节)。

6.3 现象:tar cJf / tar caf xxx.tar.xz 报 /bin/sh: 1: xz: not found

原因:精简环境默认未安装 xz-utils,-J 与 -a(.tar.xz)均需要外部 xz 程序。实测回显:

/bin/sh: 1: xz: not found
tar: Child returned status 127
tar: Error is not recoverable: exiting now

解决:apt-get install -y xz-utils 后实测 .tar.xz 1066224 字节。CentOS 8 默认已装 xz,无此问题。

6.4 现象:tar -k 解包已存在文件报 Cannot open: File exists 且退出码 2

原因:-k / --keep-old-files 有意把已存在文件当作错误处理。实测退出码 2;如需"存在即跳过不报错"改用 --skip-old-files。

6.5 现象:gzip -d 解压时报 data.txt already exists; not overwritten

原因:gzip 默认不覆盖同名文件,需 -f 强制;-q 无法抑制该错误(属 error 而非 warning)。

6.6 现象:gzip -q 能静默"尾部垃圾"警告但不改变退出码

实测在 .gz 末尾追加垃圾字节后解压:无 -q 打印 decompression OK, trailing garbage ignored 且退出码 2;加 -q 后警告消失但退出码仍为 2。

6.7 现象:tar xf 指定成员名 subdir/file3.txt 报 Not found in archive

原因:归档内成员名带 ./ 前缀(./subdir/file3.txt),精确指定必须完全一致或使用通配符。解决:写 ./subdir/file3.txt 或 tar xf a.tar --wildcards '*/file3.txt'。

6.8 版本差异速览(Ubuntu 22.04 与 CentOS 8 对照)

对比项Ubuntu 22.04(本篇)CentOS 8(另篇)
tar 版本GNU tar 1.34(1.34+dfsg-1ubuntu0.1.22.04.6)GNU tar 1.30(2:1.30-5.el8)
gzip 版本1.10(1.10-4ubuntu4.2)1.9(1.9-12.el8)
unzip 版本UnZip 6.00(Debian 再打包,6.0-26ubuntu3.2)UnZip 6.00(Info-ZIP 原版,6.0-45.el8_4)
zip 版本Zip 3.0(3.0-12build2)Zip 3.0(3.0-23.el8)
tar --help 差异新增 --zstd;--wildcards 标注 use wildcards (default for exclusion)无 --zstd;--wildcards 标注 use wildcards (default)
tar 默认 rmt/rsh--rmt-command=/usr/sbin/rmt --rsh-command=/usr/bin/rsh--rmt-command=/etc/rmt --rsh-command=/usr/bin/ssh
tar -j 依赖默认缺 bzip2,需 apt-get install -y bzip2默认缺 bzip2,需 dnf install -y bzip2
tar -J 依赖默认缺 xz,需 apt-get install -y xz-utilsxz 已装
gzip 行为与 1.9 实测行为一致(help 相同、-1/-6/-9 体积相同)与 1.10 一致
unzip -l 日期格式2026-10-06(YYYY-MM-DD)10-06-2026(MM-DD-YYYY)
包来源Ubuntu 官方仓库 jammy/main 与 jammy-updatesCentOS 8 官方 BaseOS

七、命令与参数速查表

7.1 tar

参数/命令含义实测说明/默认值
tar --version打印版本实测 tar (GNU tar) 1.34
tar --help打印帮助含全部长选项与默认值段
-c / --create创建归档tar czf a.tar.gz dir
-x / --extract解包配合 -C 指定落盘目录
-t / --list列出内容不写文件
-v / --verbose详细输出列表显示权限/属主/大小/时间
-f FILE归档文件- 表示标准输入输出
-zgzip 压缩实测 .tar.gz 1049214 字节
-jbzip2 压缩需安装 bzip2;实测 .tar.bz2 1054752 字节
-Jxz 压缩需安装 xz-utils;实测 .tar.xz 1066224 字节
--zstdzstd 压缩1.34 新增选项(未实测,依赖 zstd)
-a按后缀自动压缩.tar.gz→gzip、.tar.xz→xz
--exclude=PATTERN排除文件默认通配符匹配
-C DIR切换目录打包/解包均可
-p解包保留权限root 默认保留
-P保留绝对路径list 阶段打印 Removing 提示
--strip-components=N剥离 N 层路径实测剥离 ./ 前缀
-k / --keep-old-files不覆盖已存在文件报错且 exit=2
-u / --update只追加更新的文件归档中不存在或更新才追加
-r / --append追加文件无条件追加到末尾
-A / --catenate拼接归档第二归档并入第一归档
-d / --compare比较归档与文件系统有差异 exit=1
--totals打印总字节数实测 1054720 (1.1MiB)
--sparse稀疏文件优化105M 文件归档仅 5M
--xattrs扩展属性存取解包时也需带此参数
--no-same-owner不恢复属主普通用户默认行为
-m / --touch解包不恢复 mtime实测用解包时刻
--one-top-level[=DIR]收纳到单一子目录默认用归档名(去扩展名)
--transform=EXPR转换成员名sed 表达式
-h / --dereference跟随符号链接默认归档链接本身
--wildcards通配符匹配1.34 标注 default for exclusion
-T FILE从文件读成员列表每行一个名
--checkpoint=N进度提示默认每 10 条记录一次

7.2 gzip

参数/命令含义实测说明/默认值
gzip --version / -V打印版本实测 gzip 1.10
gzip FILE压缩(原地)2M→5905 字节(99.7%),默认删原文件
gunzip(gzip -d)解压目标存在时报错需 -f
-k / --keep保留原文件压缩/解压均适用
-c / --stdout输出到 stdout原文件不变
-f / --force强制覆盖覆盖已存在 .gz / 解压目标
-1 ~ -9压缩级别默认 -6;实测 -1=11725、-6=5903、-9=5903 字节
-t / --test完整性测试通过无输出 exit=0
-l / --list列出压缩信息显示 5905/2000000/99.7%
-v / --verbose详细输出99.7% -- replaced with ...
zcat查看 .gz 内容等价 gzip -dc
-n / --no-name不保存原名与时间戳头部 MTIME=0 无 FNAME
-N / --name保存原名与时间戳默认行为,头部含 FNAME
-r / --recursive递归压缩目录目录结构保留
-S SUF / --suffix自定义后缀默认 .gz
-q / --quiet安静模式抑制警告、不抑制错误

7.3 unzip / zip

参数/命令含义实测说明/默认值
unzip -v版本信息实测 UnZip 6.00(Debian 再打包)
zip -r a.zip dir创建 zip小文件 stored 0%
unzip a.zip解压到当前目录自动创建目录
-l列出内容4 files 51 bytes,日期为 YYYY-MM-DD
-t测试完整性No errors detected
-d DIR解压到目录输出带完整路径
-o覆盖解压恢复归档内内容
-n不覆盖保持磁盘文件
-q安静模式无输出 exit=0
-x PATTERN排除解压实测排除后 3 files
-P PWD密码解压/加密zip -P test 加密;无密码报 unable to get password
-Zzipinfo 模式含权限/压缩方法;-Z -v 为结构诊断
-v ARCHIVE详细列表含 CRC-32 校验值
-j丢弃路径解压所有文件落当前目录
-aASCII 转换\r\n→\n
-b二进制不做转换
-p输出到 stdout配合 -a/-b 验证行尾

八、卸载方式

在线安装的四个包使用 apt-get 卸载:

apt-get remove -y tar gzip unzip zip

如需连同配置文件一起彻底清除:

apt-get purge -y tar gzip unzip zip

离线场景(使用 dpkg -i 安装)可先 dpkg -l tar gzip unzip zip 确认包状态,再执行上述 apt-get remove/purge,或直接 dpkg -r tar gzip unzip zip。卸载后可用各工具 --version 确认已移除。

💡 说明:tar / gzip / unzip / zip 均为无守护进程的命令行工具,卸载后无需清理服务与自启项;tar / gzip 是系统预装基础组件,卸载可能影响依赖它们的其他软件,建议谨慎操作。实测补装的 bzip2、xz-utils、attr(tar -j / -J / --xattrs 依赖)如需一并移除可执行 apt-get remove -y bzip2 xz-utils attr。