文章目录


这个命令居然windows控制台也能用!用来解压tgz文件不错@20230228 (要注意不要解压带软链接的,会导致软链接失效!)

除了windows控制台,git的git bash here也支持这个命令:

在这里插入图片描述

tar命令简答使用示例

解压tgz:tar xvzf 压缩包
压缩tgz:tar cvzf 压缩包包名.tar 文件1 文件2 文件3…

压缩tgz时排除文件:tar cvzf kyai.tgz kyai/ --exclude=.docker_temp_652316136

解压tgz到某个目录下:tar -xvzf file.tar -C /path/to/directory

tar命令使用文档(man 1 tar)

英文

TAR(1)                                                                    User Commands                                                                   TAR(1)

NAME
       tar - manual page for tar 1.26

SYNOPSIS
       tar [OPTION...] [FILE]...

DESCRIPTION
       GNU `tar' saves many files together into a single tape or disk archive, and can restore individual files from the archive.

       Note  that  this  manual  page  contains just very brief description (or more like a list of possible functionality) originally generated by the help2man
       utility.  The full documentation for tar is maintained as a Texinfo manual.  If the info and tar programs are properly installed at your site,  the  com‐
       mand `info tar' should give you access to the complete manual.

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.

DEFAULTS
       *This* tar installation defaults to:

       --format=gnu -f- -b20 --quoting-style=escape --rmt-command=/sbin/rmt --rsh-command=/usr/bin/rsh

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

Common options:
       -C, --directory=DIR
              change to directory DIR	//切换到目录 DIR

       -f, --file=ARCHIVE
              use archive file or device ARCHIVE

       -j, --bzip2
              filter the archive through bzip2

        -J, --xz
              filter the archive through xz

       -p, --preserve-permissions
              extract  information  about  file permissions (default for superuser)

       -v, --verbose
              verbosely list files processed

       -z, --gzip
              filter the archive through gzip

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-newer-files
              don't replace existing files that are newer than their archive copies

       --keep-directory-symlink
              Don't replace existing symlinks to directories when extracting.

       --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
              sort names to extract to match 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

       Note: You might need to install external program (lzip/ncompress/lzma...) to use some of these compression options

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 in 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 dates 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

AUTHOR
       Written by John Gilmore and Jay Fenlason.

REPORTING BUGS
       Report bugs to <bug-tar@gnu.org>.

COPYRIGHT
       Copyright © 2013 Free Software Foundation, Inc.  License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
       This is free software: you are free to change and redistribute it.  There is NO WARRANTY, to the extent permitted by law.

tar 1.26                                                                  February 2013                                                                   TAR(1)
 Manual page tar(1) line 457/550 (END) (press h for help or q to quit)

中文

TAR(1) 用户命令 TAR(1)

名称
       tar - tar 1.26 的手册页

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

描述
       GNU `tar' 将许多文件一起保存到单个磁带或磁盘存档中,并可以从存档中还原单个文件。

       注意,此手册页只包含了非常简短的描述(或者更像是可能功能的列表),最初由help2man实用程序生成。 tar的完整文档作为Texinfo手册维护。 如果在您的站点正确安装了info和tar程序,`info tar' 命令应该能让您访问完整的手册。

示例
       tar -cf archive.tar foo bar
              # 从文件 foo 和 bar 创建 archive.tar。

       tar -tvf archive.tar
              # 详细列出 archive.tar 中的所有文件。

       tar -xf archive.tar
              # 从 archive.tar 中提取所有文件。

默认值
       *这个* tar 安装默认为:

       --format=gnu -f- -b20 --quoting-style=escape --rmt-command=/sbin/rmt --rsh-command=/usr/bin/rsh

主要操作模式:
       -A, --catenate, --concatenate
              将 tar 文件追加到一个存档中

       -c, --create
              创建一个新的存档

       -d, --diff, --compare
              查找存档和文件系统之间的差异

       --delete
              从存档中删除(不在磁带上!)

       -r, --append
              将文件追加到存档的末尾

       -t, --list
              列出存档的内容

       --test-label
              测试存档卷标签并退出

       -u, --update
              仅追加比存档中的副本更新的文件

       -x, --extract, --get
              从存档中提取文件

常见选项:
       -C, --directory=DIR
              切换到目录 DIR

       -f, --file=ARCHIVE
              使用存档文件或设备 ARCHIVE

       -j, --bzip2
              通过 bzip2 过滤存档

       -J, --xz
              通过 xz 过滤存档

       -p, --preserve-permissions
              提取有关文件权限的信息(超级用户默认)

       -v, --verbose
              详细列出已处理的文件

       -z, --gzip
              通过 gzip 过滤存档

