1.命令作用
命令tar将许多文件一起保存到单个磁带或磁盘存档中,并且可以从存档中恢复单个文件(GNU 'tar' saves many files together into a single tape or disk archive, and can restore individual files from the archive.)。
2.命令语法
Usage: tar [OPTION...] [FILE]...
2.简单举例
tar -cf archive.tar foo bar,将文件foo和bar,创建一个tar格式的归档(archive.tar)。
tar -tvf archive.tar,详细列出archive.tar中的所有文件。
tar -xf archive.tar,从archive.tar中提取所有文件。
3.参数详解
OPTION:
①主要运行方式:
- -A, --catenate, --concatenate,将tar文件附加到另一个tar文件(归档文件)中,需与-f 参数一起使用;仅可一个一个tar文件附加,不可多个tar文件一次性附加;
- -c, --create,创建一个新的归档文件,需与-f 参数一起使用;
- -d, --diff, --compare,查找归档中文件和系统上解压后的文件之间的差异,系统上解压后的文件多了不会输出,如果解压后的文件少了则会输出提示,需与-f、-v 参数一起使用;
- --delete,从存档中删除文件或者目录,删除的文件和目录可以使用tar -tf 查看;
- -r, --append,将文件追加到存档文件末尾,需与-f 参数一起使用;
- -t, --list,列出存档的内容列表,需与-f 参数一起使用;
- --test-label,测试归档卷标签并退出,用于自动化脚本验证归档的存在和卷标是否符合要求;
- -u, --update,只追加比归档文件中比列表文件更新的文件,需与-f、-v 参数一起使用;使用-tf 查看更新后的归档文件列表,是追加方式,即列表中存在多个不同版本但相同名称的文件;注意解压后仅只保留版本最新的文件;
- -x, --extract, --get,从存档中提取全部文件,或者指定文件名称进行提取,通常与-f、-v 参数一起使用
②操作修饰符:
- --check-device,在创建或增量存档时,检查设备(默认)
- -g, --listed-incremental=FILE,处理新GNU格式的增量归档,需要保留所有的归档,增量归档解压需要指定快照文件,全量归档加多个增量归档可以进行备份;
- -G, --incremental,处理旧GNU格式增量备份,与-g用法类似
- --ignore-failed-read,解压归档时,当归档中有不可读的文件(归档存在损坏情况),不会因为非零退出,而是继续提取归档中获取文件,对损坏的归档解压场景很有用;默认情况下遇到错误直接退出;简而言之即忽略错误继续解压,默认遇到错误直接退出;
- --level=NUMBER,指定全量归档的增量归档文件的转储级别,全量为--level=0,增量依次为1、2、3、...,通常与-g参数一起使用;
- -n, --seek,归档可查找,tar -cn ...tar显示tar文件中所有文件内容;
- --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,提取时不提取系统中已存在的现有文件,将其视为错误
- --keep-directory-symlink,提取时保留到目录的现有符号链接,tar默认会保留符号链接,与--dereference参数相反
- --keep-newer-files,提取时不提取系统中现有文件,比归档中文件版本新,或者与归档中文件版本一致的文件;即如果归档中有新版本的文件会被提取覆盖系统中的现有文件;
- --no-overwrite-dir,提取归档时,保留已存在目录的元数据(比如用户和读写权限);只有系统上存在多层目录时才有效,且对最内层目录无效;可使用stat dir查看,其中Access、Modify、Change时间会有所变化;
- --overwrite,提取归档时,覆盖已存在的文件;
- --overwrite-dir,提取归档时,覆盖已存在目录的元数据(默认),与--no-overwrite-dir效果相反;
- --recursive-unlink,在提取目录之前去除目录递归(删除目录文件夹),无明显效果,与默认差不多;
- --remove-files,将文件添加到存档后删除文件;同样适用于目录归档
- --skip-old-files,提取时不要替换现有文件,静默地跳过它们;与-k类似;
- -U, --unlink-first,在对每个文件进行提取之前先删除它;
- -W, --verify,在写入归档文件后尝试验证它;
④选择输出流:
- --ignore-command-error,忽略命令错误而导致执行退出;主要处理外部压缩工具gzip、bzip2时的错误怎么处理,启动参数会跳过错误继续执行,未启动参数报错后立即终止压缩/解压;
- --no-ignore-command-error,将非零退出视为错误,与--ignore-command-error相反;
- -O, --to-stdout,将文件提取到标准输出
- --to-command=COMMAND,管道提取文件到另一个程序
⑤文件属性的处理:
- --atime-preserve[=METHOD],保留对转储文件的访问时间,或者通过恢复读取后的时间(METHOD='replace',默认)或不首先设置时间(METHOD='system');测试无效果;
- --delay-directory-restore,将解压目录的修改次数和权限设置延迟到解压结束,无效果;
- --group=NAME,创建归档时,强制NAME作为添加文件的组权限名称;
- --mode=CHANGES,强制(符号)模式,对新增文件的更改;无法使用,没说明模式可设置哪些参数;
- --mtime=DATE-OR-FILE,创建归档时,设置归档中文件的mtime为DATE-OR-FILE;
- -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 acl支持
- --no-acls,关闭POSIX acl支持
- --no-selinux,禁用SELinux上下文支持
- --no-xattrs,禁用扩展属性支持
- --selinux,启用SELinux上下文支持
- --xattrs,启用扩展属性支持
- --xattrs-exclude=MASK,指定xattr键的排除模式
- --xattrs-include=MASK,为xattr键指定包含模式
⑦设备选择和切换:
- -f, --file=ARCHIVE,使用归档文件或归档设备
- --force-local,归档文件是本地的,即使它有冒号;通常冒号是远程文件的书写格式,使用该参数,强制将带有冒号的文件名称视为本地文件;
- -F, --info-script=NAME, --new-volume-script=NAME,在每个磁带的末尾运行脚本(暗含-M)
- -L, --tape-length=NUMBER,写入NUMBER * 1024字节后更改磁带
- -M, --multi-volume,创建/列表/提取多卷存档
- --rmt-command=COMMAND,使用给定的rmt命令而不是rmt
- --rsh-command=COMMAND,使用远程命令代替rsh
- --volno-file=FILE,使用/update文件中的卷号
⑧装置块Device blocking:
- -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 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,与--format=v7相同
- --pax-option=keyword[[:]=value][,keyword[[:]=value]]...,控制pax的关键字,需要与--format=pax一起使用,keyword=value可设置为"charset=UTF-8"、"mtime:=%s.%N"、"delete=uid"、"delete=gid"
- --posix,与--format=posix相同
- -V, --label=TEXT,创建归档时定义一个标签为TEXT;在查看归档内容(-t)和提取归档时显示标签TEXT内容;
⑩压缩选项:
- -a, --auto-compress,使用归档后缀来确定压缩程序
- -I, --use-compress-program=PROG,通过PROG压缩方式归档(必须接受-d),简言之通过-l指定压缩方式,PROG可取值bzip2、xz、lzip;
- -j, --bzip2,通过bzip2压缩归档文件,归档命名后缀为.tar.bz2
- -J, --xz,通过xz压缩归档文件,归档命名后缀为.tar.xz
- --lzip,通过lzip压缩归档文件,归档命名后缀为.tar.lz;测试报错
- --lzma,通过lzma压缩归档文件,归档命名后缀为.tar.lzma;测试报错
- --lzop,通过lzma压缩归档文件,归档命名后缀为.tar.lzo
- --no-auto-compress,不使用归档后缀来确定压缩程序,与-a参数相反
- -z, --gzip, --gunzip, --ungzip,通过gzip压缩归档文件,归档命名后缀为.tar.gz
- -Z, --compress, --uncompress,通过compress压缩归档文件,归档命名后缀为.tar.Z
⑪本地文件选择:
- --add-file=FILE,将给定的文件添加到存档(如果其名称以破折号开头,则很有用);测试发现不是追加,是替换效果
- --backup[=CONTROL],删除前备份,选择版本控制;提取压缩归档时,系统已存在文件进行版本控制和备份,CONTROL可取值有none / off(不备份直接覆盖,默认值)、numbered / t(创建带数字的后缀备份)、nil / existing(如之前有带数字的后缀就继续数字备份,否者使用简单后缀)、never / simple(简单后缀)
- -C, --directory=DIR,更改为目录DIR,而不是默认的当前目录
- --exclude=PATTERN,排除文件,以PATTERN形式给出(创建/提取归档都适用)
- --exclude-backups,排除备份和锁定文件
- --exclude-caches,排除包含CACHEDIR的目录内容。TAG,除了标记文件本身
- --exclude-caches-all,排除包含CACHEDIR.TAR的目录
- --exclude-caches-under,排除包含CACHEDIR.TAR目录下的所有内容
- --exclude-tag=FILE,排除包含FILE的目录的内容,除了FILE本身
- --exclude-tag-all=FILE,排除包含FILE的目录
- --exclude-tag-under=FILE,排除包含FILE的目录下的所有内容
- --exclude-vcs,排除版本控制系统目录;归档时会自动跳过.git .svn .hg等版本控制目录及其目录下所有的文件和目录
- -h, --dereference,创建归档时,将软链接转储为它们指向的实际文件;即软链接会变成实际的文件或者目录;
- --hard-dereference,创建归档时,将硬链接转储为它们所引用的文件;即硬链接会变成实际的文件或者目录;
- -K, --starting-file=MEMBER-NAME,提取归档时,从MEMBER-NAME开始提取;MEMBER-NAME名称可以通过tar -tf查看,-tf查看时也可以使用该参数;
- --newer-mtime=DATE,仅比较数据更改时的日期和时间
- --no-null,-T参数使用时,指定文件命令列表的分隔方式,该方式为换行\n,为默认值
- --no-recursion,指定归档的目录层级,指定的目录层级之下的文件或者目录不会被归档
- --no-unquote,不取消用-T读取的文件名的引号
- --null,-T参数使用时,指定文件命令列表的分隔方式,与--no-null对立
- -N, --newer=DATE-OR-FILE, --after-date=DATE-OR-FILE,归档ctime在DATE-OR-FILE指定时间及其之后的文件
- --one-file-system,在创建存档时保留在本地文件系统中的中;归档备份"/"目录不归档/proc、/mnt、/dev等挂载内容;
- -P, --absolute-names,不要从文件名中去掉前导"/"(更目录)
- --recursion,递归到目录(默认)
- --suffix=STRING,删除前备份,覆盖通常的后缀('~',除非被环境变量SIMPLE_BACKUP_SUFFIX覆盖);指定后备份后缀还是'~'
- -T, --files-from=FILE,从FILE文件内容指定的文件名称列表提取/创建归档
- --unquote,用-T读取的文件名取消引号(默认)
- -X, --exclude-from=FILE,排除FILE文件中列出的文件或者目录,与-T相反
⑫文件名转换:
- --strip-components=NUMBER,提取时从文件名中剥离前NUMBER层目录
- --transform=EXPRESSION, --xform=EXPRESSION,使用sed替换表达式来转换文件名
⑬文件名匹配选项(影响排除和包含模式):
- --anchored,模式匹配文件名开头
- --ignore-case,忽略大小写匹配,比如使用--exclude时使用
- --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,禁用字符串字符的引号
- --quote-chars=STRING,另外引用字符串中的字符
- --quoting-style=STYLE,设置名称引用样式;下面是有效的STYLE值
- literal
- shell
- shell-always
- c
- c-maybe
- escape
- locale
- clocale
- -R, --block-number,显示每条消息存档中的块号,如:tar -Rtf file.tar
- --show-defaults,显示tar默认值,如:tar --show-defaults
- --show-omitted-dirs,列出或提取出现错误时,列出不匹配搜索条件的每个目录
- --show-transformed-names, --show-stored-names,-tf查看列表显示转换后的文件或存档名称,与--transform参数或者--strip-components参数一起使用
- --totals[=SIGNAL],打印归档处理后的总字节数;tar -cvf test.tar *.txt --totals
- --utc,以UTC格式打印文件修改时间,-cvf与-tf创建归档和查看归档列表时使用
- -v, --verbose,标准输出处理过程中的相关信息
- --warning=KEYWORD,告警控制;可取值none(禁止所有告警)、all(启动所有告警)、no-timestamp(忽略归档中的时间戳异常的告警)
- -w, --interactive, --confirmation,每归档一个目标都要输入y确认
⑮其他选择:
- -?, --help,给出以下帮助列表
- --restrict,禁用一些可能有害的选项
- --usage,给出简短的用法信息
- --version,打印程序版本
⑯tar默值,可通过tar --show-defaults查看:
- --format=gnu -f- -b20 --quoting-style=escape --rmt-command=/etc/rmt --rsh-command=/usr/bin/ssh
4.常用用例
4.1.归档文件内容查看
## 查看归档文件(tar文件)内的文件列表
[root@node1 test]# tar -tf file1.txt.tar
file1.txt
[root@node1 test]#
4.2.主动运行方式
4.2.1.将一个归档文件附加到另一个归档文件中
bash
[root@node1 test]# tar -tf file1.txt.tar
file1.txt
[root@node1 test]# tar -tf file2.txt.tar
file2.txt
[root@node1 test]# tar -Af file1.txt.tar file2.txt.tar
[root@node1 test]# tar -tf file1.txt.tar
file1.txt
file2.txt
[root@node1 test]#
4.2.2.对比归档文件与解压后文件系统中文件的差异
bash
[root@node1 test]# ll
total 643712
-rw-r--r--. 1 root root 439439360 May 13 22:50 file1.txt.tar
-rw-r--r--. 1 root root 219719680 May 13 22:49 file2.txt.tar
[root@node1 test]# tar -xvf file1.txt.tar
file1.txt
file2.txt
[root@node1 test]# tar -dvf file1.txt.tar ## 文件一致,仅输出归档文件内容列表
file1.txt
file2.txt
[root@node1 test]# ll
total 1072840
-rw-r--r--. 1 root root 219712350 May 13 22:21 file1.txt
-rw-r--r--. 1 root root 439439360 May 13 22:50 file1.txt.tar
-rw-r--r--. 1 root root 219712350 Mar 2 03:05 file2.txt
-rw-r--r--. 1 root root 219719680 May 13 22:49 file2.txt.tar
[root@node1 test]# rm -rf file2.txt
[root@node1 test]# tar -dvf file1.txt.tar ## 文件不一致,输出归档文件内容列表,且输出不存在的文件提升
file1.txt
file2.txt
tar: file2.txt: Warning: Cannot stat: No such file or directory
[root@node1 test]#
4.2.3.删除归档文件中的指定内容
bash
[root@node1 test]# tar -tf file1.txt.tar
file1.txt
file2.txt
[root@node1 test]# tar --delete -f file1.txt.tar file1.txt
[root@node1 test]# tar -tf file1.txt.tar
file2.txt
[root@node1 test]#
4.2.4.将文件追加到存档文件末尾
bash
[root@node1 test]# ll
total 643708
-rw-r--r--. 1 root root 219712350 May 13 23:13 file1.txt
-rw-r--r--. 1 root root 219719680 May 13 23:07 file1.txt.tar
-rw-r--r--. 1 root root 219719680 May 13 22:49 file2.txt.tar
[root@node1 test]# tar -tf file1.txt.tar
file2.txt
[root@node1 test]# tar -rf file1.txt.tar file1.txt
[root@node1 test]# tar -tf file1.txt.tar
file2.txt
file1.txt
[root@node1 test]#
4.2.5.验证归档文件是否存在
bash
[root@node1 test]# ll
total 643704
-rw-r--r--. 1 root root 439429120 May 13 23:18 file1.txt.tar
-rw-r--r--. 1 root root 219719680 May 13 22:49 file2.txt.tar
[root@node1 test]# tar --test-label -f file2.txt.tar
[root@node1 test]# echo $?
0
[root@node1 test]# tar --test-label -f file3.txt.tar
tar: file3.txt.tar: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
[root@node1 test]# echo $?
2
[root@node1 test]#
4.2.6.更新归档文件里面更新过的新版本文件
bash
[root@node1 Desktop]# tar -tf file.tar
file1.txt
file2.txt
file3.txt
file4.txt
[root@node1 Desktop]# vi file2.txt
[root@node1 Desktop]#
[root@node1 Desktop]# tar -uvf file.tar file1.txt file2.txt file3.txt file4.txt
file2.txt
[root@node1 Desktop]# tar -tf file.tar
file1.txt
file2.txt
file3.txt
file4.txt
file2.txt
[root@node1 Desktop]#
4.2.7.从归档中提取指定文件或全部文件
bash
[root@node1 test]# tar -tf file.tar
file1.txt
file2.txt
file3.txt
file4.txt
file2.txt
[root@node1 test]# tar -xvf file.tar file3.txt
file3.txt
[root@node1 test]# ll
total 1287384
-rw-r--r--. 1 root root 219712350 May 13 22:21 file3.txt
-rw-r--r--. 1 root root 1098567680 May 13 23:48 file.tar
[root@node1 test]# tar -xvf file.tar
file1.txt
file2.txt
file3.txt
file4.txt
file2.txt
[root@node1 test]# ll
total 1931076
-rw-r--r--. 1 root root 219712350 May 13 22:21 file1.txt
-rw-r--r--. 1 root root 219712359 May 13 23:46 file2.txt
-rw-r--r--. 1 root root 219712350 May 13 22:21 file3.txt
-rw-r--r--. 1 root root 219712350 May 13 22:21 file4.txt
-rw-r--r--. 1 root root 1098567680 May 13 23:48 file.tar
[root@node1 test]#
4.3. 操作修饰符
4.3.1.是否检查设备
bash
[root@node1 test]# tar --check-device -uvf file.tar file1.txt # 更新替换追加
file1.txt
[root@node1 test]# vi file1.txt ## 更新文件
[root@node1 test]# tar --no-check-device -uvf file.tar file1.txt # 更新替换追加
file1.txt
[root@node1 test]#
4.3.2.全量备份与增量备份
bash
[root@node1 Desktop]# tar -g snapshot.file -cvf file.full.txt.tar file[1-4].txt # 全量备份,生产快照文件snapshot.file
file1.txt
file2.txt
file3.txt
file4.txt
[root@node1 Desktop]# cat snapshot.file
GNU tar-1.26-2
1747152665332697702[root@node1 Desktop]# vi file3.txt # 更新文件
[root@node1 Desktop]# tar -g snapshot.file -cvf file.inc.txt.tar file[1-4].txt # 增量备份,更新快照文件snapshot.file
file3.txt
[root@node1 Desktop]# cat snapshot.file
GNU tar-1.26-2
1747152815368262961[root@node1 Desktop]#
4.3.3.全量增量备份级别指定
bash
[root@node1 Desktop]# tar --level=0 -g snapshot.file -cvf file.full.txt.tar file[1-4].txt
file1.txt
file2.txt
file3.txt
file4.txt
[root@node1 Desktop]# cat snapshot.file
GNU tar-1.26-2
1747154213825223342[root@node1 Desktop]# vi file2.txt
[root@node1 Desktop]# tar --level=1 -g snapshot.file -cvf file.inc.txt.tar file[1-4].txt
file2.txt
[root@node1 Desktop]# cat snapshot.file
GNU tar-1.26-2
1747154260428040570[root@node1 Desktop]#
4.3.4.归档中多版本相同名称文件,处理指定的文件
bash
[root@node1 Desktop]# tar -tf file.tar
file5.txt
file6.txt
file5.txt
[root@node1 Desktop]# tar --delete -f file.tar --occurrence=2 file5.txt # 指定删除第2个file5.txt
[root@node1 Desktop]# tar -tf file.tar
file5.txt
file6.txt
[root@node1 Desktop]#
----------------------------------------------------------
[root@node1 Desktop]# tar -tf file.tar
file5.txt
file6.txt
file5.txt
[root@node1 Desktop]# tar --delete -f file.tar --occurrence=1 file5.txt # 指定删除第1个file5.txt
[root@node1 Desktop]# tar -tf file.tar
file6.txt
file5.txt
[root@node1 Desktop]#
4.3.5.稀疏文件的归档
bash
[root@node1 Desktop]# qemu-img create -f raw file8.txt 2G # 创建一个稀疏文件
Formatting 'file8.txt', fmt=raw size=2147483648
[root@node1 Desktop]# ll file8.txt
-rw-r--r--. 1 root root 2147483648 May 14 01:49 file8.txt
[root@node1 Desktop]# ll file8.txt -h # 稀疏文件ls -h查看ll -h查看,大小显示比较大
-rw-r--r--. 1 root root 2.0G May 14 01:49 file8.txt
[root@node1 Desktop]# du -sh file8.txt # 使用du -sh查看就很小
0 file8.txt
[root@node1 Desktop]#
[root@node1 Desktop]# tar -cvf file8.txt.tar file8.txt # 普通归档文件与ls -h的结果接近
file8.txt
[root@node1 Desktop]# tar -Scvf file8.txt.1.tar file8.txt # 稀疏归档与du -sh的结果接近
file8.txt
[root@node1 Desktop]# ll -h file8*
-rw-r--r--. 1 root root 2.0G May 14 01:49 file8.txt
-rw-r--r--. 1 root root 10K May 14 01:51 file8.txt.1.tar
-rw-r--r--. 1 root root 2.1G May 14 01:50 file8.txt.tar
[root@node1 Desktop]#
4.4.输入控制覆盖寄存器
4.4.1.提取归档时,是否替换系统已存在文件
bash
[root@node1 test]# ll
total 1072832
-rw-r--r--. 1 root root 219712350 May 13 22:21 file1.txt
-rw-r--r--. 1 root root 28 May 14 01:24 file5.txt
-rw-r--r--. 1 root root 20 May 14 00:45 file6.txt
-rw-r--r--. 1 root root 878858240 May 14 02:00 file.tar
[root@node1 test]# tar -tf file.tar
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
file6.txt
[root@node1 test]# tar -xvf file.tar # 提取归档时,替换系统已存在文件
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
file6.txt
[root@node1 test]# ll
total 1716524
-rw-r--r--. 1 root root 219712350 May 13 22:21 file1.txt
-rw-r--r--. 1 root root 219712363 May 14 00:37 file2.txt
-rw-r--r--. 1 root root 219712372 May 14 00:13 file3.txt
-rw-r--r--. 1 root root 219712350 May 13 22:21 file4.txt
-rw-r--r--. 1 root root 28 May 14 01:24 file5.txt
-rw-r--r--. 1 root root 20 May 14 00:45 file6.txt
-rw-r--r--. 1 root root 878858240 May 14 02:00 file.tar
[root@node1 test]#
----------------------------------------------------------
[root@node1 test]# ll
total 1072832
-rw-r--r--. 1 root root 219712350 May 13 22:21 file1.txt
-rw-r--r--. 1 root root 28 May 14 01:24 file5.txt
-rw-r--r--. 1 root root 20 May 14 00:45 file6.txt
-rw-r--r--. 1 root root 878858240 May 14 02:00 file.tar
[root@node1 test]# tar -kxvf file.tar # 提取归档时,不替换系统已存在文件,视其为错误
file1.txt
tar: file1.txt: Cannot open: File exists
file2.txt
file3.txt
file4.txt
file5.txt
tar: file5.txt: Cannot open: File exists
file6.txt
tar: file6.txt: Cannot open: File exists
tar: Exiting with failure status due to previous errors
[root@node1 test]# ll
total 1716524
-rw-r--r--. 1 root root 219712350 May 13 22:21 file1.txt
-rw-r--r--. 1 root root 219712363 May 14 00:37 file2.txt
-rw-r--r--. 1 root root 219712372 May 14 00:13 file3.txt
-rw-r--r--. 1 root root 219712350 May 13 22:21 file4.txt
-rw-r--r--. 1 root root 28 May 14 01:24 file5.txt
-rw-r--r--. 1 root root 20 May 14 00:45 file6.txt
-rw-r--r--. 1 root root 878858240 May 14 02:00 file.tar
4.4.2.是否覆盖新文件
bash
[root@node1 test]# ll
total 858264
-rw-r--r--. 1 root root 219712350 May 13 22:21 file1.txt
-rw-r--r--. 1 root root 219712363 May 14 00:37 file2.txt
-rw-r--r--. 1 root root 219712372 May 14 00:13 file3.txt
-rw-r--r--. 1 root root 219712350 May 13 22:21 file4.txt
-rw-r--r--. 1 root root 24 May 14 02:28 file5.txt
-rw-r--r--. 1 root root 20 May 14 00:45 file6.txt
[root@node1 test]# tar -cvf file.tar file[1-6].txt
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
file6.txt
[root@node1 test]# vi file5.txt # 更新文件
[root@node1 test]# tar -xvf file.tar # 默认提取归档是覆盖系统上已存在的文件
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
file6.txt
[root@node1 test]#
----------------------------------------------------------
[root@node1 test]# ll
total 1716524
drwxr-xr-x. 2 root root 124 May 14 04:43 dir1
-rw-r--r--. 1 root root 219712353 May 14 04:43 file1.txt
-rw-r--r--. 1 root root 219712363 May 14 04:43 file2.txt
-rw-r--r--. 1 root root 219712391 May 14 04:43 file3.txt
-rw-r--r--. 1 root root 219712350 May 14 04:43 file4.txt
-rw-r--r--. 1 root root 72 May 14 04:43 file5.txt
-rw-r--r--. 1 root root 20 May 14 04:43 file6.txt
-rw-r--r--. 1 root root 878858240 May 14 04:43 file.tar
[root@node1 test]# vi file5.txt # 编辑文件使得成为新的版本文件
[root@node1 test]# rm -rf file.tar
[root@node1 test]# tar -cvf file.tar file[1-6].txt # 重新归档打包
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
file6.txt
[root@node1 test]# cp file.tar dir1/ # 覆盖之前的归档文件
cp: overwrite 'dir1/file.tar'? yes
[root@node1 test]# cd dir1/
[root@node1 dir1]# ll # 查看之前归档提取的文件,下面添加--keep-newer-files参数提取
total 1716524
-rw-r--r--. 1 root root 219712353 May 14 04:43 file1.txt
-rw-r--r--. 1 root root 219712363 May 14 04:43 file2.txt
-rw-r--r--. 1 root root 219712391 May 14 04:43 file3.txt
-rw-r--r--. 1 root root 219712350 May 14 04:43 file4.txt
-rw-r--r--. 1 root root 72 May 14 04:43 file5.txt
-rw-r--r--. 1 root root 20 May 14 04:43 file6.txt
-rw-r--r--. 1 root root 878858240 May 14 04:43 file.tar
[root@node1 dir1]# tar --keep-newer-files -xvf file.tar # 归档中新版本文件会被提取并替换系统中对应的现有文件,归档中其他旧版本或者相同版本不会被提取
file1.txt
tar: Current `file1.txt' is newer or same age
file2.txt
tar: Current `file2.txt' is newer or same age
file3.txt
tar: Current `file3.txt' is newer or same age
file4.txt
tar: Current `file4.txt' is newer or same age
file5.txt
file6.txt
tar: Current `file6.txt' is newer or same age
[root@node1 dir1]#
4.4.3.保留目录元数据作用域
bash
[root@node1 dir2]# chmod -R 777 dir1
[root@node1 dir2]# chown -R qwer:qwer dir1
[root@node1 dir2]# ll
total 12
drwxrwxrwx. 4 qwer qwer 49 May 14 13:32 dir1
-rw-r--r--. 1 root root 10240 May 14 05:01 dir1.tar
[root@node1 dir2]# ll dir1
total 4
drwxrwxrwx. 3 qwer qwer 36 May 14 13:32 dir11
drwxrwxrwx. 3 qwer qwer 36 May 14 13:32 dir12
-rwxrwxrwx. 1 qwer qwer 17 May 14 04:49 file1.txt
[root@node1 dir2]# ll dir1/dir11
total 4
drwxrwxrwx. 2 qwer qwer 6 May 14 05:00 dir21 # 修改了用户和读写执行权限
-rwxrwxrwx. 1 qwer qwer 15 May 14 04:48 file1.txt
[root@node1 dir2]#
[root@node1 dir2]# tar --no-overwrite-dir -xvf dir1.tar # 之前解压过,修改权限后,使用--no-overwrite-dir参数再解压
dir1/
dir1/dir11/
dir1/dir11/file1.txt
dir1/dir11/dir21/
dir1/dir12/
dir1/dir12/file1.txt
dir1/dir12/dir22/
dir1/file1.txt
[root@node1 dir2]# ll
total 12
drwxrwxrwx. 4 qwer qwer 49 May 14 13:33 dir1 # 已经存在的非最内层目录保留了元数据
-rw-r--r--. 1 root root 10240 May 14 05:01 dir1.tar
[root@node1 dir2]# ll dir1
total 4
drwxrwxrwx. 3 qwer qwer 36 May 14 13:33 dir11 # 已经存在的非最内层目录保留了元数据
drwxrwxrwx. 3 qwer qwer 36 May 14 13:33 dir12 # 已经存在的非最内层目录保留了元数据
-rw-r--r--. 1 root root 17 May 14 04:49 file1.txt # 仅对目录有效果,对文件无效果
[root@node1 dir2]# ll dir1/dir11
total 4
drwxr-xr-x. 2 root root 6 May 14 05:00 dir21 # 已经存在的最内层目录没有保留元数据
-rw-r--r--. 1 root root 15 May 14 04:48 file1.txt
[root@node1 dir2]#
4.4.4.归档提取覆盖系统上现有文件
bash
[root@node1 dir1]# cat file1.txt
aaaaaaaa
bbbbbbbb
cccccccc
[root@node1 dir1]# tar -cvf file1.tar file1.txt
file1.txt
[root@node1 dir1]# ll
total 16
-rw-r--r--. 1 root root 10240 May 14 13:54 file1.tar
-rw-r--r--. 1 root root 27 May 14 13:54 file1.txt
[root@node1 dir1]# vi file1.txt
[root@node1 dir1]# cat file1.txt # 文件修改后保留两行
aaaaaaaa
bbbbbbbb
[root@node1 dir1]# tar --overwrite -xvf file1.tar # 覆盖提取文件
file1.txt
[root@node1 dir1]# cat file1.txt # 覆盖后文件还是原始的三行
aaaaaaaa
bbbbbbbb
cccccccc
[root@node1 dir1]#
4.4.5.文件添加到归档后删除文件,同样适用于目录归档
bash
[root@node1 dir1]# ll
total 4
-rw-r--r--. 1 root root 27 May 14 13:54 file1.txt
[root@node1 dir1]# tar --remove-files -cvf file1.tar file1.txt
file1.txt
[root@node1 dir1]# ll
total 12
-rw-r--r--. 1 root root 10240 May 14 14:10 file1.tar
[root@node1 dir1]#
4.4.6.提取归档时,先删除对应内容(删除目录时,提升目录非空错误)
bash
[root@node1 dir2]# ll
total 12
drwxr-xr-x. 4 root root 49 May 14 04:49 dir1
-rw-r--r--. 1 root root 10240 May 14 05:01 dir1.tar
[root@node1 dir2]# tar -Uxvf dir1.tar
dir1/
tar: dir1: Cannot unlink: Directory not empty
dir1/dir11/
tar: dir1/dir11: Cannot unlink: Directory not empty
dir1/dir11/file1.txt
dir1/dir11/dir21/
dir1/dir12/
tar: dir1/dir12: Cannot unlink: Directory not empty
dir1/dir12/file1.txt
dir1/dir12/dir22/
dir1/file1.txt
tar: Exiting with failure status due to previous errors
[root@node1 dir2]#
4.4.7.写入归档后验证归档内文件目录
bash
[root@node1 dir2]# tar -Wcvf dir2.tar dir1/
dir1/
dir1/dir11/
dir1/dir11/file1.txt
dir1/dir11/dir21/
dir1/dir12/
dir1/dir12/file1.txt
dir1/dir12/dir22/
dir1/file1.txt
Verify dir1/
Verify dir1/dir11/
Verify dir1/dir11/file1.txt
Verify dir1/dir11/dir21/
Verify dir1/dir12/
Verify dir1/dir12/file1.txt
Verify dir1/dir12/dir22/
Verify dir1/file1.txt
[root@node1 dir2]#
4.5.选择输出流
4.5.1.将归档中的文件提取到标准输出
bash
[root@node1 dir1]# tar -Oxvf file1.tar
file1.txt
aaaaaaaa
bbbbbbbb
cccccccc
[root@node1 dir1]# ll
total 12
-rw-r--r--. 1 root root 10240 May 14 14:10 file1.tar
[root@node1 dir1]#
4.5.2.将归档中文件提取到指定命令进行处理
bash
[root@node1 dir1]# tar -xvf file1.tar --to-command='cat'
file1.txt
aaaaaaaa
bbbbbbbb
cccccccc
[root@node1 dir1]# ll
total 12
-rw-r--r--. 1 root root 10240 May 14 14:10 file1.tar
[root@node1 dir1]# tar -xvf file1.tar --to-command='cat | grep aaa'
file1.txt
aaaaaaaa
[root@node1 dir1]#
4.6.文件属性的处理
4.6.1.保留归档中文件访问时间,无效果
bash
[root@node1 dir1]# stat file1.txt
File: 'file1.txt'
Size: 219712350 Blocks: 429128 IO Block: 4096 regular file
Device: 803h/2051d Inode: 35033734 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2025-05-14 16:06:13.430271347 +0800
Modify: 2025-05-14 16:03:46.000000000 +0800
Change: 2025-05-14 16:07:43.513273581 +0800
Birth: -
[root@node1 dir1]# tar --atime-preserve='replace' -cvf file1.1.tar file1.txt
file1.txt
[root@node1 dir1]# tar --atime-preserve='replace' -xvf file1.1.tar
file1.txt
[root@node1 dir1]# stat file1.txt
File: 'file1.txt'
Size: 219712350 Blocks: 429128 IO Block: 4096 regular file
Device: 803h/2051d Inode: 35033734 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2025-05-14 16:08:02.103274042 +0800 # 访问时间改变了,无效果
Modify: 2025-05-14 16:03:46.000000000 +0800
Change: 2025-05-14 16:08:02.258274045 +0800
Birth: -
[root@node1 dir1]#
4.6.2.归档时,指定组权限
bash
[root@node1 dir2]# tar --group=qwer -cvf dir3.tar dir1/ # 归档时指定组权限归档
dir1/
dir1/dir11/
dir1/dir11/dir21/
dir1/dir11/file1.txt
dir1/dir12/
dir1/dir12/dir22/
dir1/dir12/file1.txt
dir1/file1.txt
[root@node1 dir2]# ll
total 36
drwxr-xr-x. 4 root root 49 May 14 04:49 dir1 # dir1 是root组权限
-rw-r--r--. 1 root root 10240 May 14 05:01 dir1.tar
-rw-r--r--. 1 root root 10240 May 14 14:19 dir2.tar
-rw-r--r--. 1 root root 10240 May 14 16:53 dir3.tar
[root@node1 dir2]#
[root@node1 dir2]#
[root@node1 dir2]# tar --group=qwer -xvf dir3.tar # 提取归档后的目录文件组权限就会改变
dir1/
dir1/dir11/
dir1/dir11/dir21/
dir1/dir11/file1.txt
dir1/dir12/
dir1/dir12/dir22/
dir1/dir12/file1.txt
dir1/file1.txt
[root@node1 dir2]# ll
total 36
drwxr-xr-x. 4 root qwer 49 May 14 04:49 dir1
-rw-r--r--. 1 root root 10240 May 14 05:01 dir1.tar
-rw-r--r--. 1 root root 10240 May 14 14:19 dir2.tar
-rw-r--r--. 1 root root 10240 May 14 16:53 dir3.tar
[root@node1 dir2]#
4.6.3.创建归档时,修改Modify时间(mtime)
bash
[root@node1 dir1]# stat file1.txt
File: 'file1.txt'
Size: 41 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 35033735 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2025-05-14 16:12:36.390280843 +0800
Modify: 2025-05-14 16:10:53.000000000 +0800 # 文件原来的mtime时间
Change: 2025-05-14 16:12:36.390280843 +0800
Birth: -
[root@node1 dir1]# tar --mtime='2025-05-14 16:01:01' -cvf file1.tar file1.txt
file1.txt
[root@node1 dir1]# tar -xvf file1.tar
file1.txt
[root@node1 dir1]# stat file1.txt
File: 'file1.txt'
Size: 41 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 35033735 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2025-05-14 17:27:39.489392499 +0800
Modify: 2025-05-14 16:01:01.000000000 +0800 # 归档提取后mtime时间是创建归档时指定的时间
Change: 2025-05-14 17:27:39.489392499 +0800
Birth: -
[root@node1 dir1]#
4.6.4.提取归档时,不保留文件的mtime时间
bash
[root@node1 dir1]# stat file1.txt
File: 'file1.txt'
Size: 41 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 35033735 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2025-05-14 17:33:54.282401793 +0800
Modify: 2025-05-14 16:01:01.000000000 +0800 # 正常归档提取的mtime时间
Change: 2025-05-14 17:33:54.282401793 +0800
Birth: -
[root@node1 dir1]# tar -mxvf file1.tar
file1.txt
[root@node1 dir1]# stat file1.txt
File: 'file1.txt'
Size: 41 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 35033735 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2025-05-14 17:34:26.442402590 +0800
Modify: 2025-05-14 17:34:26.442402590 +0800 # 指定-m参数归档提取的mtime时间
Change: 2025-05-14 17:34:26.442402590 +0800
Birth: -
[root@node1 dir1]#
4.6.5.提取归档文件时是否保留归档中文件原有的拥有者(owner)、用户组、用户
bash
[qwer@node1 dir1]$ ll dir1/
total 4
drwxr-xr-x. 3 qwer qwer 36 May 14 05:00 dir11
drwxr-xr-x. 3 qwer qwer 36 May 14 05:00 dir12
-rw-r--r--. 1 qwer qwer 17 May 14 04:49 file1.txt
[qwer@node1 dir1]$ tar -cvf dir1.tar dir1/ # 普通用户压缩目录,整个目录的用户组和用户权限都是qwer用户
dir1/
dir1/dir11/
dir1/dir11/dir21/
dir1/dir11/file1.txt
dir1/dir12/
dir1/dir12/dir22/
dir1/dir12/file1.txt
dir1/file1.txt
[qwer@node1 dir1]$
-----------------------------------------------------
[root@node1 opt]# tar --no-same-owner -xvf dir1.tar # 提取归档后文件拥有者(owner)为当前用户,与归档中文件拥有者(owner)不同
dir1/
dir1/dir11/
dir1/dir11/dir21/
dir1/dir11/file1.txt
dir1/dir12/
dir1/dir12/dir22/
dir1/dir12/file1.txt
dir1/file1.txt
[root@node1 opt]# ll dir1
total 4
drwxr-xr-x. 3 root root 36 May 14 05:00 dir11
drwxr-xr-x. 3 root root 36 May 14 05:00 dir12
-rw-r--r--. 1 root root 17 May 14 04:49 file1.txt
[root@node1 opt]#
-----------------------------------------------------
[root@node1 opt]# tar --same-owner -xvf dir1.tar # 提取归档后文件属性与归档中的文件拥有者(owner)相同
dir1/
dir1/dir11/
dir1/dir11/dir21/
dir1/dir11/file1.txt
dir1/dir12/
dir1/dir12/dir22/
dir1/dir12/file1.txt
dir1/file1.txt
[root@node1 opt]# ll dir1
total 4
drwxr-xr-x. 3 qwer qwer 36 May 14 05:00 dir11
drwxr-xr-x. 3 qwer qwer 36 May 14 05:00 dir12
-rw-r--r--. 1 qwer qwer 17 May 14 04:49 file1.txt
[root@node1 opt]#
-----------------------------------------------------
[root@node1 opt]# tar --no-same-permissions -xvf dir1.tar # 提取归档后文件属性保留归档前的用户组和用户权限
dir1/
dir1/dir11/
dir1/dir11/dir21/
dir1/dir11/file1.txt
dir1/dir12/
dir1/dir12/dir22/
dir1/dir12/file1.txt
dir1/file1.txt
[root@node1 opt]# ll dir1/
total 4
drwxr-xr-x. 3 qwer qwer 36 May 14 05:00 dir11
drwxr-xr-x. 3 qwer qwer 36 May 14 05:00 dir12
-rw-r--r--. 1 qwer qwer 17 May 14 04:49 file1.txt
[root@node1 opt]#
4.6.6.归档时,用户/组权限以数字方式保留,提取归档时显示数字对应的用户(与之前的用户可能不一样)或者显示数字(数字对应的用户不存在时)
bash
[root@node1 dir1]# cat /etc/passwd
... ...
u1:x:1001:1001::/home/u1:/bin/bash
[root@node1 opt]# ll
total 0
drwxr-xr-x. 4 u1 u1 49 May 14 04:49 dir1
... ...
[root@node1 opt]# tar --numeric-owner -cvf dir1.tar dir1/ # 归档时,指定用户/组权限使用数字方式保留
dir1/
dir1/dir11/
dir1/dir11/dir21/
dir1/dir11/file1.txt
dir1/dir12/
dir1/dir12/dir22/
dir1/dir12/file1.txt
dir1/file1.txt
[root@node1 opt]#
[root@node1 opt]# useradd u2
[root@node1 opt]# useradd u3
[root@node1 opt]# userdel u1
[root@node1 opt]# useradd u1
[root@node1 dir1]# cat /etc/passwd
... ...
u3:x:1002:1002::/home/u2:/bin/bash
u4:x:1003:1003::/home/u3:/bin/bash
u1:x:1004:1004::/home/u1:/bin/bash
[root@node1 opt]# tar --numeric-owner -xvf dir1.tar # tar -xvf dir1.tar # 以数字保留归档中文件的用户/组权限,解压后如果数字对应的用户不存在就显示数字
dir1/
dir1/dir11/
dir1/dir11/dir21/
dir1/dir11/file1.txt
dir1/dir12/
dir1/dir12/dir22/
dir1/dir12/file1.txt
dir1/file1.txt
[root@node1 opt]# ll
total 12
drwxr-xr-x. 4 1001 1001 49 May 14 04:49 dir1
... ...
[root@node1 opt]#
4.6.7.创建归档时,指定用户权限
bash
[root@node1 dir1]# ll
total 4
-rw-r--r--. 1 root root 41 May 14 17:34 file1.txt
[root@node1 dir1]# tar --owner='qwer' -cvf file1.tar file1.txt
file1.txt
[root@node1 dir1]# ll
total 16
-rw-r--r--. 1 root root 10240 May 14 18:35 file1.tar
-rw-r--r--. 1 root root 41 May 14 17:34 file1.txt
[root@node1 dir1]# tar -xvf file1.tar
file1.txt
[root@node1 dir1]# ll
total 16
-rw-r--r--. 1 root root 10240 May 14 18:35 file1.tar
-rw-r--r--. 1 qwer root 41 May 14 17:34 file1.txt # 提取归档后文件为创建归档时指定的用户权限
[root@node1 dir1]#
4.7.设备选择和切换
4.7.1.带有冒号的归档文件视为本地文件
bash
[root@node1 dir1]# tar -cvf file:1.tar file1.txt # 归档名称定义带有冒号,执行会卡住,Ctrl+c强制退出
^C
[root@node1 dir1]# tar --force-local -cvf file:1.tar file1.txt # 强制视为本地文件
file1.txt
[root@node1 dir1]# ll
total 16
-rw-r--r--. 1 root root 10240 May 14 19:19 file:1.tar
-rw-r--r--. 1 root root 20 May 14 19:18 file1.txt
[root@node1 dir1]#
4.8.档案格式选择
4.8.1.给归档设置一个标签
bash
[root@node1 dir1]# tar -cvf file1.tar file1.txt
file1.txt
[root@node1 dir1]# tar -V 'This is label' -cvf file1_1.tar file1.txt
This is label
file1.txt
[root@node1 dir1]# tar -tf file1.tar
file1.txt
[root@node1 dir1]# tar -tf file1_1.tar
This is label # 元数据中可以看到归档是自定义的标签
file1.txt
[root@node1 dir1]#
4.9.压缩选项
4.9.1.按照归档文件的后缀名进行归档或者压缩
bash
[root@node1 dir1]# tar -cvf file1.1.tar file1.txt
file1.txt
[root@node1 dir1]# tar -zcvf file1.2.tar file1.txt
file1.txt
[root@node1 dir1]# tar -acvf file1.3.tar file1.txt
file1.txt
[root@node1 dir1]# tar -acvf file1.4.tar.gz file1.txt
file1.txt
[root@node1 dir1]# ll
total 644964
-rw-r--r--. 1 root root 219719680 May 15 19:51 file1.1.tar
-rw-r--r--. 1 root root 639173 May 15 19:51 file1.2.tar.gz
-rw-r--r--. 1 root root 219719680 May 15 19:53 file1.3.tar
-rw-r--r--. 1 root root 639173 May 15 19:53 file1.4.tar.gz
-rw-r--r--. 1 root root 219712350 May 15 19:50 file1.txt
[root@node1 dir1]#
4.9.2.选择压缩方式进行归档
bash
[root@node1 dir1]# tar -zcvf dir2.tar.gz dir2/
dir2/
dir2/file4.txt
dir2/file56.tat
dir2/file5.txt
dir2/file6.txt
dir2/file7.txt
[root@node1 dir1]# tar -jcvf dir2.tar.bz2 dir2/
dir2/
dir2/file4.txt
dir2/file56.tat
dir2/file5.txt
dir2/file6.txt
dir2/file7.txt
[root@node1 dir1]# tar -Jcvf dir2.tar.xz dir2/
dir2/
dir2/file4.txt
dir2/file56.tat
dir2/file5.txt
dir2/file6.txt
dir2/file7.txt
[root@node1 dir1]# tar --lzip -cvf dir2.tar.lz dir2/
tar (child): lzip: Cannot exec: No such file or directory
tar (child): Error is not recoverable: exiting now
dir2/
dir2/file4.txt
[root@node1 dir1]# tar --lzma -cvf dir2.tar.lzma dir2/
tar (child): lzma: Cannot execdir2/
dir2/file4.txt
: No such file or directory
tar (child): Error is not recoverable: exiting now
[root@node1 dir1]#
[root@node1 dir1]# tar --lzop -cvf dir2.tar.lzop dir2/
dir2/
dir2/file4.txt
dir2/file56.tat
dir2/file5.txt
dir2/file6.txt
dir2/file7.txt
[root@node1 dir1]# tar -Zcvf dir2.tar.Z dir2/
dir2/
dir2/file4.txt
dir2/file56.tat
dir2/file5.txt
dir2/file6.txt
dir2/file7.txt
[root@node1 dir1]#
[root@node1 dir1]# ll
total 2388
drwxr-xr-x. 2 root root 92 May 15 20:31 dir2
-rw-r--r--. 1 root root 10782 May 15 20:32 dir2.tar.bz2
-rw-r--r--. 1 root root 639568 May 15 20:31 dir2.tar.gz
-rw-r--r--. 1 root root 0 May 15 20:46 dir2.tar.lz # 执行报错,压缩归档文件大小为0
-rw-r--r--. 1 root root 0 May 15 20:46 dir2.tar.lzma # 执行报错,压缩归档文件大小为0
-rw-r--r--. 1 root root 1100615 May 15 20:47 dir2.tar.lzop
-rw-r--r--. 1 root root 32500 May 15 20:40 dir2.tar.xz
-rw-r--r--. 1 root root 651647 May 15 20:47 dir2.tar.Z
[root@node1 dir1]# # 可更具其大小判断压缩比
4.10.本地文件选择
4.10.1.系统上如已经存在,可选择备份(使用数字备份)
bash
[root@node1 dir1]# ll
total 24
-rw-r--r--. 1 root root 14 May 15 20:58 file1.txt
-rw-r--r--. 1 root root 14 May 15 20:58 file2.txt
-rw-r--r--. 1 root root 14 May 15 20:58 file3.txt
-rw-r--r--. 1 root root 10240 May 15 21:49 file.tar
[root@node1 dir1]# tar -tf file.tar
file1.txt
file2.txt
file3.txt
[root@node1 dir1]# tar --backup=t -xvf file.tar
file1.txt
Renaming `file1.txt' to `file1.txt.~1~'
file2.txt
Renaming `file2.txt' to `file2.txt.~1~'
file3.txt
Renaming `file3.txt' to `file3.txt.~1~'
[root@node1 dir1]# ll
total 36
-rw-r--r--. 1 root root 14 May 15 20:58 file1.txt
-rw-r--r--. 1 root root 14 May 15 20:58 file1.txt.~1~ # 已存在文件进行备份,使用数字后缀
-rw-r--r--. 1 root root 14 May 15 20:58 file2.txt
-rw-r--r--. 1 root root 14 May 15 20:58 file2.txt.~1~
-rw-r--r--. 1 root root 14 May 15 20:58 file3.txt
-rw-r--r--. 1 root root 14 May 15 20:58 file3.txt.~1~
-rw-r--r--. 1 root root 10240 May 15 21:49 file.tar
[root@node1 dir1]#
4.10.2.提取归档内容到指定的目录(默认提取是当前目录)
bash
[root@node1 dir1]# ll dir2/
total 0
[root@node1 dir1]# tar -C dir2/ -xvf file.tar
file1.txt
file2.txt
file3.txt
[root@node1 dir1]# ll dir2/
total 12
-rw-r--r--. 1 root root 14 May 15 20:58 file1.txt
-rw-r--r--. 1 root root 14 May 15 20:58 file2.txt
-rw-r--r--. 1 root root 14 May 15 20:58 file3.txt
[root@node1 dir1]#
4.10.3.提取/创建归档时排除指定的文件
bash
[root@node1 dir1]# ll
total 12
-rw-r--r--. 1 root root 14 May 15 20:58 file1.txt
-rw-r--r--. 1 root root 14 May 15 20:58 file2.txt
-rw-r--r--. 1 root root 14 May 15 20:58 file3.txt
[root@node1 dir1]# tar --exclude=file2.txt -cvf file.tar file*.txt
file1.txt
file3.txt
[root@node1 dir1]#
------------------------------------------------------------------
[root@node1 dir1]# ll dir2/
total 0
[root@node1 dir1]# tar -C dir2/ --exclude=file2.txt -xvf file.tar
file1.txt
file3.txt
[root@node1 dir1]# ll dir2/
total 8
-rw-r--r--. 1 root root 14 May 15 20:58 file1.txt
-rw-r--r--. 1 root root 14 May 15 20:58 file3.txt
[root@node1 dir1]#
4.10.4.压缩时是否保留符号链接(软链接/硬链接)
bash
[root@node1 Desktop]# ll dir1
total 8
lrwxrwxrwx. 1 root root 5 May 16 04:41 dir -> dir2/
drwxr-xr-x. 2 root root 6 May 16 04:40 dir2
drwxr-xr-x. 2 root root 6 May 16 04:41 dir3
-rw-r--r--. 1 root root 12 May 16 04:40 file1.txt
-rw-r--r--. 1 root root 58 May 16 04:40 file2.txt
lrwxrwxrwx. 1 root root 9 May 16 04:41 file.txt -> file1.txt
[root@node1 Desktop]# tar --dereference -cvf dir1.tar dir1/
dir1/
dir1/file1.txt
dir1/file2.txt
dir1/dir2/
dir1/dir3/
dir1/file.txt
dir1/dir/
[root@node1 Desktop]# tar -tvf dir1.tar
drwxr-xr-x root/root 0 2025-05-16 04:41 dir1/
-rw-r--r-- root/root 12 2025-05-16 04:40 dir1/file1.txt
-rw-r--r-- root/root 58 2025-05-16 04:40 dir1/file2.txt
drwxr-xr-x root/root 0 2025-05-16 04:40 dir1/dir2/
drwxr-xr-x root/root 0 2025-05-16 04:41 dir1/dir3/
hrw-r--r-- root/root 0 2025-05-16 04:40 dir1/file.txt link to dir1/file1.txt
drwxr-xr-x root/root 0 2025-05-16 04:40 dir1/dir/
[root@node1 Desktop]# rm -rf dir1/
[root@node1 Desktop]# tar xvf dir1.tar
dir1/
dir1/file1.txt
dir1/file2.txt
dir1/dir2/
dir1/dir3/
dir1/file.txt
dir1/dir/
[root@node1 Desktop]# ll dir1
total 12
drwxr-xr-x. 2 root root 6 May 16 04:40 dir
drwxr-xr-x. 2 root root 6 May 16 04:40 dir2
drwxr-xr-x. 2 root root 6 May 16 04:41 dir3
-rw-r--r--. 2 root root 12 May 16 04:40 file1.txt
-rw-r--r--. 1 root root 58 May 16 04:40 file2.txt
-rw-r--r--. 2 root root 12 May 16 04:40 file.txt
[root@node1 Desktop]#
--------------------------------------------------
[root@node1 dir1]# ln file1.txt file.txt
[root@node1 dir1]# ll
total 12
drwxr-xr-x. 2 root root 6 May 16 04:40 dir2
drwxr-xr-x. 2 root root 6 May 16 04:41 dir3
-rw-r--r--. 2 root root 12 May 16 04:40 file1.txt
-rw-r--r--. 1 root root 58 May 16 04:40 file2.txt
-rw-r--r--. 2 root root 12 May 16 04:40 file.txt
[root@node1 dir1]# cd ..
[root@node1 Desktop]# tar --hard-dereference -cvf dir1.tar dir1/
dir1/
dir1/file1.txt
dir1/file2.txt
dir1/dir2/
dir1/dir3/
dir1/file.txt
[root@node1 Desktop]#
[root@node1 Desktop]# tar -xvf dir1.tar
dir1/
dir1/file1.txt
dir1/file2.txt
dir1/dir2/
dir1/dir3/
dir1/file.txt
[root@node1 Desktop]# ll dir1
total 12
drwxr-xr-x. 2 root root 6 May 16 04:40 dir2
drwxr-xr-x. 2 root root 6 May 16 04:41 dir3
-rw-r--r--. 1 root root 20 May 16 04:52 file1.txt
-rw-r--r--. 1 root root 58 May 16 04:40 file2.txt
-rw-r--r--. 1 root root 20 May 16 04:52 file.txt
[root@node1 Desktop]# vi dir1/file.txt
[root@node1 Desktop]# cat dir1/file.txt
asjdlfjaldk
iaaaaaa
ibbbbbb
[root@node1 Desktop]# cat dir1/file1.txt # 已经不是硬链接,硬链接修改一个,另一个也会更新
asjdlfjaldk
iaaaaaa
[root@node1 Desktop]#
4.10.5.从指定的文件开始解压
bash
[root@node1 Desktop]# tar -tf dir1.2.tar.gz
dir1/
dir1/file1.txt
dir1/file2.txt
dir1/file3.txt
dir1/file4.txt
dir1/file56.tat
dir1/file5.txt
dir1/file6.txt
dir1/file7.txt
dir1/file8.txt
[root@node1 Desktop]# tar -zxvf dir1.2.tar.gz -K dir1/file56.tat
dir1/file56.tat
dir1/file5.txt
dir1/file6.txt
dir1/file7.txt
dir1/file8.txt
[root@node1 Desktop]# ll dir1
total 2097176
-rw-r--r--. 1 root root 10240 May 15 20:02 file56.tat
-rw-r--r--. 1 root root 28 May 15 20:02 file5.txt
-rw-r--r--. 1 root root 20 May 15 20:02 file6.txt
-rw-r--r--. 1 root root 2390 May 15 20:02 file7.txt
-rw-r--r--. 1 root root 2147483648 May 15 20:02 file8.txt
[root@node1 Desktop]#
4.10.6.仅归档某一个时间及其之后时间的的文件(s时间包括mtime、ctime)
bash
[root@node1 Desktop]# stat dir1/* | grep Modify
Modify: 2025-05-15 20:02:15.000000000 +0800
Modify: 2025-05-15 20:02:15.000000000 +0800
Modify: 2025-05-15 20:02:15.000000000 +0800
Modify: 2025-05-16 05:14:29.701865574 +0800
Modify: 2025-05-16 05:12:46.702869002 +0800
[root@node1 Desktop]# tar --newer-mtime="2025-05-16 00:00:00" -cvf dir1.tar dir1/
dir1/
tar: dir1/file5.txt: file is unchanged; not dumped
tar: dir1/file6.txt: file is unchanged; not dumped
tar: dir1/file7.txt: file is unchanged; not dumped
dir1/file8.txt
dir1/file9.txt
[root@node1 Desktop]#
[root@node1 Desktop]# stat dir1/* | grep Change
Change: 2025-05-16 06:00:42.509773290 +0800
Change: 2025-05-16 06:00:42.509773290 +0800
Change: 2025-05-16 06:00:42.509773290 +0800
Change: 2025-05-16 06:01:29.106771739 +0800
Change: 2025-05-16 06:01:39.172771404 +0800
[root@node1 Desktop]#
[root@node1 Desktop]# tar -N '2025-05-16 06:00:45' -cvf dir1.tar dir1/
dir1/
tar: dir1/file5.txt: file is unchanged; not dumped
tar: dir1/file6.txt: file is unchanged; not dumped
tar: dir1/file7.txt: file is unchanged; not dumped
dir1/file8.txt
dir1/file9.txt
[root@node1 Desktop]#
4.10.7.在文件中指定(-T)/排除(-X)需要提取/创建归档的文件名称
bash
[root@node1 dir1]# ll
total 24
-rw-r--r--. 1 root root 28 May 15 20:02 file5.txt
-rw-r--r--. 1 root root 20 May 15 20:02 file6.txt
-rw-r--r--. 1 root root 2390 May 15 20:02 file7.txt
-rw-r--r--. 1 root root 15 May 16 05:14 file8.txt
-rw-r--r--. 1 root root 14 May 16 05:12 file9.txt
-rw-r--r--. 1 root root 20 May 16 05:27 filelist.txt
[root@node1 dir1]# cat filelist.txt
file5.txt
file7.txt
[root@node1 dir1]# tar -cvf dir1.tar -T filelist.txt
file5.txt
file7.txt
[root@node1 dir1]# tar --no-null -cvf dir1.tar -T filelist.txt
file5.txt
file7.txt
[root@node1 dir1]#
[root@node1 dir1]# tar --null -cvf dir1.tar -T filelist.txt
tar: file5.txt\nfile7.txt\n: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
[root@node1 dir1]#
--------------------------------------------------------
[root@node1 dir1]# ll
total 24
-rw-r--r--. 1 root root 28 May 15 20:02 file5.txt
-rw-r--r--. 1 root root 20 May 15 20:02 file6.txt
-rw-r--r--. 1 root root 2390 May 15 20:02 file7.txt
-rw-r--r--. 1 root root 25 May 16 06:01 file8.txt
-rw-r--r--. 1 root root 81 May 16 06:01 file9.txt
-rw-r--r--. 1 root root 20 May 16 06:36 filelist
[root@node1 dir1]# cat filelist
file6.txt
file9.txt
[root@node1 dir1]# tar -X filelist -cvf file.tar file*.txt
file5.txt
file7.txt
file8.txt
[root@node1 dir1]#
4.10.8.按照目录的层级进行归档
bash
[root@node1 Desktop]# ll dir1
total 20
drwxr-xr-x. 2 root root 23 May 16 05:39 dir1
drwxr-xr-x. 2 root root 23 May 16 05:39 dir2
-rw-r--r--. 1 root root 28 May 15 20:02 file5.txt
-rw-r--r--. 1 root root 20 May 15 20:02 file6.txt
-rw-r--r--. 1 root root 2390 May 15 20:02 file7.txt
-rw-r--r--. 1 root root 15 May 16 05:14 file8.txt
-rw-r--r--. 1 root root 14 May 16 05:12 file9.txt
[root@node1 Desktop]# ll dir1/dir1
total 0
-rw-r--r--. 1 root root 0 May 16 05:39 file1.txt
[root@node1 Desktop]# ll dir1/dir2
total 0
-rw-r--r--. 1 root root 0 May 16 05:39 file2.txt
[root@node1 Desktop]#
[root@node1 Desktop]# tar --no-recursion -cvf dir1.tar dir1/ # 仅归档指定的当前目录层级,其内部的文件或者目录都不会归档
dir1/ # 上面指定dir1,就归档该目录,其下的文件目录不会归档
[root@node1 Desktop]# tar --no-recursion -cvf dir1.tar dir1/* # 这里的dir1/*,时dir1的下面一层,再下一次就不会被归档
dir1/dir1/
dir1/dir2/
dir1/file5.txt
dir1/file6.txt
dir1/file7.txt
dir1/file8.txt
dir1/file9.txt
[root@node1 Desktop]#
4.10.9.带有根目录开始的目录或文件,-P参数归档不会去除根目录,看以下测试效果
bash
[root@node1 Desktop]# tar -cvPf back.tar /root/Desktop/dir1
/root/Desktop/dir1/
/root/Desktop/dir1/file5.txt
/root/Desktop/dir1/file6.txt
/root/Desktop/dir1/file7.txt
/root/Desktop/dir1/file8.txt
/root/Desktop/dir1/file9.txt
[root@node1 Desktop]#
[root@node1 Desktop]#
[root@node1 Desktop]# tar -cvf back1.tar /root/Desktop/dir1
tar: Removing leading `/' from member names # 没有-P参数
/root/Desktop/dir1/
/root/Desktop/dir1/file5.txt
/root/Desktop/dir1/file6.txt
/root/Desktop/dir1/file7.txt
/root/Desktop/dir1/file8.txt
/root/Desktop/dir1/file9.txt
[root@node1 Desktop]#
[root@node1 Desktop]# tar -xvPf back.tar
/root/Desktop/dir1/
/root/Desktop/dir1/file5.txt
/root/Desktop/dir1/file6.txt
/root/Desktop/dir1/file7.txt
/root/Desktop/dir1/file8.txt
/root/Desktop/dir1/file9.txt
[root@node1 Desktop]#
[root@node1 Desktop]# tar -xvf back1.tar # 没有-P参数
root/Desktop/dir1/
root/Desktop/dir1/file5.txt
root/Desktop/dir1/file6.txt
root/Desktop/dir1/file7.txt
root/Desktop/dir1/file8.txt
root/Desktop/dir1/file9.txt
[root@node1 Desktop]#
4.11.文件名转换
4.11.1.指定解压的目录层级
bash
[root@node1 dir1]# tar --strip-components=1 -xvf Desktop.tar
root/Desktop/dir1/
root/Desktop/dir1/file5.txt
root/Desktop/dir1/file6.txt
root/Desktop/dir1/file7.txt
root/Desktop/dir1/file8.txt
root/Desktop/dir1/file9.txt
root/Desktop/dir1/filelist
root/Desktop/dir1/file.tar
[root@node1 dir1]# ll
total 20
drwxr-xr-x. 3 root root 18 May 16 06:56 Desktop # 去除1层后是Desktop
-rw-r--r--. 1 root root 20480 May 16 06:54 Desktop.tar
[root@node1 dir1]#
[root@node1 dir1]# tar --strip-components=2 -xvf Desktop.tar
root/Desktop/dir1/
root/Desktop/dir1/file5.txt
root/Desktop/dir1/file6.txt
root/Desktop/dir1/file7.txt
root/Desktop/dir1/file8.txt
root/Desktop/dir1/file9.txt
root/Desktop/dir1/filelist
root/Desktop/dir1/file.tar
[root@node1 dir1]# ll
total 20
-rw-r--r--. 1 root root 20480 May 16 06:54 Desktop.tar
drwxr-xr-x. 2 root root 123 May 16 06:36 dir1 # 去除2层后是dir1
[root@node1 dir1]#
4.11.2.sed正则修改归档中文件的名称
bash
[root@node1 dir1]# ll
total 12
-rw-r--r--. 1 root root 10240 May 16 06:58 file.tar
[root@node1 dir1]# tar -tf file.tar
file5.txt
file6.txt
file7.txt
file8.txt
file9.txt
[root@node1 dir1]# tar -xvf file.tar --transform='s/file/test/g'
file5.txt
file6.txt
file7.txt
file8.txt
file9.txt
[root@node1 dir1]# ll
total 32
-rw-r--r--. 1 root root 10240 May 16 06:58 file.tar
-rw-r--r--. 1 root root 28 May 15 20:02 test5.txt
-rw-r--r--. 1 root root 20 May 15 20:02 test6.txt
-rw-r--r--. 1 root root 2390 May 15 20:02 test7.txt
-rw-r--r--. 1 root root 25 May 16 06:01 test8.txt
-rw-r--r--. 1 root root 81 May 16 06:01 test9.txt
[root@node1 dir1]#
4.12.文件名匹配选项
4.12.1.匹配规则(忽略大小写、严格大小写、通配符、逐字符匹配)
bash
[root@node1 dir1]# ll
total 20
-rw-r--r--. 1 root root 28 May 15 20:02 test5.txt
-rw-r--r--. 1 root root 20 May 15 20:02 test6.txt
-rw-r--r--. 1 root root 2390 May 15 20:02 test7.txt
-rw-r--r--. 1 root root 25 May 16 06:01 test8.txt
-rw-r--r--. 1 root root 81 May 16 06:01 test9.txt
[root@node1 dir1]# tar -cvf file.tar test* --ignore-case --exclude=Test7.txt # 忽略大小写匹配
test5.txt
test6.txt
test8.txt
test9.txt
[root@node1 dir1]# tar -cvf file.tar test* --no-ignore-case --exclude=Test7.txt # 严格大小写匹配
test5.txt
test6.txt
test7.txt
test8.txt
test9.txt
[root@node1 dir1]# tar -cvf file.tar test* --wildcards --exclude=test7.* # 通配符匹配
test5.txt
test6.txt
test8.txt
test9.txt
[root@node1 dir1]# tar -cvf file.tar test* --no-wildcards --exclude=test7.* # 逐字符匹配
test5.txt
test6.txt
test7.txt
test8.txt
test9.txt
[root@node1 dir1]#
4.12.2.显示进度
bash
[root@node1 Desktop]# tar -zxvf dir1.2.tar.gz -C dir2/ --checkpoint=10000 # 指定显示进度的频率,值越大越不频繁
dir1/
dir1/file1.txt
tar: Read checkpoint 10000
tar: Read checkpoint 20000
dir1/file2.txt
tar: Read checkpoint 30000
tar: Read checkpoint 40000
... ...
[root@node1 Desktop]# tar -zxvf dir1.2.tar.gz -C dir2/ --checkpoint=10000 --checkpoint-action=echo="%u files processed" # 自定义进度显示
dir1/
dir1/file1.txt
tar: 10000 files processed
tar: 20000 files processed
dir1/file2.txt
tar: 30000 files processed
tar: 40000 files processed
... ...
[root@node1 Desktop]# tar -zxvf dir1.2.tar.gz -C dir2/ --checkpoint=10000 --checkpoint-action=ttyout="%s %u \r" # 末尾可\r \n \t
... ...
[root@node1 Desktop]#
4.12.3.如果链接归档错误会输出信息
bash
[root@node1 dir1]# ln test5.txt test.txt
[root@node1 dir1]# tar -cvlf test.tar *
test5.txt
test6.txt
test7.txt
test8.txt
test9.txt
test.txt
[root@node1 dir1]# tar -tvf test.tar
-rw-r--r-- root/root 28 2025-05-15 20:02 test5.txt
-rw-r--r-- root/root 20 2025-05-15 20:02 test6.txt
-rw-r--r-- root/root 2390 2025-05-15 20:02 test7.txt
-rw-r--r-- root/root 25 2025-05-16 06:01 test8.txt
-rw-r--r-- root/root 81 2025-05-16 06:01 test9.txt
hrw-r--r-- root/root 0 2025-05-15 20:02 test.txt link to test5.txt
[root@node1 dir1]#
4.12.4.查看替换文件名称
bash
[root@node1 dir1]# tar -tf test.tar --transform='s/test/aaa/g'
test5.txt
test6.txt
test7.txt
test8.txt
test9.txt
test.txt
[root@node1 dir1]# tar -tf test.tar --transform='s/test/aaa/g' --show-transformed-names
aaa5.txt
aaa6.txt
aaa7.txt
aaa8.txt
aaa9.txt
aaa.txt
[root@node1 dir1]# cd /opt
[root@node1 opt]# tar -tf file.tar --strip-components=2
root/Desktop/dir1/
root/Desktop/dir1/test5.txt
root/Desktop/dir1/test6.txt
root/Desktop/dir1/test7.txt
root/Desktop/dir1/test8.txt
root/Desktop/dir1/test9.txt
root/Desktop/dir1/file.tar
[root@node1 opt]# tar -tf file.tar --strip-components=2 --show-transformed-names
dir1
dir1/test5.txt
dir1/test6.txt
dir1/test7.txt
dir1/test8.txt
dir1/test9.txt
dir1/file.tar
[root@node1 opt]#