操作修饰符:
       --check-device
              创建增量存档时检查设备号码(默认)

       -g, --listed-incremental=FILE
              处理新的 GNU 格式增量备份

       -G, --incremental
              处理旧的 GNU 格式增量备份

       --ignore-failed-read
              不因无法读取的文件而退出非零

       --level=NUMBER
              创建 listed-incremental 存档的转储级别

       -n, --seek
              存档是可查找的

       --no-check-device
              创建增量存档时不检查设备号码

       --no-seek
              存档不可查找

       --occurrence[=NUMBER]
              只处理存档中每个文件的 NUMBERth 事件;此选项仅在与 --delete、--diff、--extract 或 --list 的子命令一起使用,并且在命令行上或通过 -T 选项给出文件列表时有效;NUMBER 默认为 1

       --sparse-version=MAJOR[.MINOR]
              设置要使用的稀疏格式的版本(暗示 --sparse)

       -S, --sparse
              高效处理稀疏文件

覆盖控制:
       -k, --keep-old-files
              在提取时,不替换现有文件,将它们视为错误

       --keep-newer-files
              不替换比其存档副本更新的现有文件

       --keep-directory-symlink
              在提取时,不替换现有的指向目录的符号链接。

       --no-overwrite-dir
              保留现有目录的元数据

       --overwrite
              在提取时覆盖现有文件

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

       --recursive-unlink
              在提取目录之前清空层次结构

       --remove-files
              将文件添加到存档后删除它们

       --skip-old-files
              在提取时,不替换现有文件,静默地跳过它们

       -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'--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
              按存档中的顺序提取文件

扩展文件属性的处理:
       --acls 启用POSIX ACLs支持

       --no-acls
              禁用POSIX ACLs支持

       --no-selinux
              禁用SELinux上下文支持

       --no-xattrs
              禁用扩展属性支持

       --selinux
              启用SELinux上下文支持

       --xattrs
              启用扩展属性支持

       --xattrs-exclude=MASK
              指定xattr键的排除模式

       --xattrs-include=MASK
              指定xattr键的包含模式

设备选择和切换:
       -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
              使用远程COMMAND替代rsh

       --volno-file=FILE
              在FILE中使用/更新卷号

设备阻塞:
       -b, --blocking-factor=BLOCKS
              每个记录BLOCKS x 512字节

       -B, --read-full-records
              我们读取时重新阻塞(用于4.2BSD管道)

       -i, --ignore-zeros
              忽略存档中的零块(表示EOF)

       --record-size=NUMBER
              每个记录的字节数,是512的倍数

存档格式选择:
       -H, --format=FORMAT
              创建给定格式的存档

              FORMAT是以下之一:

                     gnu    GNU tar 1.13.x 格式

                     oldgnu 根据tar <= 1.12的GNU格式

                     pax    POSIX 1003.1-2001 (pax) 格式

                     posix  同pax

                     ustar  POSIX 1003.1-1988 (ustar) 格式

                     v7     旧V7 tar格式

       --old-archive, --portability
              等同于--format=v7

       --pax-option=keyword[[:]=value][,keyword[[:]=value]]...
              控制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 通过lzma过滤存档

       --lzop

       --no-auto-compress
              不使用存档后缀来确定压缩程序

       -z, --gzip, --gunzip, --ungzip
              通过gzip过滤存档

       -Z, --compress, --uncompress
              通过compress过滤存档

       注意:您可能需要安装外部程序(lzip/ncompress/lzma...)才能使用这些压缩选项

本地文件选择:
       --add-file=FILE
              将指定的FILE添加到归档中(如果文件名以破折号开头时很有用)

       --backup[=CONTROL]
              删除前备份,选择版本CONTROL

       -C, --directory=DIR
              更改工作目录到DIR

       --exclude=PATTERN
              排除模式匹配的文件

       --exclude-backups
              排除备份和锁定文件

       --exclude-caches
              排除包含CACHEDIR.TAG的目录内容,但标签文件本身除外

       --exclude-caches-all
              排除包含CACHEDIR.TAG的目录

       --exclude-caches-under
              排除包含CACHEDIR.TAG的目录下的所有内容

       --exclude-tag=FILE
              排除包含FILE的目录内容,但FILE本身除外

       --exclude-tag-all=FILE
              排除包含FILE的目录

       --exclude-tag-under=FILE
              排除包含FILE的目录下的所有内容

       --exclude-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进行了覆盖,默认为'~'-T, --files-from=FILE
              从FILE获取要提取或创建的文件名

       --unquote
              对使用-T读取的文件名进行反引号处理(默认设置)

       -X, --exclude-from=FILE
              排除FILE中列出的模式

文件名转换:
       --strip-components=NUMBER
              在提取时从文件名中剥离前NUMBER个组件

       --transform=EXPRESSION, --xform=EXPRESSION
              使用sed替换表达式来转换文件名

              文件名匹配选项(影响排除和包含模式):

       --anchored
              模式从文件名开始处匹配

       --ignore-case
              忽略大小写

       --no-anchored
              模式可以在任意`/'后匹配(排除的默认设置)

       --no-ignore-case
              区分大小写的匹配(默认设置)

       --no-wildcards
              精确字符串匹配

       --no-wildcards-match-slash
              通配符不匹配`/'

       --wildcards
              使用通配符(默认设置)

       --wildcards-match-slash
              通配符匹配`/'(排除的默认设置)

信息性输出:
       --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-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
              打印程序版本

       对于长选项的强制或可选参数,对应的短选项也是强制或可选的。

       备份后缀默认为 `~',除非使用 --suffix 或 SIMPLE_BACKUP_SUFFIX 进行设置。版本控制可以通过 --backup 或 VERSION_CONTROL 进行设置,值包括:

       none, off
              从不创建备份

       t, numbered
              创建编号备份

       nil, existing
              如果存在编号备份则创建编号备份,否则创建简单备份

       never, simple
              总是创建简单备份

       --quoting-style 选项的有效参数包括:

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

作者
       由 John Gilmore 和 Jay Fenlason 编写。

报告错误
       向 <bug-tar@gnu.org> 报告错误。

版权
       版权 © 2013 自由软件基金会(Free Software Foundation, Inc.)。许可证 GPLv3+:GNU GPL 版本 3 或之后版本 <http://gnu.org/licenses/gpl.html>。
       这是自由软件:您可以自由更改和重新分发它。在法律允许的范围内,不提供任何保证。

tar 1.26                                                                  20132月                                                                     TAR(1)
 手册页 tar(1)457/550(结束) (按 h 获取帮助或 q 退出)

示例

待更新。。。

注意

tar xpvf -              解开归档文件('x'),保持文件属主和文件的权限('p'),
 #                            并且把输出的详细信息打印到标准输出 ('v'),
 #                            从标准输入读('f'后跟'-').
 #
 #                            注意'x'是一个命令,'p', 'v', 'f'是选项.

参考文章:linux shell编程、bash编程、shell教程、bash教程、shell文档、bash文档、shell脚本、bash脚本【教程】第二部分:基础

20220926 如何打包指定目录的文件

压缩:

tar czvf /data/backup/test.tar.gz /data/a/b/directory

解压:

cd /data/test
 tar xzvf /data/backup/test.tar.gz

问题是,解压后的文件,在/data/test/data/a/b/directory里面

要想解压在当前目录路径。

这样写就可以解决了

tar czvf /data/backup/test.tar.gz /data/a/b/directory

改成

tar czvf /data/backup/test.tar.gz -C /data/a/b/directory

-C是临时切换工作目录,-P是绝对路径,在这里只用到-C参数就行了

参考文章:linux中tar打包指定路径文件

20230129 用解压tgz的命令解压tar还解压不了。。。

解压tgz要加z

在这里插入图片描述

20230328 自己注意啊,好几次把压缩.tgz文件的后缀写成.tar,到时候别人弄不清的,因为以gzip压缩和解压都要加-z参数的,别人可能不知道,解压的时候不加-z,就解压不出来!

20230329 普通压缩包一般用.tgz,但是用docker save -o生成的镜像包就是.tar,这个注意区分开!

20230417 十分之坑,千万不要在windows上解压带有软链接的压缩文件,会把软链接搞没了!

在这里插入图片描述

搞了半天,原来是软链接给弄没了。。。

20230516 软链接失效测试

剪切到windows测试(发现会失效)

如图,我的ubuntu目录中有一个b.cpp文件,我给它创建了一个软链接:

ln -s b.cpp b

在这里插入图片描述
在这里插入图片描述

然后我把它剪切到windows桌面上:

在这里插入图片描述

然后我再剪切回去:

在这里插入图片描述

然后我们发现软链接失效了:

在这里插入图片描述

用winrar打压缩包测试(会失效)

如图,我用winrar打了个压缩包,然后再解压,发现软链接失效了:

在这里插入图片描述

ubuntu下用tar命令打压缩包测试(不会失效)

如图,在ubuntu里用tar命令打压缩包,再解压不会失效:

在这里插入图片描述

windows上用tar命令压缩测试(会失效)

在这里插入图片描述

在ubuntu上用tar命令压缩,在windows上用tar命令解压(发现根本解压不了)

在ubuntu上用tar命令压缩软链接,然后在windows上用tar命令解压,发现根本解压不了

在这里插入图片描述

结论:linux软链接无法在windows上存活,只要其暴露于windows下,就会失效

20231030 如何压缩时对软链接解引用,将实际文件打进压缩包?

tar -czf 命令默认会保留软链接,不会把软链接转换为实际文件进行压缩。但是如果想将软链接转换为实际文件进行压缩,可以使用 -h--dereference 选项。这个选项会告诉 tar 命令跟踪并包含软链接指向的实际文件。

以下是一个例子:

tar -czhf output.tar.gz input

在这个命令中,output.tar.gz 是输出的压缩文件,input 是要压缩的文件或目录。-h 选项让 tarinput 中的软链接转换为它们实际指向的文件。

20231115 解决文件时间相对系统时间超前,导致输出大量警告问题(加-m参数,tar -xzmf不提取文件时间,按解压时间赋予文件时间)

如图,我的系统时间不对,压缩包内文件时间相对系统时间更新,导致输出大量警告:

在这里插入图片描述

在这里插入图片描述

我们解压时加个-m参数就好了,记得不要加在-f参数后,-f参数后是要直接接压缩包的:

示例:

tar -xzmf $DEPLOY_PACK --strip-components=2 -C $INSTALL_PATH

20240130 也可以把大量警告屏蔽掉,但是不推荐这样做,因为把可能解压的错误信息也屏蔽掉了,如果解压失败,程序将继续往下运行,有点危险(但是正常来说应该不会出问题,可以这样做,因为按照之前加了-m参数,导致文件时间都一个样)(但是2>/dev/null屏蔽了应有的错误信息,这样做也不靠谱!)

tar命令在解压文件时,如果遇到文件的时间戳比当前系统时间晚,就会输出一条警告信息。这种情况下,你可以使用重定向操作符将这些警告信息屏蔽掉。

在Linux中,2是标准错误输出(stderr),1是标准输出(stdout)。我们可以通过将stderr重定向到/dev/null(一个特殊的设备,它丢弃所有写入其的数据)来屏蔽警告信息。所以,你可以尝试这样操作:

tar -xzf test.tgz 2>/dev/null

这个命令将只打印标准输出(stdout),而忽略所有标准错误输出(stderr)。注意,这样做将屏蔽所有错误信息,包括可能需要注意的重要错误。因此,如果你想在保留其他错误信息的同时,只屏蔽特定的警告信息,可能需要更复杂的处理方式。

在这里插入图片描述

2024031 自定义压缩等级,以最快速度压缩

如果你的解压端接口要求使用.tgz格式,但你又希望尽可能减少压缩时间,你可以使用tar命令结合gzip的最快压缩等级。gzip允许你通过-1(也可以用--fast表示)指定最快的压缩速度,虽然这会略微牺牲压缩效率。

要创建一个.tgz文件,同时尽量减少压缩所需的时间,你可以先使用tar命令创建一个未压缩的归档文件,然后用gzip的快速压缩选项来压缩这个归档文件。但是,更简便的方式是直接在tar命令中指定gzip的压缩等级。下面是如何做到这一点的命令:

tar -cvf - /path/to/directory | gzip -1 > archive.tgz

或者,更直接的方法是在tar命令中使用-z参数并通过GZIP环境变量指定压缩等级:

GZIP=-1 tar -czvf archive.tgz /path/to/directory

这两种方法都会生成.tgz文件,同时通过选择最快的压缩等级来减少压缩所需的时间。请将/path/to/directory替换为你想要打包的目录路径。这样,即使你的解压端接口要求使用.tgz格式,你也能在保证文件格式兼容的同时,尽可能地减少压缩时间。

如:

GZIP=-1 tar -czf ${DEPLOY_PACK} ${DIR_TO_BE_COMPRESSED}

gzip: warning: GZIP environment variable is deprecated; use an alias or script问题

如果你在使用GZIP环境变量来设置gzip压缩等级时遇到了“gzip: warning: GZIP environment variable is deprecated; use an alias or script”的警告,这意味着直接通过GZIP环境变量来控制压缩等级的方法已不被推荐使用。作为替代,你可以创建一个别名(alias)或者编写一个简单的脚本来达到同样的目的,不过这在一些情况下可能不太方便。

一个更简单且不会引起警告的方法是直接在命令行中使用管道(pipe)将tar命令的输出传递给gzip,并在gzip命令中明确指定压缩等级。这样可以避免使用已被废弃的环境变量,并且仍然能够控制压缩过程的速度。

修改你的kyai_tar.sh脚本,将创建归档的部分改为下面这样:

tar -cvf - kyai | gzip -1 > kyai_arm_ubuntu_20240314-105323.tgz

这里,tar -cvf - kyai命令会创建一个归档并将其输出到标准输出(stdout),|管道会将这个输出作为gzip -1命令的输入,gzip -1则将其压缩在最快的压缩等级并输出,最后> kyai_arm_ubuntu_20240314-105323.tgz会将压缩后的数据重定向到文件中。

这种方式不会使用到已被废弃的GZIP环境变量,因此不会产生警告,同时仍能实现快速压缩的目的。请确保替换脚本中相应的部分,以适应你的具体需求。

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